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

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

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1379,6 +1379,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 @@ -1431,9 +1433,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
Loading