Auto merge of #10481 - blyxyas:allow_attribute, r=xFrednet
Add `allow_attribute` lint Fixes #10468 changelog: new lint: [`allow_attributes`] [#10481](https://github.com/rust-lang/rust-clippy/pull/10481)
This commit is contained in:
commit
5afa93bd8e
@ -4382,6 +4382,7 @@ Released 2018-09-13
|
||||
<!-- begin autogenerated links to lint list -->
|
||||
[`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_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
|
||||
|
71
clippy_lints/src/allow_attributes.rs
Normal file
71
clippy_lints/src/allow_attributes.rs
Normal file
@ -0,0 +1,71 @@
|
||||
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_lint_pass, declare_tool_lint};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// 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?
|
||||
///
|
||||
/// `#[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)]
|
||||
/// 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_ATTRIBUTES,
|
||||
restriction,
|
||||
"`#[allow]` will not trigger if a warning isn't found. `#[expect]` triggers if there are no warnings."
|
||||
}
|
||||
|
||||
declare_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTES]);
|
||||
|
||||
impl LateLintPass<'_> for AllowAttribute {
|
||||
// Separate each crate's features.
|
||||
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &ast::Attribute) {
|
||||
if_chain! {
|
||||
if cx.tcx.features().lint_reasons;
|
||||
if let AttrStyle::Outer = attr.style;
|
||||
if let Some(ident) = attr.ident();
|
||||
if ident.name == rustc_span::symbol::sym::allow;
|
||||
then {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
ALLOW_ATTRIBUTES,
|
||||
ident.span,
|
||||
"#[allow] attribute found",
|
||||
"replace it with",
|
||||
"expect".into(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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_attributes::ALLOW_ATTRIBUTES_INFO,
|
||||
crate::almost_complete_range::ALMOST_COMPLETE_RANGE_INFO,
|
||||
crate::approx_const::APPROX_CONSTANT_INFO,
|
||||
crate::as_conversions::AS_CONVERSIONS_INFO,
|
||||
|
@ -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_attributes;
|
||||
mod almost_complete_range;
|
||||
mod approx_const;
|
||||
mod as_conversions;
|
||||
@ -934,6 +935,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_attributes::AllowAttribute));
|
||||
// add lints here, do not remove this comment, it's used in `new_lint`
|
||||
}
|
||||
|
||||
|
25
tests/ui/allow_attributes.fixed
Normal file
25
tests/ui/allow_attributes.fixed
Normal file
@ -0,0 +1,25 @@
|
||||
// run-rustfix
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::allow_attributes)]
|
||||
#![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;
|
||||
// `panic = "unwind"` should always be true
|
||||
#[cfg_attr(panic = "unwind", expect(dead_code))]
|
||||
struct CfgT;
|
||||
|
||||
fn ignore_inner_attr() {
|
||||
#![allow(unused)] // Should not lint
|
||||
}
|
25
tests/ui/allow_attributes.rs
Normal file
25
tests/ui/allow_attributes.rs
Normal file
@ -0,0 +1,25 @@
|
||||
// run-rustfix
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::allow_attributes)]
|
||||
#![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;
|
||||
// `panic = "unwind"` should always be true
|
||||
#[cfg_attr(panic = "unwind", allow(dead_code))]
|
||||
struct CfgT;
|
||||
|
||||
fn ignore_inner_attr() {
|
||||
#![allow(unused)] // Should not lint
|
||||
}
|
16
tests/ui/allow_attributes.stderr
Normal file
16
tests/ui/allow_attributes.stderr
Normal file
@ -0,0 +1,16 @@
|
||||
error: #[allow] attribute found
|
||||
--> $DIR/allow_attributes.rs:11:3
|
||||
|
|
||||
LL | #[allow(dead_code)]
|
||||
| ^^^^^ help: replace it with: `expect`
|
||||
|
|
||||
= note: `-D clippy::allow-attributes` implied by `-D warnings`
|
||||
|
||||
error: #[allow] attribute found
|
||||
--> $DIR/allow_attributes.rs:20:30
|
||||
|
|
||||
LL | #[cfg_attr(panic = "unwind", allow(dead_code))]
|
||||
| ^^^^^ help: replace it with: `expect`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
Loading…
x
Reference in New Issue
Block a user