Skip to content

Commit d66acc2

Browse files
committed
Make if_chain more readable
1 parent 72a5d7b commit d66acc2

File tree

1 file changed

+55
-38
lines changed

1 file changed

+55
-38
lines changed

clippy_lints/src/assertions_on_constants.rs

+55-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
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};
34
use if_chain::if_chain;
45
use rustc::hir::*;
56
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -99,59 +100,75 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
99100
}
100101
}
101102

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!`.
106122
fn assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> Option<(LocalInternedString, bool)> {
107123
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 }
110126
if let ExprKind::DropTemps(ref expr) = expr.kind;
111127
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
113129
if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, expr);
114-
//if is_true;
115-
// match arms
116130
// arm 1 pattern
117131
if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
118132
if let ExprKind::Lit(ref lit) = lit_expr.kind;
119133
if let LitKind::Bool(true) = lit.node;
120-
//if let LitKind::Bool(true) = lit1.node;
121134
// 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;
128140
// 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);
134142
if args.len() == 2;
135143
if let ExprKind::Lit(ref lit) = args[0].kind;
136144
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
152149
then {
153150
return Some((panic_message, is_true));
154151
}
155152
}
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
157174
}

0 commit comments

Comments
 (0)