rust-lang / rust-lang/rust-clippy

non_minimal_cfg suppression sometimes not effective

Open
#13,007 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Putting #![allow(clippy::non_minimal_cfg)] at the top of the module, or on the mod statement, sometimes doesn't work!

Lint Name

non_minimal_cfg

To reproduce

Cargo toml
[package]
name = "test"
version = "0.0.1"
edition = "2021"
publish = false

[features]
recent = []
src/lib.rs
mod middle;
src/middle.rs
#![allow(clippy::non_minimal_cfg)]

#[cfg(all(
    feature = "recent", // we want toml parser etc.
))]
mod inner;
src/middle/inner.rs

empty.

Or from git:
git clone https://gitlab.torproject.org/Diziet/rust-derive-deftly -b clippy-non-minmal-cfg-bug-repro
cd rust-derive-deftly
cargo  clippy --all-features

Actual output

    Checking test v0.0.1 (/volatile/rustcargo/d/rust-derive-deftly)
warning: unneeded sub `cfg` when there is only one condition
 --> src/middle.rs:3:7
  |
3 |   #[cfg(all(
  |  _______^
4 | |     feature = "recent", // we want toml parser etc.
5 | | ))]
  | |_^ help: try: `feature = "recent"`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#non_minimal_cfg
  = note: `#[warn(clippy::non_minimal_cfg)]` on by default

warning: `test` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s

Expected output

No lint output.

Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Also fails with current beta and nightly.

Notes

Moving the #![allow()] to an #[allow()] on mod middle doesn't help. Changing it to #[allow] to apply only to mod inner, makes the bug go away.

Bizarrely, changing mod inner; to mod inner {} makes the bug go away too!

In my actual project I'm using #![allow(clippy::complexity)] at the crate top-level.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning with the Cargo project using src/lib.rs, src/middle.rs, and the empty src/middle/inner.rs, then run cargo clippy --all-features. Trace how the non_minimal_cfg lint handles allow attributes on modules and out-of-line mod declarations. Done means the reported configurations suppress the lint and a regression test covers the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.