Skip to content

Commit e4cb25d

Browse files
committed
Report unexpected_cfgs lint in external macros
1 parent db034ce commit e4cb25d

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,7 @@ declare_lint! {
31843184
pub UNEXPECTED_CFGS,
31853185
Warn,
31863186
"detects unexpected names and values in `#[cfg]` conditions",
3187+
report_in_external_macro
31873188
}
31883189

31893190
declare_lint! {
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Inspired by https://github.com/rust-lang/cargo/issues/14775
2+
3+
pub fn my_lib_func() {}
4+
5+
#[macro_export]
6+
macro_rules! my_lib_macro {
7+
() => {
8+
#[cfg(my_lib_cfg)]
9+
$crate::my_lib_func()
10+
};
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This test checks that we emit the `unexpected_cfgs` lint even in code
2+
// coming from an external macro.
3+
4+
//@ check-pass
5+
//@ no-auto-check-cfg
6+
//@ aux-crate: cfg_macro=cfg_macro.rs
7+
//@ compile-flags: --check-cfg=cfg()
8+
9+
fn main() {
10+
cfg_macro::my_lib_macro!();
11+
//~^ WARNING unexpected `cfg` condition name
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: unexpected `cfg` condition name: `my_lib_cfg`
2+
--> $DIR/report-in-external-macros.rs:10:5
3+
|
4+
LL | cfg_macro::my_lib_macro!();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `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`, `ub_checks`, `unix`, and `windows`
8+
= help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)`
9+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
13+
warning: 1 warning emitted
14+

0 commit comments

Comments
 (0)