Skip to content

Commit

Permalink
Make --check-cfg use a different lint for CLI --cfg checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Nov 2, 2023
1 parent 62270fb commit 586f8d4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
43 changes: 38 additions & 5 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2890,22 +2890,55 @@ 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;
for &(name, value) in cfg {
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 */ }
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ early_lint_methods!(
IncompleteInternalFeatures: IncompleteInternalFeatures,
RedundantSemicolons: RedundantSemicolons,
UnusedDocComment: UnusedDocComment,
UnexpectedCfgs: UnexpectedCfgs,
UnexpectedCliCfgs: UnexpectedCliCfgs,
]
]
);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/check-cfg/unexpected-cli-cfg.rs
Original file line number Diff line number Diff line change
@@ -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() {}
36 changes: 36 additions & 0 deletions tests/ui/check-cfg/unexpected-cli-cfg.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 586f8d4

Please sign in to comment.