rust-lang / rust-lang/rust

#![allow(unexpected_cfgs)] does not work for modules

Open
#124,735 11 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-check-cfg A-lints C-bug L-unexpected_cfgs S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

// lib.rs
mod a;
// a.rs
#![allow(unexpected_cfgs)] // for cfg(rustfmt)
#![cfg_attr(rustfmt, rustfmt::skip)]

I expected to see this happen: no warning

Instead, this happened: #![allow(unexpected_cfgs)] is ignored:

warning: unexpected `cfg` condition name: `rustfmt`
 --> src/a.rs:2:13
  |
2 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = help: expected names are: `clippy`, `debug_assertions`, `doc`, `docsrs`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, `windows`
  = help: consider using a Cargo feature instead or adding `println!("cargo::rustc-check-cfg=cfg(rustfmt)");` to the top of the `build.rs`
  = note: see <https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg> for more information about checking conditional configuration
  = note: `#[warn(unexpected_cfgs)]` on by default

Even if adding #[allow(unexpected_cfgs)] to mod a;, the result is the same.

cc @Urgau

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (e82c861d7 2024-05-04)
binary: rustc
commit-hash: e82c861d7e5ecd766cb0dab0bf622445dec999dc
commit-date: 2024-05-04
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.4

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 lib.rs and a.rs examples shown in the issue, then trace how unexpected_cfgs attributes are handled for modules. Done means that the module-level allow suppresses the warning for cfg(rustfmt), including when the allow is placed on mod a; add or update a regression test if the relevant test location is found.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.