Skip to content

Commit f23c3f4

Browse files
committed
Exclude well known names from showing a suggestion in check-cfg
1 parent 2a3e635 commit f23c3f4

11 files changed

+40
-37
lines changed

compiler/rustc_lint/src/context/diagnostics.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,42 @@ pub(super) fn builtin(
351351
}
352352
}
353353

354+
// We don't want to suggest adding values to well known names
355+
// since those are defined by rustc it-self. Users can still
356+
// do it if they want, but should not encourage them.
357+
const WELL_KNOWN_NAMES: &'static [Symbol] = &[
358+
// tidy-alphabetical-start
359+
sym::debug_assertions,
360+
sym::doc,
361+
sym::doctest,
362+
sym::miri,
363+
sym::overflow_checks,
364+
sym::panic,
365+
sym::proc_macro,
366+
sym::relocation_model,
367+
sym::sanitize,
368+
sym::sanitizer_cfi_generalize_pointers,
369+
sym::sanitizer_cfi_normalize_integers,
370+
sym::target_abi,
371+
sym::target_arch,
372+
sym::target_endian,
373+
sym::target_env,
374+
sym::target_family,
375+
sym::target_feature,
376+
sym::target_has_atomic,
377+
sym::target_has_atomic_equal_alignment,
378+
sym::target_has_atomic_load_store,
379+
sym::target_os,
380+
sym::target_pointer_width,
381+
sym::target_thread_local,
382+
sym::target_vendor,
383+
sym::test,
384+
sym::unix,
385+
sym::windows,
386+
// tidy-alphabetical-end
387+
];
388+
389+
let is_cfg_a_well_know_name = WELL_KNOWN_NAMES.contains(&name);
354390
let inst = if let Some((value, _value_span)) = value {
355391
let pre = if is_from_cargo { "\\" } else { "" };
356392
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
@@ -363,12 +399,14 @@ pub(super) fn builtin(
363399
if let Some((value, _value_span)) = value {
364400
db.help(format!("consider adding `{value}` as a feature in `Cargo.toml`"));
365401
}
366-
} else {
402+
} else if !is_cfg_a_well_know_name {
367403
db.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo:rustc-check-cfg={inst}\");` to the top of a `build.rs`"));
368404
}
369405
db.note("see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration");
370406
} else {
371-
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
407+
if !is_cfg_a_well_know_name {
408+
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
409+
}
372410
db.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration");
373411
}
374412
}

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)