Skip to content

Commit 29afbbd

Browse files
committed
Exclude well known names from showing a suggestion in check-cfg
1 parent 174e73a commit 29afbbd

12 files changed

+15
-39
lines changed

compiler/rustc_lint/src/context/diagnostics.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ pub(super) fn builtin(
356356
}
357357
}
358358

359+
// We don't want to suggest adding values to well known names
360+
// since those are defined by rustc it-self. Users can still
361+
// do it if they want, but should not encourage them.
362+
let is_cfg_a_well_know_name =
363+
sess.parse_sess.check_config.well_known_names.contains(&name);
364+
359365
let inst = if let Some((value, _value_span)) = value {
360366
let pre = if is_from_cargo { "\\" } else { "" };
361367
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
@@ -368,12 +374,14 @@ pub(super) fn builtin(
368374
if let Some((value, _value_span)) = value {
369375
db.help(format!("consider adding `{value}` as a feature in `Cargo.toml`"));
370376
}
371-
} else {
377+
} else if !is_cfg_a_well_know_name {
372378
db.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo:rustc-check-cfg={inst}\");` to the top of a `build.rs`"));
373379
}
374380
db.note("see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration");
375381
} else {
376-
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
382+
if !is_cfg_a_well_know_name {
383+
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
384+
}
377385
db.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration");
378386
}
379387
}

compiler/rustc_session/src/config.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,8 @@ pub struct CheckCfg {
13791379
pub exhaustive_values: bool,
13801380
/// All the expected values for a config name
13811381
pub expecteds: FxHashMap<Symbol, ExpectedValues<Symbol>>,
1382+
/// Well known names (only used for diagnostics purposes)
1383+
pub well_known_names: FxHashSet<Symbol>,
13821384
}
13831385

13841386
pub enum ExpectedValues<T> {
@@ -1431,9 +1433,10 @@ impl CheckCfg {
14311433
};
14321434

14331435
macro_rules! ins {
1434-
($name:expr, $values:expr) => {
1436+
($name:expr, $values:expr) => {{
1437+
self.well_known_names.insert($name);
14351438
self.expecteds.entry($name).or_insert_with($values)
1436-
};
1439+
}};
14371440
}
14381441

14391442
// Symbols are inserted in alphabetical order as much as possible.

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)