Skip to content

Commit

Permalink
Auto merge of #118924 - Urgau:check-cfg-exclude-well-known-from-diag,…
Browse files Browse the repository at this point in the history
… r=petrochenkov

Exclude well known names from showing a suggestion in check-cfg

This PR adds an exclusion for well known names from showing in suggestions of check-cfg/`unexpected_cfgs`.

Follow-up to #118213 and fixes #118213 (comment).

r? `@petrochenkov`
  • Loading branch information
bors committed Jan 13, 2024
2 parents 1825374 + 29afbbd commit f1f8687
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 39 deletions.
12 changes: 10 additions & 2 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ pub(super) fn builtin(
}
}

// We don't want to suggest adding values to well known names
// since those are defined by rustc it-self. Users can still
// do it if they want, but should not encourage them.
let is_cfg_a_well_know_name =
sess.parse_sess.check_config.well_known_names.contains(&name);

let inst = if let Some((value, _value_span)) = value {
let pre = if is_from_cargo { "\\" } else { "" };
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
Expand All @@ -368,12 +374,14 @@ pub(super) fn builtin(
if let Some((value, _value_span)) = value {
db.help(format!("consider adding `{value}` as a feature in `Cargo.toml`"));
}
} else {
} else if !is_cfg_a_well_know_name {
db.help(format!("consider using a Cargo feature instead or adding `println!(\"cargo:rustc-check-cfg={inst}\");` to the top of a `build.rs`"));
}
db.note("see <https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg> for more information about checking conditional configuration");
} else {
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
if !is_cfg_a_well_know_name {
db.help(format!("to expect this configuration use `--check-cfg={inst}`"));
}
db.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration");
}
}
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,8 @@ pub struct CheckCfg {
pub exhaustive_values: bool,
/// All the expected values for a config name
pub expecteds: FxHashMap<Symbol, ExpectedValues<Symbol>>,
/// Well known names (only used for diagnostics purposes)
pub well_known_names: FxHashSet<Symbol>,
}

pub enum ExpectedValues<T> {
Expand Down Expand Up @@ -1432,9 +1434,10 @@ impl CheckCfg {
};

macro_rules! ins {
($name:expr, $values:expr) => {
($name:expr, $values:expr) => {{
self.well_known_names.insert($name);
self.expecteds.entry($name).or_insert_with($values)
};
}};
}

// Symbols are inserted in alphabetical order as much as possible.
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/compact-values.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ LL | #[cfg(target(os = "linux", pointer_width = "X"))]
| ^^^^^^^^^^^^^^^^^^^
|
= note: expected values for `target_pointer_width` are: `16`, `32`, `64`
= help: to expect this configuration use `--check-cfg=cfg(target_pointer_width, values("X"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default

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

warning: unexpected `cfg` condition name: `feature`
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/exhaustive-names-values.feature.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ LL | #[cfg(test = "value")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition value: `unk`
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/exhaustive-names-values.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ LL | #[cfg(test = "value")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: unexpected `cfg` condition value: `unk`
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/exhaustive-values.empty_cfg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | #[cfg(test = "value")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default

Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/exhaustive-values.without_names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | #[cfg(test = "value")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("value"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default

Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/no-expected-values.empty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LL | #[cfg(test = "foo")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("foo"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: 2 warnings emitted
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/no-expected-values.mixed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LL | #[cfg(test = "foo")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("foo"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: 2 warnings emitted
Expand Down
1 change: 0 additions & 1 deletion tests/ui/check-cfg/no-expected-values.simple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LL | #[cfg(test = "foo")]
| help: remove the value
|
= note: no expected value for `test`
= help: to expect this configuration use `--check-cfg=cfg(test, values("foo"))`
= note: see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration

warning: 2 warnings emitted
Expand Down
Loading

0 comments on commit f1f8687

Please sign in to comment.