Skip to content

Commit e069ea0

Browse files
committed
look for should_panic attribute directly instead
1 parent 1a0ff8c commit e069ea0

File tree

1 file changed

+5
-38
lines changed

1 file changed

+5
-38
lines changed

clippy_lints/src/attrs.rs

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! checks for attributes
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
4+
use clippy_utils::is_from_proc_macro;
45
use clippy_utils::macros::{is_panic, macro_backtrace};
56
use clippy_utils::msrvs::{self, Msrv};
67
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
7-
use clippy_utils::{is_from_proc_macro, match_def_path, paths};
88
use if_chain::if_chain;
99
use rustc_ast::token::{Token, TokenKind};
1010
use rustc_ast::tokenstream::TokenTree;
@@ -13,7 +13,7 @@ use rustc_ast::{
1313
};
1414
use rustc_errors::Applicability;
1515
use rustc_hir::{
16-
Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, QPath, StmtKind, TraitFn, TraitItem, TraitItemKind,
16+
Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind,
1717
};
1818
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext};
1919
use rustc_middle::lint::in_external_macro;
@@ -482,6 +482,9 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
482482
}
483483
}
484484
}
485+
if attr.has_name(sym::should_panic) {
486+
check_should_panic_reason(cx, attr);
487+
}
485488
}
486489

487490
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
@@ -558,42 +561,6 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
558561
}
559562
}
560563
},
561-
ItemKind::Const(_, body_id) => {
562-
// Looking for #[should_panic] attributes without a specified message means
563-
// looking for the expanded #[test] const item.
564-
//
565-
// #[test] attributes expand to a const item that initializes `test::TestDescAndFn`.
566-
//
567-
// `#[test] #[should_panic] fn f() {}`
568-
// becomes roughly:
569-
// ```
570-
// const f: test::TestDescAndFn = test::TestDescAndFn {
571-
// ...,
572-
// testfn: test::StaticTestFn(|| test::assert_result(f()))
573-
// }
574-
// ```
575-
// The #[should_panic] attribute, along with its span, is on the `f`.
576-
577-
let body = cx.tcx.hir().body(body_id);
578-
579-
if let ExprKind::Struct(QPath::Resolved(_, path), fields, _) = body.value.kind
580-
&& let Some(did) = path.res.opt_def_id()
581-
&& match_def_path(cx, did, &paths::TEST_DESC_AND_FN)
582-
&& let Some(testfn_field) = fields.iter().find(|field| field.ident.name == sym!(testfn))
583-
// test::StaticTestFn(|| ..)
584-
&& let ExprKind::Call(_, [closure]) = testfn_field.expr.kind
585-
&& let ExprKind::Closure(closure) = closure.kind
586-
// test::assert_result(f())
587-
// ^ get this `DefId` and its should_panic attr
588-
&& let ExprKind::Call(_, [fn_call_closure]) = cx.tcx.hir().body(closure.body).value.kind
589-
&& let ExprKind::Call(callee, _) = fn_call_closure.kind
590-
&& let ExprKind::Path(QPath::Resolved(_, path)) = callee.kind
591-
&& let Some(test_function_did) = path.res.opt_def_id()
592-
&& let Some(attribute) = cx.tcx.get_attr(test_function_did, sym::should_panic)
593-
{
594-
check_should_panic_reason(cx, attribute);
595-
}
596-
},
597564
_ => {},
598565
}
599566
}

0 commit comments

Comments
 (0)