Skip to content

Commit 0ab3ae8

Browse files
committed
Disable most Cargo check-cfg help in external macros
1 parent 44c166e commit 0ab3ae8

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub(super) fn unexpected_cfg_name(
8585
};
8686

8787
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
88+
let is_from_external_macro = rustc_middle::lint::in_external_macro(sess, name_span);
8889
let mut is_feature_cfg = name == sym::feature;
8990

9091
let code_sugg = if is_feature_cfg && is_from_cargo {
@@ -185,7 +186,11 @@ pub(super) fn unexpected_cfg_name(
185186
};
186187

187188
let invocation_help = if is_from_cargo {
188-
let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
189+
let sub = if !is_feature_cfg && !is_from_external_macro {
190+
Some(cargo_help_sub(sess, &inst))
191+
} else {
192+
None
193+
};
189194
lints::unexpected_cfg_name::InvocationHelp::Cargo { sub }
190195
} else {
191196
lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new(
@@ -216,7 +221,9 @@ pub(super) fn unexpected_cfg_value(
216221
.copied()
217222
.flatten()
218223
.collect();
224+
219225
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
226+
let is_from_external_macro = rustc_middle::lint::in_external_macro(sess, name_span);
220227

221228
// Show the full list if all possible values for a given name, but don't do it
222229
// for names as the possibilities could be very long
@@ -284,13 +291,13 @@ pub(super) fn unexpected_cfg_value(
284291
};
285292

286293
let invocation_help = if is_from_cargo {
287-
let help = if name == sym::feature {
294+
let help = if name == sym::feature && !is_from_external_macro {
288295
if let Some((value, _value_span)) = value {
289296
Some(lints::unexpected_cfg_value::CargoHelp::AddFeature { value })
290297
} else {
291298
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
292299
}
293-
} else if can_suggest_adding_value {
300+
} else if can_suggest_adding_value && !is_from_external_macro {
294301
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst)))
295302
} else {
296303
None

tests/ui/check-cfg/report-in-external-macros.cargo.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ LL | cfg_macro::my_lib_macro!();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `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(my_lib_cfg)'] }
12-
= help: or consider adding `println!("cargo::rustc-check-cfg=cfg(my_lib_cfg)");` to the top of the `build.rs`
138
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
149
= note: `#[warn(unexpected_cfgs)]` on by default
1510
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -31,7 +26,6 @@ LL | cfg_macro::my_lib_macro_feature!();
3126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3227
|
3328
= note: no expected values for `feature`
34-
= help: consider adding `UNEXPECTED_FEATURE` as a feature in `Cargo.toml`
3529
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
3630
= note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info)
3731

0 commit comments

Comments
 (0)