rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
93.58k stars 12.05k forks source link

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

Open taiki-e opened 1 week ago

taiki-e commented 1 week ago

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
Urgau commented 1 week ago

I would also expect the #[allow] to work here.

However, I don't think you need the cfg_attr anymore, it can be removed safely.

- #![cfg_attr(rustfmt, rustfmt::skip)]
+ #![rustfmt::skip]
taiki-e commented 1 week ago

it can be removed safely.

No. Custom inner attributes are unstable.

error[E0658]: custom inner attributes are unstable
 --> src/lib.rs:1:4
  |
1 | #![rustfmt::skip]
  |    ^^^^^^^^^^^^^
  |
  = note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
  = help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
  = note: this compiler was built on 2024-05-04; consider upgrading it if it is out of date

playground

Urgau commented 1 week ago

Oh, good point, but they can (stably) be applied directly to the mod, like this:

#[rustfmt::skip]
mod a;
Urgau commented 1 week ago

Another thing that isn't right here is that the rustfmt cfg is not a well known cfg.

I have never been able to find where that cfg might be set by rustfmt so I (wrongly) assumed it was anymore, looks this I was wrong.

@rustbot label -needs-triage +F-check-cfg @rustbot claim

taiki-e commented 1 week ago

they can (stably) be applied directly to the mod, like this:

#[rustfmt::skip]
mod a;

Unfortunately, that is not an option available to me due to a rust-analyzer bug: https://github.com/rust-lang/rust-analyzer/issues/10826#issuecomment-1647890621

lukas-code commented 1 week ago

I have never been able to find where that cfg might be set by rustfmt so I (wrongly) assumed it was anymore, looks this I was wrong.

It looks like rustfmt will just always treat cfg_attr(anything, rustfmt::skip) as rustfmt::skip, regardless of the actual cfg condition, so this will also work: (playgound)

mod m {
    #![cfg_attr(any(), rustfmt::skip)]

    fn dont_format_me ()  {   }
}
fmease commented 1 week ago

It looks like rustfmt will just always treat cfg_attr(anything, rustfmt::skip) as rustfmt::skip, regardless of the actual cfg condition

Oh, no! >.<

Urgau commented 1 week ago

The rustfmt cfg is now (as of nighlty-2024-05-07) recognized has a well known cfg. Fixing one of the issue reported by this issue.

pnkfelix commented 1 week ago

cc #82450