Rollup merge of #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 #97094
This commit is contained in:
Yuki Okushi 2022-05-25 07:08:42 +09:00 committed by GitHub
commit 896a59d8db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 265 additions and 100 deletions

View file

@ -424,10 +424,25 @@ pub fn check_ast_node<'a>(
let mut passes: Vec<_> = passes.iter().map(|p| (p)()).collect();
let mut buffered = lint_buffer.unwrap_or_default();
if !sess.opts.debugging_opts.no_interleave_lints {
if sess.opts.debugging_opts.no_interleave_lints {
for (i, pass) in passes.iter_mut().enumerate() {
buffered =
sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
early_lint_node(
sess,
!pre_expansion && i == 0,
lint_store,
registered_tools,
buffered,
EarlyLintPassObjects { lints: slice::from_mut(pass) },
check_node,
)
});
}
} else {
buffered = early_lint_node(
sess,
pre_expansion,
!pre_expansion,
lint_store,
registered_tools,
buffered,
@ -446,21 +461,6 @@ pub fn check_ast_node<'a>(
check_node,
);
}
} else {
for (i, pass) in passes.iter_mut().enumerate() {
buffered =
sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
early_lint_node(
sess,
pre_expansion && i == 0,
lint_store,
registered_tools,
buffered,
EarlyLintPassObjects { lints: slice::from_mut(pass) },
check_node,
)
});
}
}
// All of the buffered lints should have been emitted at this point.

View file

