Skip to content

Commit 0883bfb

Browse files
committed
Exclude well known names from showing a suggestion in check-cfg
1 parent a90372c commit 0883bfb

11 files changed

+40
-36
lines changed

compiler/rustc_lint/src/context.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,42 @@ pub trait LintContext {
821821
}
822822
}
823823

824+
// We don't want to suggest adding values to well known names
825+
// since those are defined by rustc it-self. Users can still
826+
// do it if they want, but should not encourage them.
827+
const WELL_KNOWN_NAMES: &'static [Symbol] = &[
828+
// tidy-alphabetical-start
829+
sym::debug_assertions,
830+
sym::doc,
831+
sym::doctest,
832+
sym::miri,
833+
sym::overflow_checks,
834+
sym::panic,
835+
sym::proc_macro,
836+
sym::relocation_model,
837+
sym::sanitize,
838+
sym::sanitizer_cfi_generalize_pointers,
839+
sym::sanitizer_cfi_normalize_integers,
840+
sym::target_abi,
841+
sym::target_arch,
842+
sym::target_endian,
843+
sym::target_env,
844+
sym::target_family,
845+
sym::target_feature,
846+
sym::target_has_atomic,
847+
sym::target_has_atomic_equal_alignment,
848+
sym::target_has_atomic_load_store,
849+
sym::target_os,
850+
sym::target_pointer_width,
851+
sym::target_thread_local,
852+
sym::target_vendor,
853+
sym::test,
854+
sym::unix,
855+
sym::windows,
856+
// tidy-alphabetical-end
857+
];
858+
859+
let is_cfg_a_well_know_name = WELL_KNOWN_NAMES.contains(&name);
824860
let inst = if let Some((value, _value_span)) = value {
825861
let pre = if is_from_cargo { "\\" } else { "" };
826862
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
@@ -833,12 +869,14 @@ pub trait LintContext {
833869
if let Some((value, _value_span)) = value {
834870
db.help(format!("consider adding `{value}` as a feature in `Cargo.toml`"));
835871
}
836-
} else {
872+
} else if !is_cfg_a_well_know_name {
837873
db.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo:rustc-check-cfg={inst}\");` to the top of a `build.rs`"));
838874
}
839875
db.note("see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration");
840876
} else {
841-
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
877+
if !is_cfg_a_well_know_name {
878+
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
879+
}
842880
db.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration");
843881
}
844882
},

tests/ui/check-cfg/compact-values.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | #[cfg(target(os = "linux", pointer_width = "X"))]
55
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: expected values for `target_pointer_width` are: `16`, `32`, `64`
8-
= help: to expect this configuration use `--check-cfg=cfg(target_pointer_width, values("X"))`
98
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
109
= note: `#[warn(unexpected_cfgs)]` on by default
1110

tests/ui/check-cfg/exhaustive-names-values.empty_cfg.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ LL | #[cfg(test = "value")]
1818
| help: remove the value
1919
|
2020
= note: no expected value for `test`
21-
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
2221
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2322

2423
warning: unexpected `cfg` condition name: `feature`

tests/ui/check-cfg/exhaustive-names-values.feature.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ LL | #[cfg(test = "value")]
1818
| help: remove the value
1919
|
2020
= note: no expected value for `test`
21-
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
2221
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2322

2423
warning: unexpected `cfg` condition value: `unk`

tests/ui/check-cfg/exhaustive-names-values.full.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ LL | #[cfg(test = "value")]
1818
| help: remove the value
1919
|
2020
= note: no expected value for `test`
21-
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
2221
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2322

2423
warning: unexpected `cfg` condition value: `unk`

tests/ui/check-cfg/exhaustive-values.empty_cfg.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | #[cfg(test = "value")]
77
| help: remove the value
88
|
99
= note: no expected value for `test`
10-
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
1110
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
1211
= note: `#[warn(unexpected_cfgs)]` on by default
1312

tests/ui/check-cfg/exhaustive-values.without_names.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | #[cfg(test = "value")]
77
| help: remove the value
88
|
99
= note: no expected value for `test`
10-
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
1110
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
1211
= note: `#[warn(unexpected_cfgs)]` on by default
1312

tests/ui/check-cfg/no-expected-values.empty.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LL | #[cfg(test = "foo")]
2020
| help: remove the value
2121
|
2222
= note: no expected value for `test`
23-
= help: to expect this configuration use `--check-cfg=cfg(test, values("foo"))`
2423
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2524

2625
warning: 2 warnings emitted

tests/ui/check-cfg/no-expected-values.mixed.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LL | #[cfg(test = "foo")]
2020
| help: remove the value
2121
|
2222
= note: no expected value for `test`
23-
= help: to expect this configuration use `--check-cfg=cfg(test, values("foo"))`
2423
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2524

2625
warning: 2 warnings emitted

tests/ui/check-cfg/no-expected-values.simple.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LL | #[cfg(test = "foo")]
2020
| help: remove the value
2121
|
2222
= note: no expected value for `test`
23-
= help: to expect this configuration use `--check-cfg=cfg(test, values("foo"))`
2423
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
2524

2625
warning: 2 warnings emitted

0 commit comments

Comments
 (0)