@@ -11,50 +11,40 @@ use crate::rustc::hir::{Expr, ExprKind};
1111use crate :: rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
1212use crate :: rustc:: { declare_tool_lint, lint_array} ;
1313use crate :: syntax:: ast:: LitKind ;
14- use crate :: utils:: { is_direct_expn_of, span_lint} ;
14+ use crate :: utils:: { is_direct_expn_of, span_lint, span_lint_and_sugg} ;
15+ use rustc_errors:: Applicability ;
1516use if_chain:: if_chain;
1617
17- /// **What it does:** Check explicit call assert!(true)
18+ /// **What it does:** Check explicit call assert!(true/false )
1819///
19- /// **Why is this bad?** Will be optimized out by the compiler
20- ///
21- /// **Known problems:** None
22- ///
23- /// **Example:**
24- /// ```rust
25- /// assert!(true)
26- /// ```
27- declare_clippy_lint ! {
28- pub EXPLICIT_TRUE ,
29- correctness,
30- "assert!(true) will be optimized out by the compiler"
31- }
32-
33- /// **What it does:** Check explicit call assert!(false)
34- ///
35- /// **Why is this bad?** Should probably be replaced by a panic!() or unreachable!()
20+ /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a panic!() or unreachable!()
3621///
3722/// **Known problems:** None
3823///
3924/// **Example:**
4025/// ```rust
4126/// assert!(false)
27+ /// // or
28+ /// assert!(true)
29+ /// // or
30+ /// const B: bool = false;
31+ /// assert!(B)
4232/// ```
4333declare_clippy_lint ! {
44- pub EXPLICIT_FALSE ,
45- correctness ,
46- "assert!(false) should probably be replaced by a panic!() or unreachable!()"
34+ pub ASSERTIONS_ON_CONSTANTS ,
35+ style ,
36+ "assert!(true/ false) will be optimized out by the compiler/ should probably be replaced by a panic!() or unreachable!()"
4737}
4838
49- pub struct AssertChecks ;
39+ pub struct AssertionsOnConstants ;
5040
51- impl LintPass for AssertChecks {
41+ impl LintPass for AssertionsOnConstants {
5242 fn get_lints ( & self ) -> LintArray {
53- lint_array ! [ EXPLICIT_TRUE , EXPLICIT_FALSE ]
43+ lint_array ! [ ASSERTIONS_ON_CONSTANTS ]
5444 }
5545}
5646
57- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for AssertChecks {
47+ impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for AssertionsOnConstants {
5848 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , e : & ' tcx Expr ) {
5949 if_chain ! {
6050 if is_direct_expn_of( e. span, "assert" ) . is_some( ) ;
@@ -63,12 +53,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertChecks {
6353 then {
6454 match inner. node {
6555 LitKind :: Bool ( true ) => {
66- span_lint( cx, EXPLICIT_TRUE , e. span,
56+ span_lint( cx, ASSERTIONS_ON_CONSTANTS , e. span,
6757 "assert!(true) will be optimized out by the compiler" ) ;
6858 } ,
6959 LitKind :: Bool ( false ) => {
70- span_lint( cx, EXPLICIT_FALSE , e. span,
71- "assert!(false) should probably be replaced by a panic!() or unreachable!()" ) ;
60+ span_lint_and_sugg(
61+ cx,
62+ ASSERTIONS_ON_CONSTANTS ,
63+ e. span,
64+ "assert!(false) should probably be replaced" ,
65+ "try" ,
66+ "panic!()" . to_string( ) ,
67+ Applicability :: MachineApplicable ) ;
7268 } ,
7369 _ => ( ) ,
7470 }
0 commit comments