@ -474,7 +474,7 @@ declare_lint! {
}
declare_lint! {
/// The `unknown_lints` lint detects unrecognized lint attribute.
/// The `unknown_lints` lint detects unrecognized lint attributes.
///
/// ### Example
///

View file

@ -18,14 +18,6 @@ warning: lint name `test_group` is deprecated and may not have an effect in the
LL | #[allow(test_group)]
| ^^^^^^^^^^ help: change it to: `clippy::test_group`
warning: unknown lint: `this_lint_does_not_exist`
--> $DIR/lint-tool-test.rs:36:8
|
LL | #[deny(this_lint_does_not_exist)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unknown_lints)]` on by default
warning: lint name `test_lint` is deprecated and may not have an effect in the future.
--> $DIR/lint-tool-test.rs:9:23
|
@ -44,6 +36,14 @@ warning: lint name `test_group` is deprecated and may not have an effect in the
LL | #[allow(test_group)]
| ^^^^^^^^^^ help: change it to: `clippy::test_group`
warning: unknown lint: `this_lint_does_not_exist`
--> $DIR/lint-tool-test.rs:36:8
|
LL | #[deny(this_lint_does_not_exist)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unknown_lints)]` on by default
warning: use of deprecated attribute `plugin`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
--> $DIR/lint-tool-test.rs:6:1
|

View file

@ -1,3 +1,17 @@
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:401:17
|
LL | mod inner { #![macro_escape] }
| ^^^^^^^^^^^^^^^^
|
= help: try an outer attribute: `#[macro_use]`
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:398:1
|
LL | #[macro_escape]
| ^^^^^^^^^^^^^^^
warning: unknown lint: `x5400`
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:46:9
|
@ -172,20 +186,6 @@ warning: unknown lint: `x5100`
LL | #[deny(x5100)] impl S { }
| ^^^^^
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:401:17
|
LL | mod inner { #![macro_escape] }
| ^^^^^^^^^^^^^^^^
|
= help: try an outer attribute: `#[macro_use]`
warning: `#[macro_escape]` is a deprecated synonym for `#[macro_use]`
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:398:1
|
LL | #[macro_escape]
| ^^^^^^^^^^^^^^^
warning: use of deprecated attribute `crate_id`: no longer used.
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:85:1
|

View file

@ -0,0 +1,53 @@
error: unknown lint: `nonex_lint_top_level`
--> $DIR/issue-97094.rs:14:26
|
LL | #![cfg_attr(all(), allow(nonex_lint_top_level))]
| ^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/issue-97094.rs:10:9
|
LL | #![deny(warnings)]
| ^^^^^^^^
= note: `#[deny(unknown_lints)]` implied by `#[deny(warnings)]`
error: lint `bare_trait_object` has been renamed to `bare_trait_objects`
--> $DIR/issue-97094.rs:16:26
|
LL | #![cfg_attr(all(), allow(bare_trait_object))]
| ^^^^^^^^^^^^^^^^^ help: use the new name: `bare_trait_objects`
|
= note: `#[deny(renamed_and_removed_lints)]` implied by `#[deny(warnings)]`
error: unknown lint: `nonex_lint_mod`
--> $DIR/issue-97094.rs:19:25
|
LL | #[cfg_attr(all(), allow(nonex_lint_mod))]
| ^^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_mod_inner`
--> $DIR/issue-97094.rs:22:30
|
LL | #![cfg_attr(all(), allow(nonex_lint_mod_inner))]
| ^^^^^^^^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_fn`
--> $DIR/issue-97094.rs:26:25
|
LL | #[cfg_attr(all(), allow(nonex_lint_fn))]
| ^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_in_macro`
--> $DIR/issue-97094.rs:37:29
|
LL | #[cfg_attr(all(), allow(nonex_lint_in_macro))]
| ^^^^^^^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_fn`
--> $DIR/issue-97094.rs:56:13
|
LL | #[allow(nonex_lint_fn)]
| ^^^^^^^^^^^^^
error: aborting due to 7 previous errors

View file

@ -0,0 +1,53 @@
error: unknown lint: `nonex_lint_top_level`
--> $DIR/issue-97094.rs:14:26
|
LL | #![cfg_attr(all(), allow(nonex_lint_top_level))]
| ^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/issue-97094.rs:10:9
|
LL | #![deny(warnings)]
| ^^^^^^^^
= note: `#[deny(unknown_lints)]` implied by `#[deny(warnings)]`
error: lint `bare_trait_object` has been renamed to `bare_trait_objects`
--> $DIR/issue-97094.rs:16:26
|
LL | #![cfg_attr(all(), allow(bare_trait_object))]
| ^^^^^^^^^^^^^^^^^ help: use the new name: `bare_trait_objects`
|
= note: `#[deny(renamed_and_removed_lints)]` implied by `#[deny(warnings)]`
error: unknown lint: `nonex_lint_mod`
--> $DIR/issue-97094.rs:19:25
|
LL | #[cfg_attr(all(), allow(nonex_lint_mod))]
| ^^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_mod_inner`
--> $DIR/issue-97094.rs:22:30
|
LL | #![cfg_attr(all(), allow(nonex_lint_mod_inner))]
| ^^^^^^^^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_fn`
--> $DIR/issue-97094.rs:26:25
|
LL | #[cfg_attr(all(), allow(nonex_lint_fn))]
| ^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_in_macro`
--> $DIR/issue-97094.rs:37:29
|
LL | #[cfg_attr(all(), allow(nonex_lint_in_macro))]
| ^^^^^^^^^^^^^^^^^^^
error: unknown lint: `nonex_lint_fn`
--> $DIR/issue-97094.rs:56:13
|
LL | #[allow(nonex_lint_fn)]
| ^^^^^^^^^^^^^
error: aborting due to 7 previous errors

View file

@ -0,0 +1,59 @@
// revisions: interleaved nointerleaved
// [nointerleaved]compile-flags: -Z no-interleave-lints
// This test has two revisions because the logic change
// needed to make this test pass had to be adjusted
// for no-interleave-lints. Should the debug option
// be removed one day, please don't remove this
// test entirely, just remove the revision from it.
#![deny(warnings)]
// Ensure that unknown lints inside cfg-attr's are linted for
#![cfg_attr(all(), allow(nonex_lint_top_level))]
//~^ ERROR unknown lint
#![cfg_attr(all(), allow(bare_trait_object))]
//~^ ERROR has been renamed
#[cfg_attr(all(), allow(nonex_lint_mod))]
//~^ ERROR unknown lint
mod baz {
#![cfg_attr(all(), allow(nonex_lint_mod_inner))]
//~^ ERROR unknown lint
}
#[cfg_attr(all(), allow(nonex_lint_fn))]
//~^ ERROR unknown lint
pub fn main() {}
macro_rules! bar {
($($t:tt)*) => {
$($t)*
};
}
bar!(
#[cfg_attr(all(), allow(nonex_lint_in_macro))]
//~^ ERROR unknown lint
pub fn _bar() {}
);
// No warning for non-applying cfg
#[cfg_attr(any(), allow(nonex_lint_fn))]
pub fn _foo() {}
// Allowing unknown lints works if inside cfg_attr
#[cfg_attr(all(), allow(unknown_lints))]
mod bar_allowed {
#[allow(nonex_lint_fn)]
fn _foo() {}
}
// ... but not if the cfg_attr doesn't evaluate
#[cfg_attr(any(), allow(unknown_lints))]
mod bar_not_allowed {
#[allow(nonex_lint_fn)]
//~^ ERROR unknown lint
fn _foo() {}
}

View file

@ -58,6 +58,66 @@ error[E0452]: malformed lint attribute input
LL | #![warn(keyword_idents, reason = "root in rubble", macro_use_extern_crate)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:3:58
|
LL | #![warn(absolute_paths_not_starting_with_crate, reason = 0)]
| ^ reason must be a string literal
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:8:40
|
LL | #![warn(anonymous_parameters, reason = b"consider these, for we have condemned them")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reason must be a string literal
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:13:29
|
LL | #![warn(bare_trait_objects, reasons = "leaders to no sure land, guides their bearings lost")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:13:29
|
LL | #![warn(bare_trait_objects, reasons = "leaders to no sure land, guides their bearings lost")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:22:23
|
LL | #![warn(box_pointers, blerp = "or in league with robbers have reversed the signposts")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:22:23
|
LL | #![warn(box_pointers, blerp = "or in league with robbers have reversed the signposts")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:31:36
|
LL | #![warn(elided_lifetimes_in_paths, reason("disrespectful to ancestors", "irresponsible to heirs"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:31:36
|
LL | #![warn(elided_lifetimes_in_paths, reason("disrespectful to ancestors", "irresponsible to heirs"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:40:44
|
LL | #![warn(ellipsis_inclusive_range_patterns, reason = "born barren", reason = "a freak growth")]
| ^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:45:25
|
LL | #![warn(keyword_idents, reason = "root in rubble", macro_use_extern_crate)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
warning: unknown lint: `reason`
--> $DIR/reasons-erroneous.rs:50:39
|
@ -66,66 +126,6 @@ LL | #![warn(missing_copy_implementations, reason)]
|
= note: `#[warn(unknown_lints)]` on by default
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:3:58
|
LL | #![warn(absolute_paths_not_starting_with_crate, reason = 0)]
| ^ reason must be a string literal
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:8:40
|
LL | #![warn(anonymous_parameters, reason = b"consider these, for we have condemned them")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reason must be a string literal
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:13:29
|
LL | #![warn(bare_trait_objects, reasons = "leaders to no sure land, guides their bearings lost")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:13:29
|
LL | #![warn(bare_trait_objects, reasons = "leaders to no sure land, guides their bearings lost")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:22:23
|
LL | #![warn(box_pointers, blerp = "or in league with robbers have reversed the signposts")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:22:23
|
LL | #![warn(box_pointers, blerp = "or in league with robbers have reversed the signposts")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:31:36
|
LL | #![warn(elided_lifetimes_in_paths, reason("disrespectful to ancestors", "irresponsible to heirs"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:31:36
|
LL | #![warn(elided_lifetimes_in_paths, reason("disrespectful to ancestors", "irresponsible to heirs"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bad attribute argument
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:40:44
|
LL | #![warn(ellipsis_inclusive_range_patterns, reason = "born barren", reason = "a freak growth")]
| ^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
error[E0452]: malformed lint attribute input
--> $DIR/reasons-erroneous.rs:45:25
|
LL | #![warn(keyword_idents, reason = "root in rubble", macro_use_extern_crate)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ reason in lint attribute must come last
error: aborting due to 20 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0452`.