1
1
//! checks for attributes
2
2
3
3
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;
4
5
use clippy_utils:: macros:: { is_panic, macro_backtrace} ;
5
6
use clippy_utils:: msrvs:: { self , Msrv } ;
6
7
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} ;
8
8
use if_chain:: if_chain;
9
9
use rustc_ast:: token:: { Token , TokenKind } ;
10
10
use rustc_ast:: tokenstream:: TokenTree ;
@@ -13,7 +13,7 @@ use rustc_ast::{
13
13
} ;
14
14
use rustc_errors:: Applicability ;
15
15
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 ,
17
17
} ;
18
18
use rustc_lint:: { EarlyContext , EarlyLintPass , LateContext , LateLintPass , Level , LintContext } ;
19
19
use rustc_middle:: lint:: in_external_macro;
@@ -482,6 +482,9 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
482
482
}
483
483
}
484
484
}
485
+ if attr. has_name ( sym:: should_panic) {
486
+ check_should_panic_reason ( cx, attr) ;
487
+ }
485
488
}
486
489
487
490
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' _ > ) {
@@ -558,42 +561,6 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
558
561
}
559
562
}
560
563
} ,
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
- } ,
597
564
_ => { } ,
598
565
}
599
566
}
0 commit comments