@@ -2,9 +2,9 @@ use if_chain::if_chain;
22use rustc:: hir:: { Expr , ExprKind } ;
33use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
44use rustc:: { declare_lint_pass, declare_tool_lint} ;
5+ use syntax_pos:: Span ;
56
67use crate :: consts:: { constant, Constant } ;
7- use crate :: syntax:: ast:: LitKind ;
88use crate :: utils:: { in_macro, is_direct_expn_of, span_help_and_lint} ;
99
1010declare_clippy_lint ! {
@@ -33,41 +33,40 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3333
3434impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for AssertionsOnConstants {
3535 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr ) {
36+ let mut is_debug_assert = false ;
37+ let debug_assert_not_in_macro = |span : Span | {
38+ is_debug_assert = true ;
39+ // Check that `debug_assert!` itself is not inside a macro
40+ !in_macro ( span)
41+ } ;
3642 if_chain ! {
3743 if let Some ( assert_span) = is_direct_expn_of( e. span, "assert" ) ;
3844 if !in_macro( assert_span)
39- || is_direct_expn_of( assert_span, "debug_assert" ) . map_or( false , |span| !in_macro( span) ) ;
45+ || is_direct_expn_of( assert_span, "debug_assert" )
46+ . map_or( false , debug_assert_not_in_macro) ;
4047 if let ExprKind :: Unary ( _, ref lit) = e. node;
48+ if let Some ( bool_const) = constant( cx, cx. tables, lit) ;
4149 then {
42- if let ExprKind :: Lit ( ref inner) = lit. node {
43- match inner. node {
44- LitKind :: Bool ( true ) => {
45- span_help_and_lint( cx, ASSERTIONS_ON_CONSTANTS , e. span,
46- "assert!(true) will be optimized out by the compiler" ,
47- "remove it" ) ;
48- } ,
49- LitKind :: Bool ( false ) => {
50- span_help_and_lint(
51- cx, ASSERTIONS_ON_CONSTANTS , e. span,
52- "assert!(false) should probably be replaced" ,
53- "use panic!() or unreachable!()" ) ;
54- } ,
55- _ => ( ) ,
56- }
57- } else if let Some ( bool_const) = constant( cx, cx. tables, lit) {
58- match bool_const. 0 {
59- Constant :: Bool ( true ) => {
60- span_help_and_lint( cx, ASSERTIONS_ON_CONSTANTS , e. span,
61- "assert!(const: true) will be optimized out by the compiler" ,
62- "remove it" ) ;
63- } ,
64- Constant :: Bool ( false ) => {
65- span_help_and_lint( cx, ASSERTIONS_ON_CONSTANTS , e. span,
66- "assert!(const: false) should probably be replaced" ,
67- "use panic!() or unreachable!()" ) ;
68- } ,
69- _ => ( ) ,
70- }
50+ match bool_const. 0 {
51+ Constant :: Bool ( true ) => {
52+ span_help_and_lint(
53+ cx,
54+ ASSERTIONS_ON_CONSTANTS ,
55+ e. span,
56+ "`assert!(true)` will be optimized out by the compiler" ,
57+ "remove it"
58+ ) ;
59+ } ,
60+ Constant :: Bool ( false ) if !is_debug_assert => {
61+ span_help_and_lint(
62+ cx,
63+ ASSERTIONS_ON_CONSTANTS ,
64+ e. span,
65+ "`assert!(false)` should probably be replaced" ,
66+ "use `panic!()` or `unreachable!()`"
67+ ) ;
68+ } ,
69+ _ => ( ) ,
7170 }
7271 }
7372 }
0 commit comments