|
1 | 1 | use crate::consts::{constant, Constant};
|
2 |
| -use crate::utils::{is_direct_expn_of, is_expn_of, match_qpath, span_help_and_lint}; |
| 2 | +use crate::utils::paths; |
| 3 | +use crate::utils::{is_direct_expn_of, is_expn_of, match_def_path, resolve_node, span_help_and_lint}; |
3 | 4 | use if_chain::if_chain;
|
4 | 5 | use rustc::hir::*;
|
5 | 6 | use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
@@ -99,59 +100,75 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
|
99 | 100 | }
|
100 | 101 | }
|
101 | 102 |
|
102 |
| -// fn get_assert_args(snip: String) -> Option<Vec<String>> { |
103 |
| -// |
104 |
| -// } |
105 |
| - |
| 103 | +/// Check if the expression matches |
| 104 | +/// |
| 105 | +/// ```rust,ignore |
| 106 | +/// match { let _t = !c; _t } { |
| 107 | +/// true => { |
| 108 | +/// { |
| 109 | +/// ::std::rt::begin_panic(message, _) |
| 110 | +/// } |
| 111 | +/// } |
| 112 | +/// _ => { } |
| 113 | +/// }; |
| 114 | +/// ``` |
| 115 | +/// |
| 116 | +/// where `message` is a string literal and `c` is a constant bool. |
| 117 | +/// |
| 118 | +/// TODO extend this to match anything as message not just string literals |
| 119 | +/// |
| 120 | +/// Returns the `message` argument of `begin_panic` and the value of `c` which is the |
| 121 | +/// first argument of `assert!`. |
106 | 122 | fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(LocalInternedString, bool)> {
|
107 | 123 | if_chain! {
|
108 |
| - if let ExprKind::Match(ref expr, ref arms, MatchSource::IfDesugar { contains_else_clause: false }) = expr.kind; |
109 |
| - // match expr |
| 124 | + if let ExprKind::Match(ref expr, ref arms, _) = expr.kind; |
| 125 | + // matches { let _t = expr; _t } |
110 | 126 | if let ExprKind::DropTemps(ref expr) = expr.kind;
|
111 | 127 | if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
|
112 |
| - //if let ExprKind::Lit(ref lit) = expr.kind; |
| 128 | + // bind the first argument of the `assert!` macro |
113 | 129 | if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
|
114 |
| - //if is_true; |
115 |
| - // match arms |
116 | 130 | // arm 1 pattern
|
117 | 131 | if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
|
118 | 132 | if let ExprKind::Lit(ref lit) = lit_expr.kind;
|
119 | 133 | if let LitKind::Bool(true) = lit.node;
|
120 |
| - //if let LitKind::Bool(true) = lit1.node; |
121 | 134 | // 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; |
| 135 | + if let ExprKind::Block(ref block, _) = arms[0].body.kind; |
| 136 | + if block.stmts.len() == 0; |
| 137 | + if let Some(block_expr) = &block.expr; |
| 138 | + if let ExprKind::Block(ref inner_block, _) = block_expr.kind; |
| 139 | + if let Some(begin_panic_call) = &inner_block.expr; |
128 | 140 | // 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 |
| 141 | + if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC); |
134 | 142 | if args.len() == 2;
|
135 | 143 | if let ExprKind::Lit(ref lit) = args[0].kind;
|
136 | 144 | 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; |
| 145 | + // bind the second argument of the `assert!` macro |
| 146 | + let panic_message = s.as_str(); |
| 147 | + // second argument of begin_panic is irrelevant |
| 148 | + // as is the second match arm |
152 | 149 | then {
|
153 | 150 | return Some((panic_message, is_true));
|
154 | 151 | }
|
155 | 152 | }
|
156 |
| - return None; |
| 153 | + None |
| 154 | +} |
| 155 | + |
| 156 | +/// Matches a function call with the given path and returns the arguments. |
| 157 | +/// |
| 158 | +/// Usage: |
| 159 | +/// |
| 160 | +/// ```rust,ignore |
| 161 | +/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC); |
| 162 | +/// ``` |
| 163 | +fn match_function_call<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, path: &[&str]) -> Option<&'a [Expr]> { |
| 164 | + if_chain! { |
| 165 | + if let ExprKind::Call(ref fun, ref args) = expr.kind; |
| 166 | + if let ExprKind::Path(ref qpath) = fun.kind; |
| 167 | + if let Some(fun_def_id) = resolve_node(cx, qpath, fun.hir_id).opt_def_id(); |
| 168 | + if match_def_path(cx, fun_def_id, path); |
| 169 | + then { |
| 170 | + return Some(&args) |
| 171 | + } |
| 172 | + }; |
| 173 | + None |
157 | 174 | }
|
0 commit comments