Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6166f60

Browse files
authored
Add ignore_without_reason lint (rust-lang#13931)
Closes rust-lang#13066 (tests that are ignored with no reason message). changelog: Add `ignore_without_reason` lint
2 parents a65cc36 + 5b234c9 commit 6166f60

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5681,6 +5681,7 @@ Released 2018-09-13
56815681
[`if_same_then_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else
56825682
[`if_then_some_else_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
56835683
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
5684+
[`ignore_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#ignore_without_reason
56845685
[`ignored_unit_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#ignored_unit_patterns
56855686
[`impl_hash_borrow_with_str_and_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#impl_hash_borrow_with_str_and_bytes
56865687
[`impl_trait_in_params`]: https://rust-lang.github.io/rust-clippy/master/index.html#impl_trait_in_params

clippy_lints/src/attrs/mod.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ mod useless_attribute;
1414
mod utils;
1515

1616
use clippy_config::Conf;
17+
use clippy_utils::diagnostics::span_lint_and_help;
1718
use clippy_utils::msrvs::{self, Msrv, MsrvStack};
18-
use rustc_ast::{self as ast, Attribute, MetaItemInner, MetaItemKind};
19+
use rustc_ast::{self as ast, AttrArgs, AttrKind, Attribute, MetaItemInner, MetaItemKind};
1920
use rustc_hir::{ImplItem, Item, ItemKind, TraitItem};
2021
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
2122
use rustc_session::impl_lint_pass;
@@ -448,6 +449,31 @@ declare_clippy_lint! {
448449
"duplicated attribute"
449450
}
450451

452+
declare_clippy_lint! {
453+
/// ### What it does
454+
/// Checks for ignored tests without messages.
455+
///
456+
/// ### Why is this bad?
457+
/// The reason for ignoring the test may not be obvious.
458+
///
459+
/// ### Example
460+
/// ```no_run
461+
/// #[test]
462+
/// #[ignore]
463+
/// fn test() {}
464+
/// ```
465+
/// Use instead:
466+
/// ```no_run
467+
/// #[test]
468+
/// #[ignore = "Some good reason"]
469+
/// fn test() {}
470+
/// ```
471+
#[clippy::version = "1.85.0"]
472+
pub IGNORE_WITHOUT_REASON,
473+
pedantic,
474+
"ignored tests without messages"
475+
}
476+
451477
pub struct Attributes {
452478
msrv: Msrv,
453479
}
@@ -532,6 +558,7 @@ impl_lint_pass!(PostExpansionEarlyAttributes => [
532558
ALLOW_ATTRIBUTES,
533559
ALLOW_ATTRIBUTES_WITHOUT_REASON,
534560
DEPRECATED_SEMVER,
561+
IGNORE_WITHOUT_REASON,
535562
USELESS_ATTRIBUTE,
536563
BLANKET_CLIPPY_RESTRICTION_LINTS,
537564
SHOULD_PANIC_WITHOUT_EXPECT,
@@ -575,6 +602,22 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
575602
if attr.has_name(sym::should_panic) {
576603
should_panic_without_expect::check(cx, attr);
577604
}
605+
606+
if attr.has_name(sym::ignore)
607+
&& match &attr.kind {
608+
AttrKind::Normal(normal_attr) => !matches!(normal_attr.item.args, AttrArgs::Eq { .. }),
609+
AttrKind::DocComment(..) => true,
610+
}
611+
{
612+
span_lint_and_help(
613+
cx,
614+
IGNORE_WITHOUT_REASON,
615+
attr.span,
616+
"`#[ignore]` without reason",
617+
None,
618+
"add a reason with `= \"..\"`",
619+
);
620+
}
578621
}
579622

580623
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &'_ ast::Item) {

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
5252
crate::attrs::DEPRECATED_CLIPPY_CFG_ATTR_INFO,
5353
crate::attrs::DEPRECATED_SEMVER_INFO,
5454
crate::attrs::DUPLICATED_ATTRIBUTES_INFO,
55+
crate::attrs::IGNORE_WITHOUT_REASON_INFO,
5556
crate::attrs::INLINE_ALWAYS_INFO,
5657
crate::attrs::MIXED_ATTRIBUTES_STYLE_INFO,
5758
crate::attrs::NON_MINIMAL_CFG_INFO,

tests/ui/ignore_without_reason.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![warn(clippy::ignore_without_reason)]
2+
3+
fn main() {}
4+
5+
#[test]
6+
fn unignored_test() {}
7+
8+
#[test]
9+
#[ignore = "Some good reason"]
10+
fn ignored_with_reason() {}
11+
12+
#[test]
13+
#[ignore] //~ ignore_without_reason
14+
fn ignored_without_reason() {}

tests/ui/ignore_without_reason.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: `#[ignore]` without reason
2+
--> tests/ui/ignore_without_reason.rs:13:1
3+
|
4+
LL | #[ignore]
5+
| ^^^^^^^^^
6+
|
7+
= help: add a reason with `= ".."`
8+
= note: `-D clippy::ignore-without-reason` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::ignore_without_reason)]`
10+
11+
error: aborting due to 1 previous error
12+

0 commit comments

Comments
 (0)