|
| 1 | +use ast::{AttrStyle, MetaItemKind}; |
| 2 | +use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; |
| 3 | +use rustc_ast as ast; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 7 | +use rustc_span::{symbol::Ident, BytePos}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// 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)) |
| 12 | + /// ### Why is this bad? |
| 13 | + /// Using `#[allow]` isn't bad, but `#[expect]` may be preferred as it lints if the code **doesn't** produce a warning. |
| 14 | + /// ### Example |
| 15 | + /// ```rust |
| 16 | + /// #[allow(unused_mut)] |
| 17 | + /// fn foo() -> usize { |
| 18 | + /// let mut a = Vec::new(); |
| 19 | + /// a.len() |
| 20 | + ///} |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust |
| 24 | + /// #[expect(unused_mut)] |
| 25 | + /// fn foo() -> usize { |
| 26 | + /// let mut a = Vec::new(); |
| 27 | + /// a.len() |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + #[clippy::version = "1.69.0"] |
| 31 | + pub ALLOW_ATTRIBUTE, |
| 32 | + restriction, |
| 33 | + "`#[allow]` will not trigger if a warning isn't found. `#[expect]` triggers if there are no warnings." |
| 34 | +} |
| 35 | + |
| 36 | +pub struct AllowAttribute { |
| 37 | + pub lint_reasons_active: bool, |
| 38 | +} |
| 39 | + |
| 40 | +impl_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTE]); |
| 41 | + |
| 42 | +impl LateLintPass<'_> for AllowAttribute { |
| 43 | + // Separate each crate's features. |
| 44 | + fn check_crate_post(&mut self, _: &LateContext<'_>) { |
| 45 | + self.lint_reasons_active = false; |
| 46 | + } |
| 47 | + fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &ast::Attribute) { |
| 48 | + // Check inner attributes |
| 49 | + |
| 50 | + if_chain! { |
| 51 | + if let AttrStyle::Inner = attr.style; |
| 52 | + if attr.ident() |
| 53 | + .unwrap_or(Ident::with_dummy_span(sym!(empty))) // Will not trigger if doesn't have an ident. |
| 54 | + .name == sym!(feature); |
| 55 | + if let ast::AttrKind::Normal(normal) = &attr.kind; |
| 56 | + if let Some(MetaItemKind::List(list)) = normal.item.meta_kind(); |
| 57 | + if list[0].ident().unwrap().name == sym!(lint_reasons); |
| 58 | + then { |
| 59 | + self.lint_reasons_active = true; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Check outer attributes |
| 64 | + |
| 65 | + if_chain! { |
| 66 | + if let AttrStyle::Outer = attr.style; |
| 67 | + if attr.ident() |
| 68 | + .unwrap_or(Ident::with_dummy_span(sym!(empty))) // Will not trigger if doesn't have an ident. |
| 69 | + .name == sym!(allow); |
| 70 | + if self.lint_reasons_active; |
| 71 | + then { |
| 72 | + span_lint_and_sugg( |
| 73 | + cx, |
| 74 | + ALLOW_ATTRIBUTE, |
| 75 | + attr.span, |
| 76 | + "#[allow] attribute found", |
| 77 | + "replace it with", |
| 78 | + format!("#[expect{})]", snippet( |
| 79 | + cx, |
| 80 | + attr.ident().unwrap().span |
| 81 | + .with_lo( |
| 82 | + attr.ident().unwrap().span.hi() + BytePos(2) // Cut [( |
| 83 | + ) |
| 84 | + .with_hi( |
| 85 | + attr.meta().unwrap().span.hi() - BytePos(2) // Cut )] |
| 86 | + ) |
| 87 | + , "...")), Applicability::MachineApplicable); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments