-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add allow_attribute
lint
#10481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add allow_attribute
lint
#10481
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f1474e
Add `allow_attribute` lint
blyxyas 5956896
Fix code fragment
blyxyas 1cf7218
Add ignore flag to code fragments
blyxyas 2d572d4
Replace list indexing for `.get` (fail-safe)
blyxyas d65c9a5
Extend tests + improve description + general improvement
blyxyas 4b9cb85
Rename lint
blyxyas 1cab0cb
Fix formatting, remove commented code, etc...
blyxyas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use ast::{AttrStyle, MetaItemKind}; | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; | ||
use rustc_ast as ast; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::{symbol::Ident, BytePos}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects uses of the `#[allow]` attribute and suggests to replace it with the new `#[expect]` attribute implemented by `#![feature(lint_reasons)]` ([RFC 2383](https://rust-lang.github.io/rfcs/2383-lint-reasons.html)) | ||
/// ### Why is this bad? | ||
/// Using `#[allow]` isn't bad, but `#[expect]` may be preferred as it lints if the code **doesn't** produce a warning. | ||
/// ### Example | ||
blyxyas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// ```rust,ignore | ||
blyxyas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// #[allow(unused_mut)] | ||
/// fn foo() -> usize { | ||
/// let mut a = Vec::new(); | ||
/// a.len() | ||
///} | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// # #![feature(lint_reasons)] | ||
/// #[expect(unused_mut)] | ||
/// fn foo() -> usize { | ||
/// let mut a = Vec::new(); | ||
/// a.len() | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.69.0"] | ||
pub ALLOW_ATTRIBUTE, | ||
restriction, | ||
"`#[allow]` will not trigger if a warning isn't found. `#[expect]` triggers if there are no warnings." | ||
} | ||
|
||
pub struct AllowAttribute { | ||
pub lint_reasons_active: bool, | ||
} | ||
|
||
impl_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTE]); | ||
|
||
impl LateLintPass<'_> for AllowAttribute { | ||
// Separate each crate's features. | ||
fn check_crate_post(&mut self, _: &LateContext<'_>) { | ||
self.lint_reasons_active = false; | ||
} | ||
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &ast::Attribute) { | ||
// Check inner attributes | ||
|
||
if_chain! { | ||
if let AttrStyle::Inner = attr.style; | ||
if attr.ident() | ||
.unwrap_or(Ident::with_dummy_span(sym!(empty))) // Will not trigger if doesn't have an ident. | ||
.name == sym!(feature); | ||
if let ast::AttrKind::Normal(normal) = &attr.kind; | ||
if let Some(MetaItemKind::List(list)) = normal.item.meta_kind(); | ||
if let Some(symbol) = list.get(0); | ||
if symbol.ident().unwrap().name == sym!(lint_reasons); | ||
then { | ||
self.lint_reasons_active = true; | ||
} | ||
} | ||
blyxyas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Check outer attributes | ||
|
||
if_chain! { | ||
if let AttrStyle::Outer = attr.style; | ||
if attr.ident() | ||
.unwrap_or(Ident::with_dummy_span(sym!(empty))) // Will not trigger if doesn't have an ident. | ||
.name == sym!(allow); | ||
blyxyas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if self.lint_reasons_active; | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
ALLOW_ATTRIBUTE, | ||
attr.span, | ||
"#[allow] attribute found", | ||
"replace it with", | ||
format!("#[expect{})]", snippet( | ||
cx, | ||
attr.ident().unwrap().span | ||
.with_lo( | ||
attr.ident().unwrap().span.hi() + BytePos(2) // Cut [( | ||
) | ||
.with_hi( | ||
attr.meta().unwrap().span.hi() - BytePos(2) // Cut )] | ||
) | ||
, "...")), Applicability::MachineApplicable); | ||
blyxyas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::allow_attribute)] | ||
#![feature(lint_reasons)] | ||
|
||
fn main() {} | ||
|
||
// Using clippy::needless_borrow just as a placeholder, it isn't relevant. | ||
|
||
// Should lint | ||
#[expect(dead_code)] | ||
struct T1; | ||
blyxyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
struct T2; // Should not lint | ||
#[deny(clippy::needless_borrow)] // Should not lint | ||
struct T3; | ||
#[warn(clippy::needless_borrow)] // Should not lint | ||
struct T4; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// run-rustfix | ||
#![allow(unused)] | ||
#![warn(clippy::allow_attribute)] | ||
#![feature(lint_reasons)] | ||
|
||
fn main() {} | ||
|
||
// Using clippy::needless_borrow just as a placeholder, it isn't relevant. | ||
blyxyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Should lint | ||
#[allow(dead_code)] | ||
struct T1; | ||
|
||
struct T2; // Should not lint | ||
#[deny(clippy::needless_borrow)] // Should not lint | ||
struct T3; | ||
#[warn(clippy::needless_borrow)] // Should not lint | ||
struct T4; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: #[allow] attribute found | ||
--> $DIR/allow_attribute.rs:11:1 | ||
| | ||
LL | #[allow(dead_code)] | ||
| ^^^^^^^^^^^^^^^^^^^ help: replace it with: `#[expect(dead_code)]` | ||
| | ||
= note: `-D clippy::allow-attribute` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.