Skip to content

Commit 47d1cdb

Browse files
committed
Auto 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 06b72b0 + eccdccf commit 47d1cdb

File tree

10 files changed

+70
-8
lines changed

10 files changed

+70
-8
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
@@ -3250,3 +3250,39 @@ impl EarlyLintPass for SpecialModuleName {
32503250
}
32513251
}
32523252
}
3253+
3254+
pub use rustc_session::lint::builtin::UNEXPECTED_CFGS;
3255+
3256+
declare_lint_pass!(UnexpectedCfgs => [UNEXPECTED_CFGS]);
3257+
3258+
impl EarlyLintPass for UnexpectedCfgs {
3259+
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
3260+
let cfg = &cx.sess().parse_sess.config;
3261+
let check_cfg = &cx.sess().parse_sess.check_config;
3262+
for &(name, value) in cfg {
3263+
if let Some(names_valid) = &check_cfg.names_valid {
3264+
if !names_valid.contains(&name) {
3265+
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
3266+
diag.build(fluent::lint::builtin_unexpected_cli_config_name)
3267+
.help(fluent::lint::help)
3268+
.set_arg("name", name)
3269+
.emit();
3270+
});
3271+
}
3272+
}
3273+
if let Some(value) = value {
3274+
if let Some(values) = &check_cfg.values_valid.get(&name) {
3275+
if !values.contains(&value) {
3276+
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
3277+
diag.build(fluent::lint::builtin_unexpected_cli_config_value)
3278+
.help(fluent::lint::help)
3279+
.set_arg("name", name)
3280+
.set_arg("value", value)
3281+
.emit();
3282+
});
3283+
}
3284+
}
3285+
}
3286+
}
3287+
}
3288+
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ macro_rules! early_lint_passes {
146146
IncompleteFeatures: IncompleteFeatures,
147147
RedundantSemicolons: RedundantSemicolons,
148148
UnusedDocComment: UnusedDocComment,
149+
UnexpectedCfgs: UnexpectedCfgs,
149150
]
150151
);
151152
};

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,

compiler/rustc_session/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
901901
sess.fatal(&err);
902902
});
903903

904-
let mut ret = FxHashSet::default();
904+
let mut ret = CrateConfig::default();
905905
ret.reserve(7); // the minimum number of insertions
906906
// Target bindings.
907907
ret.insert((sym::target_os, Some(Symbol::intern(os))));

compiler/rustc_session/src/parse.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::lint::{
88
};
99
use crate::SessionDiagnostic;
1010
use rustc_ast::node_id::NodeId;
11-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
11+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
1212
use rustc_data_structures::sync::{Lock, Lrc};
1313
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
1414
use rustc_errors::{
@@ -25,7 +25,7 @@ use std::str;
2525

2626
/// The set of keys (and, optionally, values) that define the compilation
2727
/// environment of the crate, used to drive conditional compilation.
28-
pub type CrateConfig = FxHashSet<(Symbol, Option<Symbol>)>;
28+
pub type CrateConfig = FxIndexSet<(Symbol, Option<Symbol>)>;
2929
pub type CrateCheckConfig = CheckCfg<Symbol>;
3030

3131
/// Collected spans during parsing for places where a certain feature was
@@ -241,7 +241,7 @@ impl ParseSess {
241241
Self {
242242
span_diagnostic: handler,
243243
unstable_features: UnstableFeatures::from_environment(None),
244-
config: FxHashSet::default(),
244+
config: FxIndexSet::default(),
245245
check_config: CrateCheckConfig::default(),
246246
edition: ExpnId::root().expn_data().edition,
247247
raw_identifier_spans: Lock::new(Vec::new()),
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)