Skip to content

Commit 37c4a77

Browse files
committed
Prefer suggesting string-literal for Cargo check-cfg lint config
1 parent 05f77d1 commit 37c4a77

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

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

+19-18
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ fn check_cfg_expected_note(
4141
note
4242
}
4343

44+
fn to_check_cfg_arg(name: Symbol, value: Option<(Symbol, Span)>, with_quotes: bool) -> String {
45+
if let Some((value, _value_span)) = value {
46+
let pre = if with_quotes { "\\" } else { "" };
47+
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
48+
} else {
49+
format!("cfg({name})")
50+
}
51+
}
52+
4453
pub(super) fn unexpected_cfg_name(
4554
sess: &Session,
4655
diag: &mut Diag<'_, ()>,
@@ -155,21 +164,17 @@ pub(super) fn unexpected_cfg_name(
155164
}
156165
}
157166

158-
let inst = if let Some((value, _value_span)) = value {
159-
let pre = if is_from_cargo { "\\" } else { "" };
160-
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
161-
} else {
162-
format!("cfg({name})")
163-
};
167+
let inst = |with_quotes| to_check_cfg_arg(name, value, with_quotes);
164168

165169
if is_from_cargo {
166170
if !is_feature_cfg {
167171
diag.help(format!("consider using a Cargo feature instead"));
168-
diag.help(format!("or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = [\"{inst}\"] }}"));
169-
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`"));
172+
diag.help(format!("or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{}'] }}", inst(/*with_quotes:*/ false)));
173+
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(/*with_quotes:*/ true)));
170174
}
171175
} else {
172-
diag.help(format!("to expect this configuration use `--check-cfg={inst}`"));
176+
let inst = inst(/*with_quotes:*/ false);
177+
diag.help(format!("to expect this configuration use `--check-cfg={inst}`",));
173178
}
174179
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");
175180
}
@@ -252,12 +257,7 @@ pub(super) fn unexpected_cfg_value(
252257
// do it if they want, but should not encourage them.
253258
let is_cfg_a_well_know_name = sess.psess.check_config.well_known_names.contains(&name);
254259

255-
let inst = if let Some((value, _value_span)) = value {
256-
let pre = if is_from_cargo { "\\" } else { "" };
257-
format!("cfg({name}, values({pre}\"{value}{pre}\"))")
258-
} else {
259-
format!("cfg({name})")
260-
};
260+
let inst = |with_quotes| to_check_cfg_arg(name, value, with_quotes);
261261

262262
if is_from_cargo {
263263
if name == sym::feature {
@@ -268,12 +268,13 @@ pub(super) fn unexpected_cfg_value(
268268
}
269269
} else if !is_cfg_a_well_know_name {
270270
diag.help(format!("consider using a Cargo feature instead"));
271-
diag.help(format!("or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = [\"{inst}\"] }}"));
272-
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={inst}\");` to the top of the `build.rs`"));
271+
diag.help(format!("or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{}'] }}", inst(/*with_quotes:*/ false)));
272+
diag.help(format!("or consider adding `println!(\"cargo::rustc-check-cfg={}\");` to the top of the `build.rs`", inst(/*with_quotes:*/ true)));
273273
}
274274
} else {
275275
if !is_cfg_a_well_know_name {
276-
diag.help(format!("to expect this configuration use `--check-cfg={inst}`"));
276+
let inst = inst(/*with_quotes:*/ false);
277+
diag.help(format!("to expect this configuration use `--check-cfg={inst}`",));
277278
}
278279
}
279280
diag.note("see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration");

tests/ui/check-cfg/cargo-feature.none.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | #[cfg(tokio_unstable)]
2929
= help: consider using a Cargo feature instead
3030
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
3131
[lints.rust]
32-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] }
32+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] }
3333
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs`
3434
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
3535

@@ -42,7 +42,7 @@ LL | #[cfg(CONFIG_NVME = "m")]
4242
= help: consider using a Cargo feature instead
4343
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
4444
[lints.rust]
45-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] }
45+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CONFIG_NVME, values("m"))'] }
4646
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs`
4747
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4848

tests/ui/check-cfg/cargo-feature.some.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | #[cfg(tokio_unstable)]
2929
= help: consider using a Cargo feature instead
3030
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
3131
[lints.rust]
32-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tokio_unstable)"] }
32+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] }
3333
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");` to the top of the `build.rs`
3434
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
3535

@@ -45,7 +45,7 @@ LL | #[cfg(CONFIG_NVME = "m")]
4545
= help: consider using a Cargo feature instead
4646
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
4747
[lints.rust]
48-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(CONFIG_NVME, values(\"m\"))"] }
48+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CONFIG_NVME, values("m"))'] }
4949
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(CONFIG_NVME, values(\"m\"))");` to the top of the `build.rs`
5050
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
5151

tests/ui/check-cfg/diagnotics.cargo.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ LL | #[cfg(no_value)]
4242
= help: consider using a Cargo feature instead
4343
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
4444
[lints.rust]
45-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value)"] }
45+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_value)'] }
4646
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value)");` to the top of the `build.rs`
4747
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
4848

@@ -55,7 +55,7 @@ LL | #[cfg(no_value = "foo")]
5555
= help: consider using a Cargo feature instead
5656
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
5757
[lints.rust]
58-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_value, values(\"foo\"))"] }
58+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_value, values("foo"))'] }
5959
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_value, values(\"foo\"))");` to the top of the `build.rs`
6060
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
6161
help: there is a config with a similar name and no value
@@ -75,7 +75,7 @@ LL | #[cfg(no_values = "bar")]
7575
= help: consider using a Cargo feature instead
7676
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
7777
[lints.rust]
78-
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(no_values, values(\"bar\"))"] }
78+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_values, values("bar"))'] }
7979
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(no_values, values(\"bar\"))");` to the top of the `build.rs`
8080
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
8181

0 commit comments

Comments
 (0)