From 586f8d4a12e2ac768fd0081b227f714f990de96f Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 2 Nov 2023 17:05:28 +0100 Subject: [PATCH] Make --check-cfg use a different lint for CLI --cfg checking --- compiler/rustc_lint/src/builtin.rs | 43 +++++++++++++++++--- compiler/rustc_lint/src/lib.rs | 2 +- compiler/rustc_lint_defs/src/builtin.rs | 1 + tests/ui/check-cfg/unexpected-cli-cfg.rs | 23 +++++++++++ tests/ui/check-cfg/unexpected-cli-cfg.stderr | 36 ++++++++++++++++ 5 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 tests/ui/check-cfg/unexpected-cli-cfg.rs create mode 100644 tests/ui/check-cfg/unexpected-cli-cfg.stderr diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 6f6150a4172f5..7b18a3e5cf799 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2890,11 +2890,41 @@ impl EarlyLintPass for SpecialModuleName { } } -pub use rustc_session::lint::builtin::UNEXPECTED_CFGS; +declare_lint! { + /// The `unexpected_cli_cfgs` lint detects unexpected conditional compilation given + /// in the command line. + /// + /// ### Example + /// + /// ```text + /// rustc --cfg 'foo' --check-cfg 'cfg()' + /// ``` + /// + /// ```rust,ignore (needs command line option) + /// fn main() {} + /// ``` + /// + /// This will produce: + /// + /// ```text + /// warning: unexpected `foo` as condition name + /// | + /// = help: was set with `--cfg` but isn't in the `--check-cfg` expected names + /// ``` + /// + /// ### Explanation + /// + /// This lint is only active when a `--check-cfg='cfg(...)'` option has been passed + /// to the compiler and triggers whenever an unknown condition name or value is used + /// on `--cfg` command line arguments. + pub UNEXPECTED_CLI_CFGS, + Allow, + "detects unexpected names and values in `--cfg`", +} -declare_lint_pass!(UnexpectedCfgs => [UNEXPECTED_CFGS]); +declare_lint_pass!(UnexpectedCliCfgs => [UNEXPECTED_CLI_CFGS]); -impl EarlyLintPass for UnexpectedCfgs { +impl EarlyLintPass for UnexpectedCliCfgs { 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; @@ -2902,10 +2932,13 @@ impl EarlyLintPass for UnexpectedCfgs { match check_cfg.expecteds.get(&name) { Some(ExpectedValues::Some(values)) if !values.contains(&value) => { let value = value.unwrap_or(kw::Empty); - cx.emit_lint(UNEXPECTED_CFGS, BuiltinUnexpectedCliConfigValue { name, value }); + cx.emit_lint( + UNEXPECTED_CLI_CFGS, + BuiltinUnexpectedCliConfigValue { name, value }, + ); } None if check_cfg.exhaustive_names => { - cx.emit_lint(UNEXPECTED_CFGS, BuiltinUnexpectedCliConfigName { name }); + cx.emit_lint(UNEXPECTED_CLI_CFGS, BuiltinUnexpectedCliConfigName { name }); } _ => { /* expected */ } } diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index d61c59af1e05f..b96ab1f620929 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -179,7 +179,7 @@ early_lint_methods!( IncompleteInternalFeatures: IncompleteInternalFeatures, RedundantSemicolons: RedundantSemicolons, UnusedDocComment: UnusedDocComment, - UnexpectedCfgs: UnexpectedCfgs, + UnexpectedCliCfgs: UnexpectedCliCfgs, ] ] ); diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 918e484b9f44d..d129e41a09d10 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -3438,6 +3438,7 @@ declare_lint_pass! { UNCONDITIONAL_PANIC, UNCONDITIONAL_RECURSION, UNDEFINED_NAKED_FUNCTION_ABI, + UNEXPECTED_CFGS, UNFULFILLED_LINT_EXPECTATIONS, UNINHABITED_STATIC, UNKNOWN_CRATE_TYPES, diff --git a/tests/ui/check-cfg/unexpected-cli-cfg.rs b/tests/ui/check-cfg/unexpected-cli-cfg.rs new file mode 100644 index 0000000000000..fd5b5585d3058 --- /dev/null +++ b/tests/ui/check-cfg/unexpected-cli-cfg.rs @@ -0,0 +1,23 @@ +// Check for unexpected configuration value in the code. +// +// check-pass +// compile-flags: -Z unstable-options +// compile-flags: --cfg foo --cfg bar --cfg feature="bar" +// compile-flags: --check-cfg=cfg(feature,values("serde","full")) +// +// error-pattern: unexpected `foo` as condition name +// error-pattern: unexpected `bar` as condition name +// error-pattern: unexpected condition value `bar` for condition name `feature` +// error-pattern: was set with `--cfg` but isn't in the `--check-cfg` expected + +#![warn(unexpected_cli_cfgs)] + +#[cfg(feature = "bar")] +//~^ WARNING unexpected `cfg` condition value +pub fn f() {} + +#[cfg(foo)] +//~^ WARNING unexpected `cfg` condition name +pub fn foo() {} + +pub fn main() {} diff --git a/tests/ui/check-cfg/unexpected-cli-cfg.stderr b/tests/ui/check-cfg/unexpected-cli-cfg.stderr new file mode 100644 index 0000000000000..8b900407bae7e --- /dev/null +++ b/tests/ui/check-cfg/unexpected-cli-cfg.stderr @@ -0,0 +1,36 @@ +warning: unexpected `cfg` condition value: `bar` + --> $DIR/unexpected-cli-cfg.rs:15:7 + | +LL | #[cfg(feature = "bar")] + | ^^^^^^^^^^^^^^^ + | + = note: expected values for `feature` are: `full`, `serde` + = note: `#[warn(unexpected_cfgs)]` on by default + +warning: unexpected `cfg` condition name: `foo` + --> $DIR/unexpected-cli-cfg.rs:19:7 + | +LL | #[cfg(foo)] + | ^^^ + | + = help: expected names are: `debug_assertions`, `doc`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `sanitize`, `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`, `unix`, `windows` + +warning: unexpected `foo` as condition name + | + = help: was set with `--cfg` but isn't in the `--check-cfg` expected names +note: the lint level is defined here + --> $DIR/unexpected-cli-cfg.rs:13:9 + | +LL | #![warn(unexpected_cli_cfgs)] + | ^^^^^^^^^^^^^^^^^^^ + +warning: unexpected `bar` 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: 5 warnings emitted +