Skip to content

Commit bc68381

Browse files
committed
Add warning against unexpected --cfg with --check-cfg
1 parent 3fdd578 commit bc68381

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
@@ -3157,3 +3157,39 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
31573157
}
31583158
}
31593159
}
3160+
3161+
pub use rustc_session::lint::builtin::UNEXPECTED_CFGS;
3162+
3163+
declare_lint_pass!(UnexpectedCfgs => [UNEXPECTED_CFGS]);
3164+
3165+
impl EarlyLintPass for UnexpectedCfgs {
3166+
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
3167+
let cfg = &cx.sess().parse_sess.config;
3168+
let check_cfg = &cx.sess().parse_sess.check_config;
3169+
for &(name, value) in cfg {
3170+
if let Some(names_valid) = &check_cfg.names_valid {
3171+
if !names_valid.contains(&name) {
3172+
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
3173+
diag.build(fluent::lint::builtin_unexpected_cli_config_name)
3174+
.help(fluent::lint::help)
3175+
.set_arg("name", name)
3176+
.emit();
3177+
});
3178+
}
3179+
}
3180+
if let Some(value) = value {
3181+
if let Some(values) = &check_cfg.values_valid.get(&name) {
3182+
if !values.contains(&value) {
3183+
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
3184+
diag.build(fluent::lint::builtin_unexpected_cli_config_value)
3185+
.help(fluent::lint::help)
3186+
.set_arg("name", name)
3187+
.set_arg("value", value)
3188+
.emit();
3189+
});
3190+
}
3191+
}
3192+
}
3193+
}
3194+
}
3195+
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ macro_rules! early_lint_passes {
140140
IncompleteFeatures: IncompleteFeatures,
141141
RedundantSemicolons: RedundantSemicolons,
142142
UnusedDocComment: UnusedDocComment,
143+
UnexpectedCfgs: UnexpectedCfgs,
143144
]
144145
);
145146
};

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 condition value `bar` for condition name `feature`
32+
|
33+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected values
34+
35+
warning: unexpected `unknown_name` as condition name
36+
|
37+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected names
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)