Skip to content

Commit 38399d6

Browse files
authored
Rollup merge of rust-lang#100574 - Urgau:check-cfg-warn-cfg, r=petrochenkov
Add warning against unexpected --cfg with --check-cfg This PR adds a warning when an unexpected `--cfg` is specified but not in the specified list of `--check-cfg`. This is the follow-up PR I mentioned in rust-lang#99519. r? ````@petrochenkov````
2 parents 211a525 + f8af919 commit 38399d6

File tree

8 files changed

+66
-4
lines changed

8 files changed

+66
-4
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ lint_builtin_unreachable_pub = unreachable `pub` {$what}
354354
.suggestion = consider restricting its visibility
355355
.help = or consider exporting it for use by other crates
356356
357+
lint_builtin_unexpected_cli_config_name = unexpected `{$name}` as condition name
358+
.help = was set with `--cfg` but isn't in the `--check-cfg` expected names
359+
360+
lint_builtin_unexpected_cli_config_value = unexpected condition value `{$value}` for condition name `{$name}`
361+
.help = was set with `--cfg` but isn't in the `--check-cfg` expected values
362+
357363
lint_builtin_type_alias_bounds_help = use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
358364
359365
lint_builtin_type_alias_where_clause = where clauses are not enforced in type aliases

compiler/rustc_lint/src/builtin.rs

+36
Original file line numberDiff line numberDiff line change
@@ -3173,3 +3173,39 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
31733173
}
31743174
}
31753175
}
3176+
3177+
pub use rustc_session::lint::builtin::UNEXPECTED_CFGS;
3178+
3179+
declare_lint_pass!(UnexpectedCfgs => [UNEXPECTED_CFGS]);
3180+
3181+
impl EarlyLintPass for UnexpectedCfgs {
3182+
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
3183+
let cfg = &cx.sess().parse_sess.config;
3184+
let check_cfg = &cx.sess().parse_sess.check_config;
3185+
for &(name, value) in cfg {
3186+
if let Some(names_valid) = &check_cfg.names_valid {
3187+
if !names_valid.contains(&name) {
3188+
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
3189+
diag.build(fluent::lint::builtin_unexpected_cli_config_name)
3190+
.help(fluent::lint::help)
3191+
.set_arg("name", name)
3192+
.emit();
3193+
});
3194+
}
3195+
}
3196+
if let Some(value) = value {
3197+
if let Some(values) = &check_cfg.values_valid.get(&name) {
3198+
if !values.contains(&value) {
3199+
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
3200+
diag.build(fluent::lint::builtin_unexpected_cli_config_value)
3201+
.help(fluent::lint::help)
3202+
.set_arg("name", name)
3203+
.set_arg("value", value)
3204+
.emit();
3205+
});
3206+
}
3207+
}
3208+
}
3209+
}
3210+
}
3211+
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ macro_rules! early_lint_passes {
141141
IncompleteFeatures: IncompleteFeatures,
142142
RedundantSemicolons: RedundantSemicolons,
143143
UnusedDocComment: UnusedDocComment,
144+
UnexpectedCfgs: UnexpectedCfgs,
144145
]
145146
);
146147
};

compiler/rustc_lint_defs/src/builtin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3365,7 +3365,6 @@ declare_lint_pass! {
33653365
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
33663366
DUPLICATE_MACRO_ATTRIBUTES,
33673367
SUSPICIOUS_AUTO_TRAIT_IMPLS,
3368-
UNEXPECTED_CFGS,
33693368
DEPRECATED_WHERE_CLAUSE_LOCATION,
33703369
TEST_UNSTABLE_LINT,
33713370
FFI_UNWIND_CALLS,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This test check that #![allow(unexpected_cfgs)] works with --cfg
2+
//
3+
// check-pass
4+
// compile-flags: --cfg=unexpected --check-cfg=names() -Z unstable-options
5+
6+
#![allow(unexpected_cfgs)]
7+
8+
fn main() {}

src/test/ui/check-cfg/invalid-cfg-value.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ LL | #[cfg(feature = "rand")]
1515
|
1616
= note: expected values for `feature` are: full, serde
1717

18-
warning: 2 warnings emitted
18+
warning: unexpected condition value `rand` for condition name `feature`
19+
|
20+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected values
21+
22+
warning: 3 warnings emitted
1923

src/test/ui/check-cfg/mix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// we correctly lint on the `cfg!` macro and `cfg_attr` attribute.
44
//
55
// check-pass
6-
// compile-flags: --check-cfg=names() --check-cfg=values(feature,"foo") --cfg feature="bar" -Z unstable-options
6+
// compile-flags: --check-cfg=names() --check-cfg=values(feature,"foo") --cfg feature="bar" --cfg unknown_name -Z unstable-options
77

88
#[cfg(windows)]
99
fn do_windows_stuff() {}

src/test/ui/check-cfg/mix.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ warning: unexpected `cfg` condition name
2828
LL | #[cfg_attr(uu, test)]
2929
| ^^
3030

31+
warning: unexpected `unknown_name` as condition name
32+
|
33+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected names
34+
35+
warning: unexpected condition value `bar` for condition name `feature`
36+
|
37+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected values
38+
3139
warning: unexpected `cfg` condition name
3240
--> $DIR/mix.rs:35:10
3341
|
@@ -170,5 +178,5 @@ LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
170178
|
171179
= note: expected values for `feature` are: foo
172180

173-
warning: 25 warnings emitted
181+
warning: 27 warnings emitted
174182

0 commit comments

Comments
 (0)