Skip to content

Commit

Permalink
Rollup merge of rust-lang#100574 - Urgau:check-cfg-warn-cfg, r=petroc…
Browse files Browse the repository at this point in the history
…henkov

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`
  • Loading branch information
matthiaskrgr authored Sep 1, 2022
2 parents bfa3555 + f8af919 commit 3bbd4ce
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/lint.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ lint_builtin_unreachable_pub = unreachable `pub` {$what}
.suggestion = consider restricting its visibility
.help = or consider exporting it for use by other crates
lint_builtin_unexpected_cli_config_name = unexpected `{$name}` as condition name
.help = was set with `--cfg` but isn't in the `--check-cfg` expected names
lint_builtin_unexpected_cli_config_value = unexpected condition value `{$value}` for condition name `{$name}`
.help = was set with `--cfg` but isn't in the `--check-cfg` expected values
lint_builtin_type_alias_bounds_help = use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
lint_builtin_type_alias_where_clause = where clauses are not enforced in type aliases
Expand Down
36 changes: 36 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3173,3 +3173,39 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
}
}
}

pub use rustc_session::lint::builtin::UNEXPECTED_CFGS;

declare_lint_pass!(UnexpectedCfgs => [UNEXPECTED_CFGS]);

impl EarlyLintPass for UnexpectedCfgs {
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
let cfg = &cx.sess().parse_sess.config;
let check_cfg = &cx.sess().parse_sess.check_config;
for &(name, value) in cfg {
if let Some(names_valid) = &check_cfg.names_valid {
if !names_valid.contains(&name) {
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
diag.build(fluent::lint::builtin_unexpected_cli_config_name)
.help(fluent::lint::help)
.set_arg("name", name)
.emit();
});
}
}
if let Some(value) = value {
if let Some(values) = &check_cfg.values_valid.get(&name) {
if !values.contains(&value) {
cx.lookup(UNEXPECTED_CFGS, None::<MultiSpan>, |diag| {
diag.build(fluent::lint::builtin_unexpected_cli_config_value)
.help(fluent::lint::help)
.set_arg("name", name)
.set_arg("value", value)
.emit();
});
}
}
}
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ macro_rules! early_lint_passes {
IncompleteFeatures: IncompleteFeatures,
RedundantSemicolons: RedundantSemicolons,
UnusedDocComment: UnusedDocComment,
UnexpectedCfgs: UnexpectedCfgs,
]
);
};
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3365,7 +3365,6 @@ declare_lint_pass! {
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
DUPLICATE_MACRO_ATTRIBUTES,
SUSPICIOUS_AUTO_TRAIT_IMPLS,
UNEXPECTED_CFGS,
DEPRECATED_WHERE_CLAUSE_LOCATION,
TEST_UNSTABLE_LINT,
FFI_UNWIND_CALLS,
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/check-cfg/allow-at-crate-level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This test check that #![allow(unexpected_cfgs)] works with --cfg
//
// check-pass
// compile-flags: --cfg=unexpected --check-cfg=names() -Z unstable-options

#![allow(unexpected_cfgs)]

fn main() {}
6 changes: 5 additions & 1 deletion src/test/ui/check-cfg/invalid-cfg-value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ LL | #[cfg(feature = "rand")]
|
= note: expected values for `feature` are: full, serde

warning: 2 warnings emitted
warning: unexpected condition value `rand` for condition name `feature`
|
= help: was set with `--cfg` but isn't in the `--check-cfg` expected values

warning: 3 warnings emitted

2 changes: 1 addition & 1 deletion src/test/ui/check-cfg/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// we correctly lint on the `cfg!` macro and `cfg_attr` attribute.
//
// check-pass
// compile-flags: --check-cfg=names() --check-cfg=values(feature,"foo") --cfg feature="bar" -Z unstable-options
// compile-flags: --check-cfg=names() --check-cfg=values(feature,"foo") --cfg feature="bar" --cfg unknown_name -Z unstable-options

#[cfg(windows)]
fn do_windows_stuff() {}
Expand Down
10 changes: 9 additions & 1 deletion src/test/ui/check-cfg/mix.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ warning: unexpected `cfg` condition name
LL | #[cfg_attr(uu, test)]
| ^^

warning: unexpected `unknown_name` as condition name
|
= help: was set with `--cfg` but isn't in the `--check-cfg` expected names

warning: unexpected condition value `bar` for condition name `feature`
|
= help: was set with `--cfg` but isn't in the `--check-cfg` expected values

warning: unexpected `cfg` condition name
--> $DIR/mix.rs:35:10
|
Expand Down Expand Up @@ -170,5 +178,5 @@ LL | cfg!(all(feature = "zebra", feature = "zebra", feature = "zebra"));
|
= note: expected values for `feature` are: foo

warning: 25 warnings emitted
warning: 27 warnings emitted

0 comments on commit 3bbd4ce

Please sign in to comment.