2 get_trait_def_id, implements_trait, in_macro, match_type, paths, snippet_opt, span_lint_and_then, SpanlessEq,
4 use rustc::hir::intravisit::*;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_tool_lint, lint_array};
8 use rustc_data_structures::thin_vec::ThinVec;
9 use rustc_errors::Applicability;
10 use syntax::ast::{LitKind, DUMMY_NODE_ID};
11 use syntax::source_map::{dummy_spanned, Span, DUMMY_SP};
13 /// **What it does:** Checks for boolean expressions that can be written more
16 /// **Why is this bad?** Readability of boolean expressions suffers from
17 /// unnecessary duplication.
19 /// **Known problems:** Ignores short circuiting behavior of `||` and
20 /// `&&`. Ignores `|`, `&` and `^`.
24 /// if a && true // should be: if a
25 /// if !(a == b) // should be: if a != b
27 declare_clippy_lint! {
30 "boolean expressions that can be written more concisely"
33 /// **What it does:** Checks for boolean expressions that contain terminals that
34 /// can be eliminated.
36 /// **Why is this bad?** This is most likely a logic bug.
38 /// **Known problems:** Ignores short circuiting behavior.
42 /// if a && b || a { ... }
44 /// The `b` is unnecessary, the expression is equivalent to `if a`.
45 declare_clippy_lint! {
48 "boolean expressions that contain terminals which can be eliminated"
51 // For each pairs, both orders are considered.
52 const METHODS_WITH_NEGATION: [(&str, &str); 2] = [("is_some", "is_none"), ("is_err", "is_ok")];
54 #[derive(Copy, Clone)]
55 pub struct NonminimalBool;
57 impl LintPass for NonminimalBool {
58 fn get_lints(&self) -> LintArray {
59 lint_array!(NONMINIMAL_BOOL, LOGIC_BUG)
62 fn name(&self) -> &'static str {
67 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonminimalBool {
70 cx: &LateContext<'a, 'tcx>,
71 _: intravisit::FnKind<'tcx>,
77 NonminimalBoolVisitor { cx }.visit_body(body)
81 struct NonminimalBoolVisitor<'a, 'tcx: 'a> {
82 cx: &'a LateContext<'a, 'tcx>,
85 use quine_mc_cluskey::Bool;
86 struct Hir2Qmm<'a, 'tcx: 'a, 'v> {
87 terminals: Vec<&'v Expr>,
88 cx: &'a LateContext<'a, 'tcx>,
91 impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
92 fn extract(&mut self, op: BinOpKind, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
94 if let ExprKind::Binary(binop, lhs, rhs) = &a.node {
96 v = self.extract(op, &[lhs, rhs], v)?;
100 v.push(self.run(a)?);
105 fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
106 // prevent folding of `cfg!` macros and the like
107 if !in_macro(e.span) {
109 ExprKind::Unary(UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)),
110 ExprKind::Binary(binop, lhs, rhs) => match &binop.node {
111 BinOpKind::Or => return Ok(Bool::Or(self.extract(BinOpKind::Or, &[lhs, rhs], Vec::new())?)),
112 BinOpKind::And => return Ok(Bool::And(self.extract(BinOpKind::And, &[lhs, rhs], Vec::new())?)),
115 ExprKind::Lit(lit) => match lit.node {
116 LitKind::Bool(true) => return Ok(Bool::True),
117 LitKind::Bool(false) => return Ok(Bool::False),
123 for (n, expr) in self.terminals.iter().enumerate() {
124 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e, expr) {
125 #[allow(clippy::cast_possible_truncation)]
126 return Ok(Bool::Term(n as u8));
128 let negated = match &e.node {
129 ExprKind::Binary(binop, lhs, rhs) => {
130 if !implements_ord(self.cx, lhs) {
134 let mk_expr = |op| Expr {
136 hir_id: DUMMY_HIR_ID,
138 attrs: ThinVec::new(),
139 node: ExprKind::Binary(dummy_spanned(op), lhs.clone(), rhs.clone()),
142 BinOpKind::Eq => mk_expr(BinOpKind::Ne),
143 BinOpKind::Ne => mk_expr(BinOpKind::Eq),
144 BinOpKind::Gt => mk_expr(BinOpKind::Le),
145 BinOpKind::Ge => mk_expr(BinOpKind::Lt),
146 BinOpKind::Lt => mk_expr(BinOpKind::Ge),
147 BinOpKind::Le => mk_expr(BinOpKind::Gt),
153 if SpanlessEq::new(self.cx).ignore_fn().eq_expr(&negated, expr) {
154 #[allow(clippy::cast_possible_truncation)]
155 return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
158 let n = self.terminals.len();
159 self.terminals.push(e);
161 #[allow(clippy::cast_possible_truncation)]
162 Ok(Bool::Term(n as u8))
164 Err("too many literals".to_owned())
169 struct SuggestContext<'a, 'tcx: 'a, 'v> {
170 terminals: &'v [&'v Expr],
171 cx: &'a LateContext<'a, 'tcx>,
176 impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
177 fn snip(&self, e: &Expr) -> Option<String> {
178 snippet_opt(self.cx, e.span)
181 fn simplify_not(&self, expr: &Expr) -> Option<String> {
183 ExprKind::Binary(binop, lhs, rhs) => {
184 if !implements_ord(self.cx, lhs) {
189 BinOpKind::Eq => Some(" != "),
190 BinOpKind::Ne => Some(" == "),
191 BinOpKind::Lt => Some(" >= "),
192 BinOpKind::Gt => Some(" <= "),
193 BinOpKind::Le => Some(" > "),
194 BinOpKind::Ge => Some(" < "),
197 .and_then(|op| Some(format!("{}{}{}", self.snip(lhs)?, op, self.snip(rhs)?)))
199 ExprKind::MethodCall(path, _, args) if args.len() == 1 => {
200 let type_of_receiver = self.cx.tables.expr_ty(&args[0]);
201 if !match_type(self.cx, type_of_receiver, &paths::OPTION)
202 && !match_type(self.cx, type_of_receiver, &paths::RESULT)
206 METHODS_WITH_NEGATION
209 .flat_map(|(a, b)| vec![(a, b), (b, a)])
210 .find(|&(a, _)| a == path.ident.as_str())
211 .and_then(|(_, neg_method)| Some(format!("{}.{}()", self.snip(&args[0])?, neg_method)))
217 fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
218 use quine_mc_cluskey::Bool::*;
221 self.output.push_str("true");
224 self.output.push_str("false");
226 Not(inner) => match **inner {
228 self.output.push('!');
229 self.output.push('(');
231 self.output.push(')');
234 let terminal = self.terminals[n as usize];
235 if let Some(str) = self.simplify_not(terminal) {
236 self.simplified = true;
237 self.output.push_str(&str)
239 self.output.push('!');
240 let snip = self.snip(terminal)?;
241 self.output.push_str(&snip);
244 True | False | Not(_) => {
245 self.output.push('!');
246 self.recurse(inner)?;
250 for (index, inner) in v.iter().enumerate() {
252 self.output.push_str(" && ");
254 if let Or(_) = *inner {
255 self.output.push('(');
257 self.output.push(')');
264 for (index, inner) in v.iter().enumerate() {
266 self.output.push_str(" || ");
272 let snip = self.snip(self.terminals[n as usize])?;
273 self.output.push_str(&snip);
280 // The boolean part of the return indicates whether some simplifications have been applied.
281 fn suggest(cx: &LateContext<'_, '_>, suggestion: &Bool, terminals: &[&Expr]) -> (String, bool) {
282 let mut suggest_context = SuggestContext {
285 output: String::new(),
288 suggest_context.recurse(suggestion);
289 (suggest_context.output, suggest_context.simplified)
292 fn simple_negate(b: Bool) -> Bool {
293 use quine_mc_cluskey::Bool::*;
297 t @ Term(_) => Not(Box::new(t)),
300 *el = simple_negate(::std::mem::replace(el, True));
306 *el = simple_negate(::std::mem::replace(el, True));
310 Not(inner) => *inner,
316 terminals: [usize; 32],
321 fn terminal_stats(b: &Bool) -> Stats {
322 fn recurse(b: &Bool, stats: &mut Stats) {
324 True | False => stats.ops += 1,
327 And(_) | Or(_) => stats.ops += 1, // brackets are also operations
328 _ => stats.negations += 1,
330 recurse(inner, stats);
333 stats.ops += v.len() - 1;
335 recurse(inner, stats);
338 &Term(n) => stats.terminals[n as usize] += 1,
341 use quine_mc_cluskey::Bool::*;
342 let mut stats = Stats::default();
343 recurse(b, &mut stats);
347 impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
348 fn bool_expr(&self, e: &Expr) {
349 let mut h2q = Hir2Qmm {
350 terminals: Vec::new(),
353 if let Ok(expr) = h2q.run(e) {
354 if h2q.terminals.len() > 8 {
355 // QMC has exponentially slow behavior as the number of terminals increases
356 // 8 is reasonable, it takes approximately 0.2 seconds.
361 let stats = terminal_stats(&expr);
362 let mut simplified = expr.simplify();
363 for simple in Bool::Not(Box::new(expr.clone())).simplify() {
365 Bool::Not(_) | Bool::True | Bool::False => {},
366 _ => simplified.push(Bool::Not(Box::new(simple.clone()))),
368 let simple_negated = simple_negate(simple);
369 if simplified.iter().any(|s| *s == simple_negated) {
372 simplified.push(simple_negated);
374 let mut improvements = Vec::new();
375 'simplified: for suggestion in &simplified {
376 let simplified_stats = terminal_stats(suggestion);
377 let mut improvement = false;
379 // ignore any "simplifications" that end up requiring a terminal more often
380 // than in the original expression
381 if stats.terminals[i] < simplified_stats.terminals[i] {
382 continue 'simplified;
384 if stats.terminals[i] != 0 && simplified_stats.terminals[i] == 0 {
389 "this boolean expression contains a logic bug",
392 h2q.terminals[i].span,
393 "this expression can be optimized out by applying boolean operations to the \
398 "it would look like the following",
399 suggest(self.cx, suggestion, &h2q.terminals).0,
400 // nonminimal_bool can produce minimal but
401 // not human readable expressions (#3141)
402 Applicability::Unspecified,
406 // don't also lint `NONMINIMAL_BOOL`
409 // if the number of occurrences of a terminal decreases or any of the stats
410 // decreases while none increases
411 improvement |= (stats.terminals[i] > simplified_stats.terminals[i])
412 || (stats.negations > simplified_stats.negations && stats.ops == simplified_stats.ops)
413 || (stats.ops > simplified_stats.ops && stats.negations == simplified_stats.negations);
416 improvements.push(suggestion);
419 let nonminimal_bool_lint = |suggestions: Vec<_>| {
424 "this boolean expression can be simplified",
429 suggestions.into_iter(),
430 // nonminimal_bool can produce minimal but
431 // not human readable expressions (#3141)
432 Applicability::Unspecified,
437 if improvements.is_empty() {
438 let suggest = suggest(self.cx, &expr, &h2q.terminals);
440 nonminimal_bool_lint(vec![suggest.0])
443 nonminimal_bool_lint(
446 .map(|suggestion| suggest(self.cx, suggestion, &h2q.terminals).0)
454 impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
455 fn visit_expr(&mut self, e: &'tcx Expr) {
456 if in_macro(e.span) {
460 ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => {
463 ExprKind::Unary(UnNot, inner) => {
464 if self.cx.tables.node_types()[inner.hir_id].is_bool() {
470 _ => walk_expr(self, e),
473 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
474 NestedVisitorMap::None
478 fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool {
479 let ty = cx.tables.expr_ty(expr);
480 get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]))