Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
taiki-e opened this issue May 5, 2024 · 9 comments
Open

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

taiki-e opened this issue May 5, 2024 · 9 comments
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-check-cfg --check-cfg L-unexpected_cfgs Lint: unexpected_cfgs S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@taiki-e
Copy link
Member

taiki-e commented May 5, 2024

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
@taiki-e taiki-e added the C-bug Category: This is a bug. label May 5, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 5, 2024
taiki-e added a commit to taiki-e/assert-unmoved that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/assert-unmoved that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
@Urgau
Copy link
Member

Urgau commented May 5, 2024

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
Copy link
Member Author

taiki-e commented May 5, 2024

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
Copy link
Member

Urgau commented May 5, 2024

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

#[rustfmt::skip]
mod a;

@Urgau
Copy link
Member

Urgau commented May 5, 2024

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

@rustbot rustbot added F-check-cfg --check-cfg and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 5, 2024
@taiki-e
Copy link
Member Author

taiki-e commented May 5, 2024

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: rust-lang/rust-analyzer#10826 (comment)

taiki-e added a commit to taiki-e/cargo-config2 that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/is_none.rs:6:13
  |
6 | #![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: `-D unexpected-cfgs` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(unexpected_cfgs)]`

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/de.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/dependabot-config that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/display.rs:6:13
  |
6 | #![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: `-D unexpected-cfgs` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(unexpected_cfgs)]`

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/from_str.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![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: `-D unexpected-cfgs` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(unexpected_cfgs)]`

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/display.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/target-spec-json that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/easytime that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
@lukas-code
Copy link
Member

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 ()  {   }
}

taiki-e added a commit to taiki-e/find-crate that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/futures-async-stream that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/parse-changelog that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/assert_impl.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/syn-serde that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/ast_struct.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/ast_enum.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/convert.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
taiki-e added a commit to taiki-e/syn-serde that referenced this issue May 5, 2024
```
error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/ast_struct.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/ast_enum.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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

error: unexpected `cfg` condition name: `rustfmt`
 --> src/gen/convert.rs:6:13
  |
6 | #![cfg_attr(rustfmt, rustfmt::skip)]
  |             ^^^^^^^
  |
  = 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
```

Due to rust-lang/rust#124735, we cannot
disable this lint at module-level.
fmease added a commit to fmease/rust that referenced this issue May 5, 2024
Add `rustfmt` cfg to well known cfgs list

This PR adds the `rustfmt` cfg to the well known cfgs list.

Related to rust-lang#124735
@fmease
Copy link
Member

fmease commented May 5, 2024

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

Oh, no! >.<

fmease added a commit to fmease/rust that referenced this issue May 5, 2024
Add `rustfmt` cfg to well known cfgs list

This PR adds the `rustfmt` cfg to the well known cfgs list.

Related to rust-lang#124735
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 6, 2024
Add `rustfmt` cfg to well known cfgs list

This PR adds the `rustfmt` cfg to the well known cfgs list.

Related to rust-lang#124735
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 6, 2024
Add `rustfmt` cfg to well known cfgs list

This PR adds the `rustfmt` cfg to the well known cfgs list.

Related to rust-lang#124735
MingweiSamuel added a commit to MingweiSamuel/Riven that referenced this issue May 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 6, 2024
Rollup merge of rust-lang#124742 - Urgau:check-cfg-rustfmt, r=fmease

Add `rustfmt` cfg to well known cfgs list

This PR adds the `rustfmt` cfg to the well known cfgs list.

Related to rust-lang#124735
@Urgau
Copy link
Member

Urgau commented May 7, 2024

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
Copy link
Member

pnkfelix commented May 7, 2024

cc #82450

@jieyouxu jieyouxu added L-unexpected_cfgs Lint: unexpected_cfgs A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-check-cfg --check-cfg L-unexpected_cfgs Lint: unexpected_cfgs S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants