Skip to content

Commit 8f6f8aa

Browse files
committed
Make --check-cfg use a different lint for CLI --cfg checking
1 parent 62270fb commit 8f6f8aa

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

compiler/rustc_lint/src/builtin.rs

+34-5
Original file line numberDiff line numberDiff line change
@@ -2890,22 +2890,51 @@ impl EarlyLintPass for SpecialModuleName {
28902890
}
28912891
}
28922892

2893-
pub use rustc_session::lint::builtin::UNEXPECTED_CFGS;
2893+
declare_lint! {
2894+
/// The `unexpected_cli_cfgs` lint detects unexpected conditional compilation given
2895+
/// in the command line.
2896+
///
2897+
/// ### Example
2898+
///
2899+
/// ```text
2900+
/// rustc --cfg 'foo' --check-cfg 'cfg()'
2901+
/// ```
2902+
///
2903+
/// This will produce:
2904+
///
2905+
/// ```text
2906+
/// warning: unexpected `foo` as condition name
2907+
/// |
2908+
/// = help: was set with `--cfg` but isn't in the `--check-cfg` expected names
2909+
/// ```
2910+
///
2911+
/// ### Explanation
2912+
///
2913+
/// This lint is only active when a `--check-cfg='cfg(...)'` option has been passed
2914+
/// to the compiler and triggers whenever an unknown condition name or value is used
2915+
/// on `--cfg` command line arguments.
2916+
pub UNEXPECTED_CLI_CFGS,
2917+
Allow,
2918+
"detects unexpected names and values in `--cfg`",
2919+
}
28942920

2895-
declare_lint_pass!(UnexpectedCfgs => [UNEXPECTED_CFGS]);
2921+
declare_lint_pass!(UnexpectedCliCfgs => [UNEXPECTED_CLI_CFGS]);
28962922

2897-
impl EarlyLintPass for UnexpectedCfgs {
2923+
impl EarlyLintPass for UnexpectedCliCfgs {
28982924
fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
28992925
let cfg = &cx.sess().parse_sess.config;
29002926
let check_cfg = &cx.sess().parse_sess.check_config;
29012927
for &(name, value) in cfg {
29022928
match check_cfg.expecteds.get(&name) {
29032929
Some(ExpectedValues::Some(values)) if !values.contains(&value) => {
29042930
let value = value.unwrap_or(kw::Empty);
2905-
cx.emit_lint(UNEXPECTED_CFGS, BuiltinUnexpectedCliConfigValue { name, value });
2931+
cx.emit_lint(
2932+
UNEXPECTED_CLI_CFGS,
2933+
BuiltinUnexpectedCliConfigValue { name, value },
2934+
);
29062935
}
29072936
None if check_cfg.exhaustive_names => {
2908-
cx.emit_lint(UNEXPECTED_CFGS, BuiltinUnexpectedCliConfigName { name });
2937+
cx.emit_lint(UNEXPECTED_CLI_CFGS, BuiltinUnexpectedCliConfigName { name });
29092938
}
29102939
_ => { /* expected */ }
29112940
}

compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ early_lint_methods!(
179179
IncompleteInternalFeatures: IncompleteInternalFeatures,
180180
RedundantSemicolons: RedundantSemicolons,
181181
UnusedDocComment: UnusedDocComment,
182-
UnexpectedCfgs: UnexpectedCfgs,
182+
UnexpectedCliCfgs: UnexpectedCliCfgs,
183183
]
184184
]
185185
);

compiler/rustc_lint_defs/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3438,6 +3438,7 @@ declare_lint_pass! {
34383438
UNCONDITIONAL_PANIC,
34393439
UNCONDITIONAL_RECURSION,
34403440
UNDEFINED_NAKED_FUNCTION_ABI,
3441+
UNEXPECTED_CFGS,
34413442
UNFULFILLED_LINT_EXPECTATIONS,
34423443
UNINHABITED_STATIC,
34433444
UNKNOWN_CRATE_TYPES,
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Check for unexpected configuration value in the code.
2+
//
3+
// check-pass
4+
// compile-flags: -Z unstable-options
5+
// compile-flags: --cfg foo --cfg bar --cfg feature="bar"
6+
// compile-flags: --check-cfg=cfg(feature,values("serde","full"))
7+
//
8+
// error-pattern: unexpected `foo` as condition name
9+
// error-pattern: unexpected `bar` as condition name
10+
// error-pattern: unexpected condition value `bar` for condition name `feature`
11+
// error-pattern: was set with `--cfg` but isn't in the `--check-cfg` expected
12+
13+
#![warn(unexpected_cli_cfgs)]
14+
15+
#[cfg(feature = "bar")]
16+
//~^ WARNING unexpected `cfg` condition value
17+
pub fn f() {}
18+
19+
#[cfg(foo)]
20+
//~^ WARNING unexpected `cfg` condition name
21+
pub fn foo() {}
22+
23+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
warning: unexpected `cfg` condition value: `bar`
2+
--> $DIR/unexpected-cli-cfg.rs:15:7
3+
|
4+
LL | #[cfg(feature = "bar")]
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: expected values for `feature` are: `full`, `serde`
8+
= note: `#[warn(unexpected_cfgs)]` on by default
9+
10+
warning: unexpected `cfg` condition name: `foo`
11+
--> $DIR/unexpected-cli-cfg.rs:19:7
12+
|
13+
LL | #[cfg(foo)]
14+
| ^^^
15+
|
16+
= 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`
17+
18+
warning: unexpected `foo` as condition name
19+
|
20+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected names
21+
note: the lint level is defined here
22+
--> $DIR/unexpected-cli-cfg.rs:13:9
23+
|
24+
LL | #![warn(unexpected_cli_cfgs)]
25+
| ^^^^^^^^^^^^^^^^^^^
26+
27+
warning: unexpected `bar` as condition name
28+
|
29+
= help: was set with `--cfg` but isn't in the `--check-cfg` expected names
30+
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: 5 warnings emitted
36+

0 commit comments

Comments
 (0)