Skip to content

Commit 8e6092d

Browse files
committed
Don't suggest adding the unexpected cfgs the build-script it-self
1 parent 54cdc13 commit 8e6092d

File tree

4 files changed

+109
-24
lines changed

4 files changed

+109
-24
lines changed

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ fn to_check_cfg_arg(name: Symbol, value: Option<Symbol>, quotes: EscapeQuotes) -
4242
}
4343
}
4444

45+
fn cargo_help_sub(inst: &impl Fn(EscapeQuotes) -> String) -> lints::UnexpectedCfgCargoHelp {
46+
// We don't want to suggest the `build.rs` way to expected cfgs if we are already in a
47+
// `build.rs`. We therefor do a best effort check to try to figure out if we are building a
48+
// Cargo build script.
49+
//
50+
// Ideally we would check the crate name via `TyCtxt::crate_name`, but this isn't available
51+
// in many places where cfgs are checked, so we use the next best thing, we look for the
52+
// `CARGO_CRATE_NAME` environment variable and check for the `build_script_build` value.
53+
// (This is similar to the `rustc_session::utils::was_invoked_from_cargo` function)
54+
55+
let unescaped = &inst(EscapeQuotes::No);
56+
if matches!(std::env::var("CARGO_CRATE_NAME"), Ok(crate_name) if crate_name == "build_script_build")
57+
{
58+
lints::UnexpectedCfgCargoHelp::lint_cfg(unescaped)
59+
} else {
60+
lints::UnexpectedCfgCargoHelp::lint_cfg_and_build_rs(unescaped, &inst(EscapeQuotes::Yes))
61+
}
62+
}
63+
4564
pub(super) fn unexpected_cfg_name(
4665
sess: &Session,
4766
(name, name_span): (Symbol, Span),
@@ -162,14 +181,7 @@ pub(super) fn unexpected_cfg_name(
162181
let inst = |escape_quotes| to_check_cfg_arg(name, value.map(|(v, _s)| v), escape_quotes);
163182

164183
let invocation_help = if is_from_cargo {
165-
let sub = if !is_feature_cfg {
166-
Some(lints::UnexpectedCfgCargoHelp::new(
167-
&inst(EscapeQuotes::No),
168-
&inst(EscapeQuotes::Yes),
169-
))
170-
} else {
171-
None
172-
};
184+
let sub = if !is_feature_cfg { Some(cargo_help_sub(&inst)) } else { None };
173185
lints::unexpected_cfg_name::InvocationHelp::Cargo { sub }
174186
} else {
175187
lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new(
@@ -267,10 +279,7 @@ pub(super) fn unexpected_cfg_value(
267279
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
268280
}
269281
} else if !is_cfg_a_well_know_name {
270-
Some(lints::unexpected_cfg_value::CargoHelp::Other(lints::UnexpectedCfgCargoHelp::new(
271-
&inst(EscapeQuotes::No),
272-
&inst(EscapeQuotes::Yes),
273-
)))
282+
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(&inst)))
274283
} else {
275284
None
276285
};

compiler/rustc_lint/src/lints.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -1961,21 +1961,33 @@ pub struct UnitBindingsDiag {
19611961
pub struct BuiltinNamedAsmLabel;
19621962

19631963
#[derive(Subdiagnostic)]
1964-
#[help(lint_unexpected_cfg_add_cargo_feature)]
1965-
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1966-
#[help(lint_unexpected_cfg_add_build_rs_println)]
1967-
pub struct UnexpectedCfgCargoHelp {
1968-
pub build_rs_println: String,
1969-
pub cargo_toml_lint_cfg: String,
1964+
pub enum UnexpectedCfgCargoHelp {
1965+
#[help(lint_unexpected_cfg_add_cargo_feature)]
1966+
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1967+
LintCfg { cargo_toml_lint_cfg: String },
1968+
#[help(lint_unexpected_cfg_add_cargo_feature)]
1969+
#[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)]
1970+
#[help(lint_unexpected_cfg_add_build_rs_println)]
1971+
LintCfgAndBuildRs { cargo_toml_lint_cfg: String, build_rs_println: String },
19701972
}
19711973

19721974
impl UnexpectedCfgCargoHelp {
1973-
pub fn new(unescaped: &str, escaped: &str) -> Self {
1974-
Self {
1975-
cargo_toml_lint_cfg: format!(
1976-
"\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}",
1977-
),
1978-
build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");",),
1975+
fn cargo_toml_lint_cfg(unescaped: &str) -> String {
1976+
format!(
1977+
"\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}"
1978+
)
1979+
}
1980+
1981+
pub fn lint_cfg(unescaped: &str) -> Self {
1982+
UnexpectedCfgCargoHelp::LintCfg {
1983+
cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped),
1984+
}
1985+
}
1986+
1987+
pub fn lint_cfg_and_build_rs(unescaped: &str, escaped: &str) -> Self {
1988+
UnexpectedCfgCargoHelp::LintCfgAndBuildRs {
1989+
cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped),
1990+
build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");"),
19791991
}
19801992
}
19811993
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This test checks that when we are building a build script provided
2+
// by Cargo we only suggest expecting the unexpected cfgs in the Cargo.toml.
3+
//
4+
//@ check-pass
5+
//@ no-auto-check-cfg
6+
//@ rustc-env:CARGO_CRATE_NAME=build_script_build
7+
//@ compile-flags:--check-cfg=cfg(has_bar)
8+
9+
#[cfg(has_foo)]
10+
//~^ WARNING unexpected `cfg` condition name
11+
fn foo() {}
12+
13+
#[cfg(has_foo = "yes")]
14+
//~^ WARNING unexpected `cfg` condition name
15+
fn foo() {}
16+
17+
#[cfg(has_bar = "yes")]
18+
//~^ WARNING unexpected `cfg` condition value
19+
fn has_bar() {}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
warning: unexpected `cfg` condition name: `has_foo`
2+
--> $DIR/cargo-build-script.rs:9:7
3+
|
4+
LL | #[cfg(has_foo)]
5+
| ^^^^^^^
6+
|
7+
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `has_bar`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`
8+
= help: consider using a Cargo feature instead
9+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
10+
[lints.rust]
11+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo)'] }
12+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
13+
= note: `#[warn(unexpected_cfgs)]` on by default
14+
15+
warning: unexpected `cfg` condition name: `has_foo`
16+
--> $DIR/cargo-build-script.rs:13:7
17+
|
18+
LL | #[cfg(has_foo = "yes")]
19+
| ^^^^^^^^^^^^^^^
20+
|
21+
= help: consider using a Cargo feature instead
22+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
23+
[lints.rust]
24+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_foo, values("yes"))'] }
25+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
26+
27+
warning: unexpected `cfg` condition value: `yes`
28+
--> $DIR/cargo-build-script.rs:17:7
29+
|
30+
LL | #[cfg(has_bar = "yes")]
31+
| ^^^^^^^--------
32+
| |
33+
| help: remove the value
34+
|
35+
= note: no expected value for `has_bar`
36+
= help: consider using a Cargo feature instead
37+
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
38+
[lints.rust]
39+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(has_bar, values("yes"))'] }
40+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
41+
42+
warning: 3 warnings emitted
43+

0 commit comments

Comments
 (0)