From 0f1474ea270fb3b5f7c0db295b2817028acdb97a Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 11 Mar 2023 00:23:58 +0100 Subject: [PATCH 1/7] Add `allow_attribute` lint --- CHANGELOG.md | 1 + clippy_lints/src/allow_attribute.rs | 91 +++++++++++++++++++++++++++++ clippy_lints/src/declared_lints.rs | 1 + clippy_lints/src/lib.rs | 6 ++ tests/ui/allow_attribute.fixed | 18 ++++++ tests/ui/allow_attribute.rs | 18 ++++++ tests/ui/allow_attribute.stderr | 10 ++++ 7 files changed, 145 insertions(+) create mode 100644 clippy_lints/src/allow_attribute.rs create mode 100644 tests/ui/allow_attribute.fixed create mode 100644 tests/ui/allow_attribute.rs create mode 100644 tests/ui/allow_attribute.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index da7042f4440..2b00fd962ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4382,6 +4382,7 @@ Released 2018-09-13 [`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons [`alloc_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#alloc_instead_of_core +[`allow_attribute`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attribute [`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason [`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range [`almost_complete_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range diff --git a/clippy_lints/src/allow_attribute.rs b/clippy_lints/src/allow_attribute.rs new file mode 100644 index 00000000000..2970d467f6f --- /dev/null +++ b/clippy_lints/src/allow_attribute.rs @@ -0,0 +1,91 @@ +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 + /// ```rust + /// #[allow(unused_mut)] + /// fn foo() -> usize { + /// let mut a = Vec::new(); + /// a.len() + ///} + /// ``` + /// Use instead: + /// ```rust + /// #[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 list[0].ident().unwrap().name == sym!(lint_reasons); + then { + self.lint_reasons_active = true; + } + } + + // 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); + 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); + } + } + } +} diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index cc6024b87cd..73f74d59f4a 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -35,6 +35,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO, #[cfg(feature = "internal")] crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO, + crate::allow_attribute::ALLOW_ATTRIBUTE_INFO, crate::almost_complete_range::ALMOST_COMPLETE_RANGE_INFO, crate::approx_const::APPROX_CONSTANT_INFO, crate::as_conversions::AS_CONVERSIONS_INFO, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 491732be208..51da5d1ba49 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -67,6 +67,7 @@ mod declared_lints; mod renamed_lints; // begin lints modules, do not remove this comment, it’s used in `update_lints` +mod allow_attribute; mod almost_complete_range; mod approx_const; mod as_conversions; @@ -933,6 +934,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(missing_assert_message::MissingAssertMessage)); store.register_early_pass(|| Box::new(redundant_async_block::RedundantAsyncBlock)); store.register_late_pass(|_| Box::new(let_with_type_underscore::UnderscoreTyped)); + store.register_late_pass(|_| { + Box::new(allow_attribute::AllowAttribute { + lint_reasons_active: false, + }) + }); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/tests/ui/allow_attribute.fixed b/tests/ui/allow_attribute.fixed new file mode 100644 index 00000000000..5f445a0bd96 --- /dev/null +++ b/tests/ui/allow_attribute.fixed @@ -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; + +struct T2; // Should not lint +#[deny(clippy::needless_borrow)] // Should not lint +struct T3; +#[warn(clippy::needless_borrow)] // Should not lint +struct T4; diff --git a/tests/ui/allow_attribute.rs b/tests/ui/allow_attribute.rs new file mode 100644 index 00000000000..36d2e3e27ab --- /dev/null +++ b/tests/ui/allow_attribute.rs @@ -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 +#[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; diff --git a/tests/ui/allow_attribute.stderr b/tests/ui/allow_attribute.stderr new file mode 100644 index 00000000000..6f90661577c --- /dev/null +++ b/tests/ui/allow_attribute.stderr @@ -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 + From 59568962aed2c4aff72360e4fbd3b9c04371dbec Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 11 Mar 2023 00:48:50 +0100 Subject: [PATCH 2/7] Fix code fragment --- clippy_lints/src/allow_attribute.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/clippy_lints/src/allow_attribute.rs b/clippy_lints/src/allow_attribute.rs index 2970d467f6f..805c804119a 100644 --- a/clippy_lints/src/allow_attribute.rs +++ b/clippy_lints/src/allow_attribute.rs @@ -21,6 +21,7 @@ declare_clippy_lint! { /// ``` /// Use instead: /// ```rust + /// # #![feature(lint_reasons)] /// #[expect(unused_mut)] /// fn foo() -> usize { /// let mut a = Vec::new(); From 1cf72183cfe0a29708130f59c79176128b06fcae Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 11 Mar 2023 01:08:03 +0100 Subject: [PATCH 3/7] Add ignore flag to code fragments --- clippy_lints/src/allow_attribute.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/allow_attribute.rs b/clippy_lints/src/allow_attribute.rs index 805c804119a..2258b973307 100644 --- a/clippy_lints/src/allow_attribute.rs +++ b/clippy_lints/src/allow_attribute.rs @@ -12,7 +12,7 @@ declare_clippy_lint! { /// ### 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 - /// ```rust + /// ```rust,ignore /// #[allow(unused_mut)] /// fn foo() -> usize { /// let mut a = Vec::new(); @@ -20,7 +20,7 @@ declare_clippy_lint! { ///} /// ``` /// Use instead: - /// ```rust + /// ```rust,ignore /// # #![feature(lint_reasons)] /// #[expect(unused_mut)] /// fn foo() -> usize { From 2d572d4a9c20a11a3aa75a7fdd9168cc95c60d4c Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 11 Mar 2023 13:11:27 +0100 Subject: [PATCH 4/7] Replace list indexing for `.get` (fail-safe) --- clippy_lints/src/allow_attribute.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/allow_attribute.rs b/clippy_lints/src/allow_attribute.rs index 2258b973307..defd14da9ef 100644 --- a/clippy_lints/src/allow_attribute.rs +++ b/clippy_lints/src/allow_attribute.rs @@ -55,7 +55,8 @@ impl LateLintPass<'_> for AllowAttribute { .name == sym!(feature); if let ast::AttrKind::Normal(normal) = &attr.kind; if let Some(MetaItemKind::List(list)) = normal.item.meta_kind(); - if list[0].ident().unwrap().name == sym!(lint_reasons); + if let Some(symbol) = list.get(0); + if symbol.ident().unwrap().name == sym!(lint_reasons); then { self.lint_reasons_active = true; } From d65c9a5700454ab2a3bd81dccff610ad13855d14 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 11 Mar 2023 21:30:34 +0100 Subject: [PATCH 5/7] Extend tests + improve description + general improvement --- clippy_lints/src/allow_attribute.rs | 84 ++++++++++++----------------- clippy_lints/src/lib.rs | 6 +-- tests/ui/allow_attribute.fixed | 7 +++ tests/ui/allow_attribute.rs | 7 +++ tests/ui/allow_attribute.stderr | 12 +++-- 5 files changed, 59 insertions(+), 57 deletions(-) diff --git a/clippy_lints/src/allow_attribute.rs b/clippy_lints/src/allow_attribute.rs index defd14da9ef..28fc45d0554 100644 --- a/clippy_lints/src/allow_attribute.rs +++ b/clippy_lints/src/allow_attribute.rs @@ -1,16 +1,27 @@ -use ast::{AttrStyle, MetaItemKind}; -use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet}; +use ast::AttrStyle; +use clippy_utils::diagnostics::span_lint_and_sugg; 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}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; 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)) + /// Detects uses of the `#[allow]` attribute and suggests replacing it with + /// the `#[expect]` (See [RFC 2383](https://rust-lang.github.io/rfcs/2383-lint-reasons.html)) + /// + /// The expect attribute is still unstable and requires the `lint_reasons` + /// on nightly. It can be enabled by adding `#![feature(lint_reasons)]` to + /// the crate root. + /// + /// This lint only warns outer attributes (`#[allow]`), as inner attributes + /// (`#![allow]`) are usually used to enable or disable lints on a global scale. + /// /// ### 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. + /// + /// `#[expect]` attributes suppress the lint emission, but emit a warning, if + /// the expectation is unfulfilled. This can be useful to be notified when the + /// lint is no longer triggered. + /// /// ### Example /// ```rust,ignore /// #[allow(unused_mut)] @@ -34,59 +45,34 @@ declare_clippy_lint! { "`#[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]); +declare_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; - } - } - - // Check outer attributes - if_chain! { + if cx.tcx.features().lint_reasons; 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); - if self.lint_reasons_active; + if let Some(ident) = attr.ident(); + if ident.name == rustc_span::symbol::sym::allow; then { span_lint_and_sugg( cx, ALLOW_ATTRIBUTE, - attr.span, + ident.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); + "replace it with", "expect".into() + // format!("expect{}", snippet( + // cx, + // ident.span + // .with_lo( + // ident.span.hi() + BytePos(2) // Cut *( + // ) + // .with_hi( + // attr.meta().unwrap().span.hi() - BytePos(1) // Cut ) + // ) + // , "...")) + , Applicability::MachineApplicable); } } } diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 51da5d1ba49..a3c65a69c99 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -934,11 +934,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(missing_assert_message::MissingAssertMessage)); store.register_early_pass(|| Box::new(redundant_async_block::RedundantAsyncBlock)); store.register_late_pass(|_| Box::new(let_with_type_underscore::UnderscoreTyped)); - store.register_late_pass(|_| { - Box::new(allow_attribute::AllowAttribute { - lint_reasons_active: false, - }) - }); + store.register_late_pass(|_| Box::new(allow_attribute::AllowAttribute)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/tests/ui/allow_attribute.fixed b/tests/ui/allow_attribute.fixed index 5f445a0bd96..01759ced09d 100644 --- a/tests/ui/allow_attribute.fixed +++ b/tests/ui/allow_attribute.fixed @@ -16,3 +16,10 @@ struct T2; // Should not lint struct T3; #[warn(clippy::needless_borrow)] // Should not lint struct T4; +// `panic = "unwind"` should always be true +#[cfg_attr(panic = "unwind", expect(dead_code))] +struct CfgT; + +fn ignore_inner_attr() { + #![allow(unused)] // Should not lint +} diff --git a/tests/ui/allow_attribute.rs b/tests/ui/allow_attribute.rs index 36d2e3e27ab..b8e341fe9e4 100644 --- a/tests/ui/allow_attribute.rs +++ b/tests/ui/allow_attribute.rs @@ -16,3 +16,10 @@ struct T2; // Should not lint struct T3; #[warn(clippy::needless_borrow)] // Should not lint struct T4; +// `panic = "unwind"` should always be true +#[cfg_attr(panic = "unwind", allow(dead_code))] +struct CfgT; + +fn ignore_inner_attr() { + #![allow(unused)] // Should not lint +} diff --git a/tests/ui/allow_attribute.stderr b/tests/ui/allow_attribute.stderr index 6f90661577c..58b7323b068 100644 --- a/tests/ui/allow_attribute.stderr +++ b/tests/ui/allow_attribute.stderr @@ -1,10 +1,16 @@ error: #[allow] attribute found - --> $DIR/allow_attribute.rs:11:1 + --> $DIR/allow_attribute.rs:11:3 | LL | #[allow(dead_code)] - | ^^^^^^^^^^^^^^^^^^^ help: replace it with: `#[expect(dead_code)]` + | ^^^^^ help: replace it with: `expect` | = note: `-D clippy::allow-attribute` implied by `-D warnings` -error: aborting due to previous error +error: #[allow] attribute found + --> $DIR/allow_attribute.rs:20:30 + | +LL | #[cfg_attr(panic = "unwind", allow(dead_code))] + | ^^^^^ help: replace it with: `expect` + +error: aborting due to 2 previous errors From 4b9cb857f9b6da4e3b274601a6c439aac8a2c425 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Wed, 15 Mar 2023 23:18:25 +0100 Subject: [PATCH 6/7] Rename lint --- CHANGELOG.md | 2 +- .../src/{allow_attribute.rs => allow_attributes.rs} | 6 +++--- clippy_lints/src/declared_lints.rs | 2 +- clippy_lints/src/lib.rs | 4 ++-- tests/ui/{allow_attribute.fixed => allow_attributes.fixed} | 2 +- tests/ui/{allow_attribute.rs => allow_attributes.rs} | 2 +- .../ui/{allow_attribute.stderr => allow_attributes.stderr} | 6 +++--- 7 files changed, 12 insertions(+), 12 deletions(-) rename clippy_lints/src/{allow_attribute.rs => allow_attributes.rs} (95%) rename tests/ui/{allow_attribute.fixed => allow_attributes.fixed} (93%) rename tests/ui/{allow_attribute.rs => allow_attributes.rs} (93%) rename tests/ui/{allow_attribute.stderr => allow_attributes.stderr} (69%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b00fd962ca..c090a844858 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4382,7 +4382,7 @@ Released 2018-09-13 [`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons [`alloc_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#alloc_instead_of_core -[`allow_attribute`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attribute +[`allow_attributes`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes [`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason [`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range [`almost_complete_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range diff --git a/clippy_lints/src/allow_attribute.rs b/clippy_lints/src/allow_attributes.rs similarity index 95% rename from clippy_lints/src/allow_attribute.rs rename to clippy_lints/src/allow_attributes.rs index 28fc45d0554..3d660a11ea3 100644 --- a/clippy_lints/src/allow_attribute.rs +++ b/clippy_lints/src/allow_attributes.rs @@ -40,12 +40,12 @@ declare_clippy_lint! { /// } /// ``` #[clippy::version = "1.69.0"] - pub ALLOW_ATTRIBUTE, + pub ALLOW_ATTRIBUTES, restriction, "`#[allow]` will not trigger if a warning isn't found. `#[expect]` triggers if there are no warnings." } -declare_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTE]); +declare_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTES]); impl LateLintPass<'_> for AllowAttribute { // Separate each crate's features. @@ -58,7 +58,7 @@ impl LateLintPass<'_> for AllowAttribute { then { span_lint_and_sugg( cx, - ALLOW_ATTRIBUTE, + ALLOW_ATTRIBUTES, ident.span, "#[allow] attribute found", "replace it with", "expect".into() diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 73f74d59f4a..25f15973490 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -35,7 +35,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO, #[cfg(feature = "internal")] crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO, - crate::allow_attribute::ALLOW_ATTRIBUTE_INFO, + crate::allow_attributes::ALLOW_ATTRIBUTES_INFO, crate::almost_complete_range::ALMOST_COMPLETE_RANGE_INFO, crate::approx_const::APPROX_CONSTANT_INFO, crate::as_conversions::AS_CONVERSIONS_INFO, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index a3c65a69c99..c455c3e3058 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -67,7 +67,7 @@ mod declared_lints; mod renamed_lints; // begin lints modules, do not remove this comment, it’s used in `update_lints` -mod allow_attribute; +mod allow_attributes; mod almost_complete_range; mod approx_const; mod as_conversions; @@ -934,7 +934,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(missing_assert_message::MissingAssertMessage)); store.register_early_pass(|| Box::new(redundant_async_block::RedundantAsyncBlock)); store.register_late_pass(|_| Box::new(let_with_type_underscore::UnderscoreTyped)); - store.register_late_pass(|_| Box::new(allow_attribute::AllowAttribute)); + store.register_late_pass(|_| Box::new(allow_attributes::AllowAttribute)); // add lints here, do not remove this comment, it's used in `new_lint` } diff --git a/tests/ui/allow_attribute.fixed b/tests/ui/allow_attributes.fixed similarity index 93% rename from tests/ui/allow_attribute.fixed rename to tests/ui/allow_attributes.fixed index 01759ced09d..b8dd0619e6d 100644 --- a/tests/ui/allow_attribute.fixed +++ b/tests/ui/allow_attributes.fixed @@ -1,6 +1,6 @@ // run-rustfix #![allow(unused)] -#![warn(clippy::allow_attribute)] +#![warn(clippy::allow_attributes)] #![feature(lint_reasons)] fn main() {} diff --git a/tests/ui/allow_attribute.rs b/tests/ui/allow_attributes.rs similarity index 93% rename from tests/ui/allow_attribute.rs rename to tests/ui/allow_attributes.rs index b8e341fe9e4..295f560906a 100644 --- a/tests/ui/allow_attribute.rs +++ b/tests/ui/allow_attributes.rs @@ -1,6 +1,6 @@ // run-rustfix #![allow(unused)] -#![warn(clippy::allow_attribute)] +#![warn(clippy::allow_attributes)] #![feature(lint_reasons)] fn main() {} diff --git a/tests/ui/allow_attribute.stderr b/tests/ui/allow_attributes.stderr similarity index 69% rename from tests/ui/allow_attribute.stderr rename to tests/ui/allow_attributes.stderr index 58b7323b068..681837e9ed7 100644 --- a/tests/ui/allow_attribute.stderr +++ b/tests/ui/allow_attributes.stderr @@ -1,13 +1,13 @@ error: #[allow] attribute found - --> $DIR/allow_attribute.rs:11:3 + --> $DIR/allow_attributes.rs:11:3 | LL | #[allow(dead_code)] | ^^^^^ help: replace it with: `expect` | - = note: `-D clippy::allow-attribute` implied by `-D warnings` + = note: `-D clippy::allow-attributes` implied by `-D warnings` error: #[allow] attribute found - --> $DIR/allow_attribute.rs:20:30 + --> $DIR/allow_attributes.rs:20:30 | LL | #[cfg_attr(panic = "unwind", allow(dead_code))] | ^^^^^ help: replace it with: `expect` From 1cab0cbaf0dddb4cd403c4a0c391315801a22835 Mon Sep 17 00:00:00 2001 From: blyxyas Date: Wed, 15 Mar 2023 23:20:52 +0100 Subject: [PATCH 7/7] Fix formatting, remove commented code, etc... --- clippy_lints/src/allow_attributes.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/allow_attributes.rs b/clippy_lints/src/allow_attributes.rs index 3d660a11ea3..15d46e954a9 100644 --- a/clippy_lints/src/allow_attributes.rs +++ b/clippy_lints/src/allow_attributes.rs @@ -6,7 +6,7 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { - /// Detects uses of the `#[allow]` attribute and suggests replacing it with + /// Detects uses of the `#[allow]` attribute and suggests replacing it with /// the `#[expect]` (See [RFC 2383](https://rust-lang.github.io/rfcs/2383-lint-reasons.html)) /// /// The expect attribute is still unstable and requires the `lint_reasons` @@ -28,11 +28,11 @@ declare_clippy_lint! { /// fn foo() -> usize { /// let mut a = Vec::new(); /// a.len() - ///} + /// } /// ``` /// Use instead: /// ```rust,ignore - /// # #![feature(lint_reasons)] + /// #![feature(lint_reasons)] /// #[expect(unused_mut)] /// fn foo() -> usize { /// let mut a = Vec::new(); @@ -61,18 +61,10 @@ impl LateLintPass<'_> for AllowAttribute { ALLOW_ATTRIBUTES, ident.span, "#[allow] attribute found", - "replace it with", "expect".into() - // format!("expect{}", snippet( - // cx, - // ident.span - // .with_lo( - // ident.span.hi() + BytePos(2) // Cut *( - // ) - // .with_hi( - // attr.meta().unwrap().span.hi() - BytePos(1) // Cut ) - // ) - // , "...")) - , Applicability::MachineApplicable); + "replace it with", + "expect".into(), + Applicability::MachineApplicable, + ); } } }