Skip to content

Commit 896a59d

Browse files
authored
Rollup merge of rust-lang#97266 - est31:unknown_lints_cfg_attr, r=lcnr
Make weird name lints trigger behind cfg_attr The weird name lints (`unknown_lints`, `renamed_and_removed_lints`), the lints that lint the linting, were previously not firing for lint level declarations behind `cfg_attr`, as they were only running before expansion. Now, this will give a `unknown_lints` warning: ```Rust #[cfg_attr(all(), allow(this_lint_does_not_exist))] fn foo() {} ``` Lint level declarations behind a `cfg_attr` whose condition is not applying are still ignored. So this still won't give a warning: ```Rust #[cfg_attr(any(), allow(this_lint_does_not_exist))] fn foo() {} ``` Furthermore, this PR also makes the weird name lints respect level delcarations for *them* that were hidden by `cfg_attr`, making them consistent to other lints. So this will now not issue a warning: ```Rust #[cfg_attr(all(), allow(unknown_lints))] mod foo { #[allow(does_not_exist)] fn foo() { } } ``` Fixes rust-lang#97094
2 parents 33f45b1 + 2a8b60f commit 896a59d

File tree

8 files changed

+213
-48
lines changed

8 files changed

+213
-48
lines changed

compiler/rustc_lint/src/early.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,25 @@ pub fn check_ast_node<'a>(
424424
let mut passes: Vec<_> = passes.iter().map(|p| (p)()).collect();
425425
let mut buffered = lint_buffer.unwrap_or_default();
426426

427-
if !sess.opts.debugging_opts.no_interleave_lints {
427+
if sess.opts.debugging_opts.no_interleave_lints {
428+
for (i, pass) in passes.iter_mut().enumerate() {
429+
buffered =
430+
sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
431+
early_lint_node(
432+
sess,
433+
!pre_expansion && i == 0,
434+
lint_store,
435+
registered_tools,
436+
buffered,
437+
EarlyLintPassObjects { lints: slice::from_mut(pass) },
438+
check_node,
439+
)
440+
});
441+
}
442+
} else {
428443
buffered = early_lint_node(
429444
sess,
430-
pre_expansion,
445+
!pre_expansion,
431446
lint_store,
432447
registered_tools,
433448
buffered,
@@ -446,21 +461,6 @@ pub fn check_ast_node<'a>(
446461
check_node,
447462
);
448463
}
449-
} else {
450-
for (i, pass) in passes.iter_mut().enumerate() {
451-
buffered =
452-
sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
453-
early_lint_node(
454-
sess,
455-
pre_expansion && i == 0,
456-
lint_store,
457-
registered_tools,
458-
buffered,
459-
EarlyLintPassObjects { lints: slice::from_mut(pass) },
460-
check_node,
461-
)
462-
});
463-
}
464464
}
465465

466466
// All of the buffered lints should have been emitted at this point.

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ declare_lint! {
474474
}
475475

476476
declare_lint! {
477-
/// The `unknown_lints` lint detects unrecognized lint attribute.
477+
/// The `unknown_lints` lint detects unrecognized lint attributes.
478478
///
479479
/// ### Example
480480
///

src/test/ui-fulldeps/lint-tool-test.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ warning: lint name `test_group` is deprecated and may not have an effect in the
1818
LL | #[allow(test_group)]
1919
| ^^^^^^^^^^ help: change it to: `clippy::test_group`
2020

21-
warning: unknown lint: `this_lint_does_not_exist`
22-
--> $DIR/lint-tool-test.rs:36:8
23-
|
24-
LL | #[deny(this_lint_does_not_exist)]
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^
26-
|
27-
= note: `#[warn(unknown_lints)]` on by default
28-
2921
warning: lint name `test_lint` is deprecated and may not have an effect in the future.
3022
--> $DIR/lint-tool-test.rs:9:23
3123
|
@@ -44,6 +36,14 @@ warning: lint name `test_group` is deprecated and may not have an effect in the
4436
LL | #[allow(test_group)]
4537
| ^^^^^^^^^^ help: change it to: `clippy::test_group`
4638

39+
warning: unknown lint: `this_lint_does_not_exist`
40+
--> $DIR/lint-tool-test.rs:36:8
41+
|
42+
LL | #[deny(this_lint_does_not_exist)]
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
= note: `#[warn(unknown_lints)]` on by default
46+
4747
warning: use of deprecated attribute `plugin`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
4848
--> $DIR/lint-tool-test.rs:6:1
4949
|

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
2+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:401:17
3+
|
4+
LL | mod inner { #![macro_escape] }
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= help: try an outer attribute: `#[macro_use]`
8+
9+
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
10+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:398:1
11+
|
12+
LL | #[macro_escape]
13+
| ^^^^^^^^^^^^^^^
14+
115
warning: unknown lint: `x5400`
216
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:46:9
317
|
@@ -172,20 +186,6 @@ warning: unknown lint: `x5100`
172186
LL | #[deny(x5100)] impl S { }
173187
| ^^^^^
174188

175-
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
176-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:401:17
177-
|
178-
LL | mod inner { #![macro_escape] }
179-
| ^^^^^^^^^^^^^^^^
180-
|
181-
= help: try an outer attribute: `#[macro_use]`
182-
183-
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
184-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:398:1
185-
|
186-
LL | #[macro_escape]
187-
| ^^^^^^^^^^^^^^^
188-
189189
warning: use of deprecated attribute `crate_id`: no longer used.
190190
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:85:1
191191
|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error: unknown lint: `nonex_lint_top_level`
2+
--> $DIR/issue-97094.rs:14:26
3+
|
4+
LL | #![cfg_attr(all(), allow(nonex_lint_top_level))]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-97094.rs:10:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(unknown_lints)]` implied by `#[deny(warnings)]`
13+
14+
error: lint `bare_trait_object` has been renamed to `bare_trait_objects`
15+
--> $DIR/issue-97094.rs:16:26
16+
|
17+
LL | #![cfg_attr(all(), allow(bare_trait_object))]
18+
| ^^^^^^^^^^^^^^^^^ help: use the new name: `bare_trait_objects`
19+
|
20+
= note: `#[deny(renamed_and_removed_lints)]` implied by `#[deny(warnings)]`
21+
22+
error: unknown lint: `nonex_lint_mod`
23+
--> $DIR/issue-97094.rs:19:25
24+
|
25+
LL | #[cfg_attr(all(), allow(nonex_lint_mod))]
26+
| ^^^^^^^^^^^^^^
27+
28+
error: unknown lint: `nonex_lint_mod_inner`
29+
--> $DIR/issue-97094.rs:22:30
30+
|
31+
LL | #![cfg_attr(all(), allow(nonex_lint_mod_inner))]
32+
| ^^^^^^^^^^^^^^^^^^^^
33+
34+
error: unknown lint: `nonex_lint_fn`
35+
--> $DIR/issue-97094.rs:26:25
36+
|
37+
LL | #[cfg_attr(all(), allow(nonex_lint_fn))]
38+
| ^^^^^^^^^^^^^
39+
40+
error: unknown lint: `nonex_lint_in_macro`
41+
--> $DIR/issue-97094.rs:37:29
42+
|
43+
LL | #[cfg_attr(all(), allow(nonex_lint_in_macro))]
44+
| ^^^^^^^^^^^^^^^^^^^
45+
46+
error: unknown lint: `nonex_lint_fn`
47+
--> $DIR/issue-97094.rs:56:13
48+
|
49+
LL | #[allow(nonex_lint_fn)]
50+
| ^^^^^^^^^^^^^
51+
52+
error: aborting due to 7 previous errors
53+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error: unknown lint: `nonex_lint_top_level`
2+
--> $DIR/issue-97094.rs:14:26
3+
|
4+
LL | #![cfg_attr(all(), allow(nonex_lint_top_level))]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-97094.rs:10:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(unknown_lints)]` implied by `#[deny(warnings)]`
13+
14+
error: lint `bare_trait_object` has been renamed to `bare_trait_objects`
15+
--> $DIR/issue-97094.rs:16:26
16+
|
17+
LL | #![cfg_attr(all(), allow(bare_trait_object))]
18+
| ^^^^^^^^^^^^^^^^^ help: use the new name: `bare_trait_objects`
19+
|
20+
= note: `#[deny(renamed_and_removed_lints)]` implied by `#[deny(warnings)]`
21+
22+
error: unknown lint: `nonex_lint_mod`
23+
--> $DIR/issue-97094.rs:19:25
24+
|
25+
LL | #[cfg_attr(all(), allow(nonex_lint_mod))]
26+
| ^^^^^^^^^^^^^^
27+
28+
error: unknown lint: `nonex_lint_mod_inner`
29+
--> $DIR/issue-97094.rs:22:30
30+
|
31+
LL | #![cfg_attr(all(), allow(nonex_lint_mod_inner))]
32+
| ^^^^^^^^^^^^^^^^^^^^
33+
34+
error: unknown lint: `nonex_lint_fn`
35+
--> $DIR/issue-97094.rs:26:25
36+
|
37+
LL | #[cfg_attr(all(), allow(nonex_lint_fn))]
38+
| ^^^^^^^^^^^^^
39+
40+
error: unknown lint: `nonex_lint_in_macro`
41+
--> $DIR/issue-97094.rs:37:29
42+
|
43+
LL | #[cfg_attr(all(), allow(nonex_lint_in_macro))]
44+
| ^^^^^^^^^^^^^^^^^^^
45+
46+
error: unknown lint: `nonex_lint_fn`
47+
--> $DIR/issue-97094.rs:56:13
48+
|
49+
LL | #[allow(nonex_lint_fn)]
50+
| ^^^^^^^^^^^^^
51+
52+
error: aborting due to 7 previous errors
53+

src/test/ui/lint/issue-97094.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// revisions: interleaved nointerleaved
2+
// [nointerleaved]compile-flags: -Z no-interleave-lints
3+
4+
// This test has two revisions because the logic change
5+
// needed to make this test pass had to be adjusted
6+
// for no-interleave-lints. Should the debug option
7+
// be removed one day, please don't remove this
8+
// test entirely, just remove the revision from it.
9+
10+
#![deny(warnings)]
11+
12+
// Ensure that unknown lints inside cfg-attr's are linted for
13+
14+
#![cfg_attr(all(), allow(nonex_lint_top_level))]
15+
//~^ ERROR unknown lint
16+
#![cfg_attr(all(), allow(bare_trait_object))]
17+
//~^ ERROR has been renamed
18+
19+
#[cfg_attr(all(), allow(nonex_lint_mod))]
20+
//~^ ERROR unknown lint
21+
mod baz {
22+
#![cfg_attr(all(), allow(nonex_lint_mod_inner))]
23+
//~^ ERROR unknown lint
24+
}
25+
26+
#[cfg_attr(all(), allow(nonex_lint_fn))]
27+
//~^ ERROR unknown lint
28+
pub fn main() {}
29+
30+
macro_rules! bar {
31+
($($t:tt)*) => {
32+
$($t)*
33+
};
34+
}
35+
36+
bar!(
37+
#[cfg_attr(all(), allow(nonex_lint_in_macro))]
38+
//~^ ERROR unknown lint
39+
pub fn _bar() {}
40+
);
41+
42+
// No warning for non-applying cfg
43+
#[cfg_attr(any(), allow(nonex_lint_fn))]
44+
pub fn _foo() {}
45+
46+
// Allowing unknown lints works if inside cfg_attr
47+
#[cfg_attr(all(), allow(unknown_lints))]
48+
mod bar_allowed {
49+
#[allow(nonex_lint_fn)]
50+
fn _foo() {}
51+
}
52+
53+
// ... but not if the cfg_attr doesn't evaluate
54+
#[cfg_attr(any(), allow(unknown_lints))]
55+
mod bar_not_allowed {
56+
#[allow(nonex_lint_fn)]
57+
//~^ ERROR unknown lint
58+
fn _foo() {}
59+
}

src/test/ui/lint/reasons-erroneous.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ error[E0452]: malformed lint attribute input
5858
LL | #![warn(keyword_idents, reason = "root in rubble", macro_use_extern_crate)]
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
6060

61-
warning: unknown lint: `reason`
62-
--> $DIR/reasons-erroneous.rs:50:39
63-
|
64-
LL | #![warn(missing_copy_implementations, reason)]
65-
| ^^^^^^
66-
|
67-
= note: `#[warn(unknown_lints)]` on by default
68-
6961
error[E0452]: malformed lint attribute input
7062
--> $DIR/reasons-erroneous.rs:3:58
7163
|
@@ -126,6 +118,14 @@ error[E0452]: malformed lint attribute input
126118
LL | #![warn(keyword_idents, reason = "root in rubble", macro_use_extern_crate)]
127119
| ^^^^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
128120

121+
warning: unknown lint: `reason`
122+
--> $DIR/reasons-erroneous.rs:50:39
123+
|
124+
LL | #![warn(missing_copy_implementations, reason)]
125+
| ^^^^^^
126+
|
127+
= note: `#[warn(unknown_lints)]` on by default
128+
129129
error: aborting due to 20 previous errors; 1 warning emitted
130130

131131
For more information about this error, try `rustc --explain E0452`.

0 commit comments

Comments
 (0)