|
1 |
| -use rustc::hir::{Expr, ExprKind}; |
| 1 | +use crate::consts::{constant, Constant}; |
| 2 | +use crate::utils::{is_direct_expn_of, is_expn_of, match_qpath, span_help_and_lint}; |
| 3 | +use if_chain::if_chain; |
| 4 | +use rustc::hir::*; |
2 | 5 | use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
3 | 6 | use rustc::{declare_lint_pass, declare_tool_lint};
|
4 |
| - |
5 |
| -use crate::consts::{constant, Constant}; |
6 |
| -use crate::utils::{is_direct_expn_of, is_expn_of, span_help_and_lint}; |
| 7 | +use syntax::ast::LitKind; |
| 8 | +use syntax::source_map::symbol::LocalInternedString; |
7 | 9 |
|
8 | 10 | declare_clippy_lint! {
|
9 | 11 | /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
|
@@ -63,7 +65,93 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
63 | 65 | if assert_span.from_expansion() {
|
64 | 66 | return;
|
65 | 67 | }
|
66 |
| - lint_assert_cb(false); |
| 68 | + if let Some((panic_message, is_true)) = assert_with_message(&cx, e) { |
| 69 | + if is_true { |
| 70 | + span_help_and_lint( |
| 71 | + cx, |
| 72 | + ASSERTIONS_ON_CONSTANTS, |
| 73 | + e.span, |
| 74 | + "`assert!(true)` will be optimized out by the compiler", |
| 75 | + "remove it", |
| 76 | + ); |
| 77 | + } else if panic_message.starts_with("assertion failed: ") { |
| 78 | + span_help_and_lint( |
| 79 | + cx, |
| 80 | + ASSERTIONS_ON_CONSTANTS, |
| 81 | + e.span, |
| 82 | + "`assert!(false)` should probably be replaced", |
| 83 | + "use `panic!()` or `unreachable!()`", |
| 84 | + ); |
| 85 | + } else { |
| 86 | + span_help_and_lint( |
| 87 | + cx, |
| 88 | + ASSERTIONS_ON_CONSTANTS, |
| 89 | + e.span, |
| 90 | + &format!("`assert!(false, \"{}\")` should probably be replaced", panic_message,), |
| 91 | + &format!( |
| 92 | + "use `panic!(\"{}\")` or `unreachable!(\"{}\")`", |
| 93 | + panic_message, panic_message, |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// fn get_assert_args(snip: String) -> Option<Vec<String>> { |
| 103 | +// |
| 104 | +// } |
| 105 | + |
| 106 | +fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(LocalInternedString, bool)> { |
| 107 | + if_chain! { |
| 108 | + if let ExprKind::Match(ref expr, ref arms, MatchSource::IfDesugar { contains_else_clause: false }) = expr.kind; |
| 109 | + // match expr |
| 110 | + if let ExprKind::DropTemps(ref expr) = expr.kind; |
| 111 | + if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind; |
| 112 | + //if let ExprKind::Lit(ref lit) = expr.kind; |
| 113 | + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr); |
| 114 | + //if is_true; |
| 115 | + // match arms |
| 116 | + // arm 1 pattern |
| 117 | + if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind; |
| 118 | + if let ExprKind::Lit(ref lit) = lit_expr.kind; |
| 119 | + if let LitKind::Bool(true) = lit.node; |
| 120 | + //if let LitKind::Bool(true) = lit1.node; |
| 121 | + // arm 1 block |
| 122 | + if let ExprKind::Block(ref block1, _) = arms[0].body.kind; |
| 123 | + if let Some(trailing_expr1) = &block1.expr; |
| 124 | + if block1.stmts.len() == 0; |
| 125 | + // |
| 126 | + if let ExprKind::Block(ref actual_block1, _) = trailing_expr1.kind; |
| 127 | + if let Some(block1_expr) = &actual_block1.expr; |
| 128 | + // function call |
| 129 | + if let ExprKind::Call(ref func, ref args) = block1_expr.kind; |
| 130 | + if let ExprKind::Path(ref path) = func.kind; |
| 131 | + // ["{{root}}", "std", "rt", "begin_panic"] does not work |
| 132 | + if match_qpath(path, &["$crate", "rt", "begin_panic"]); |
| 133 | + // arguments |
| 134 | + if args.len() == 2; |
| 135 | + if let ExprKind::Lit(ref lit) = args[0].kind; |
| 136 | + if let LitKind::Str(ref s, _) = lit.node; |
| 137 | + let panic_message = s.as_str(); // bind the panic message |
| 138 | + if let ExprKind::AddrOf(MutImmutable, ref inner) = args[1].kind; |
| 139 | + if let ExprKind::Tup(ref elements) = inner.kind; |
| 140 | + if elements.len() == 3; |
| 141 | + if let ExprKind::Lit(ref lit1) = elements[0].kind; |
| 142 | + if let LitKind::Str(ref s1, _) = lit1.node; |
| 143 | + if let ExprKind::Lit(ref lit2) = elements[1].kind; |
| 144 | + if let LitKind::Int(_, _) = lit2.node; |
| 145 | + if let ExprKind::Lit(ref lit3) = elements[2].kind; |
| 146 | + if let LitKind::Int(_, _) = lit3.node; |
| 147 | + // arm 2 block |
| 148 | + if let PatKind::Wild = arms[1].pat.kind; |
| 149 | + if let ExprKind::Block(ref block2, _) = arms[1].body.kind; |
| 150 | + if let None = &block2.expr; |
| 151 | + if block2.stmts.len() == 0; |
| 152 | + then { |
| 153 | + return Some((panic_message, is_true)); |
67 | 154 | }
|
68 | 155 | }
|
| 156 | + return None; |
69 | 157 | }
|
0 commit comments