Skip to content

Commit

Permalink
move lint to EarlyAttributes, change applicability, add cfg test
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jul 21, 2023
1 parent 87a6270 commit 491a30c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ declare_clippy_lint! {

declare_lint_pass!(Attributes => [
ALLOW_ATTRIBUTES_WITHOUT_REASON,
SHOULD_PANIC_WITHOUT_REASON,
INLINE_ALWAYS,
DEPRECATED_SEMVER,
USELESS_ATTRIBUTE,
Expand Down Expand Up @@ -744,6 +743,7 @@ impl_lint_pass!(EarlyAttributes => [
EMPTY_LINE_AFTER_DOC_COMMENTS,
NON_MINIMAL_CFG,
MAYBE_MISUSED_CFG,
SHOULD_PANIC_WITHOUT_REASON,
]);

impl EarlyLintPass for EarlyAttributes {
Expand Down Expand Up @@ -821,7 +821,7 @@ fn check_should_panic_reason<'cx>(cx: &EarlyContext<'cx>, attr: &'cx Attribute)

if let AttrArgs::Delimited(args) = &normal_attr.item.args
&& let mut tt_iter = args.tokens.trees()
&& let Some(TokenTree::Token(Token { kind: TokenKind::Ident(sym::expected, _),.. }, _)) = tt_iter.next()
&& let Some(TokenTree::Token(Token { kind: TokenKind::Ident(sym::expected, _), .. }, _)) = tt_iter.next()
&& let Some(TokenTree::Token(Token { kind: TokenKind::Eq, .. }, _)) = tt_iter.next()
&& let Some(TokenTree::Token(Token { kind: TokenKind::Literal(_), .. }, _)) = tt_iter.next()
{
Expand All @@ -835,8 +835,8 @@ fn check_should_panic_reason<'cx>(cx: &EarlyContext<'cx>, attr: &'cx Attribute)
attr.span,
"#[should_panic] attribute without a reason",
"consider specifying the expected panic",
r#"#[should_panic(expected="<panic message>")]"#.into(),
Applicability::Unspecified
r#"#[should_panic(expected = /* panic message */)]"#.into(),
Applicability::HasPlaceholders
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/should_panic_without_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ fn no_message() {}
#[should_panic()]
fn no_message2() {}

#[cfg(test)]
#[test]
#[should_panic]
fn no_message_cfg_false() {}

#[should_panic = "message"]
fn metastr() {}

Expand Down
12 changes: 9 additions & 3 deletions tests/ui/should_panic_without_reason.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: #[should_panic] attribute without a reason
--> $DIR/should_panic_without_reason.rs:7:1
|
LL | #[should_panic]
| ^^^^^^^^^^^^^^^ help: consider specifying the expected panic: `#[should_panic(expected="<panic message>")]`
| ^^^^^^^^^^^^^^^ help: consider specifying the expected panic: `#[should_panic(expected = /* panic message */)]`
|
note: the lint level is defined here
--> $DIR/should_panic_without_reason.rs:2:9
Expand All @@ -14,7 +14,13 @@ error: #[should_panic] attribute without a reason
--> $DIR/should_panic_without_reason.rs:10:1
|
LL | #[should_panic()]
| ^^^^^^^^^^^^^^^^^ help: consider specifying the expected panic: `#[should_panic(expected="<panic message>")]`
| ^^^^^^^^^^^^^^^^^ help: consider specifying the expected panic: `#[should_panic(expected = /* panic message */)]`

error: aborting due to 2 previous errors
error: #[should_panic] attribute without a reason
--> $DIR/should_panic_without_reason.rs:15:1
|
LL | #[should_panic]
| ^^^^^^^^^^^^^^^ help: consider specifying the expected panic: `#[should_panic(expected = /* panic message */)]`

error: aborting due to 3 previous errors

0 comments on commit 491a30c

Please sign in to comment.