2017-09-05 04:33:04 -05:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2016-04-07 10:46:48 -05:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::hir::intravisit::*;
|
2017-09-05 04:33:04 -05:00
|
|
|
use syntax::ast::{LitKind, NodeId, DUMMY_NODE_ID};
|
|
|
|
use syntax::codemap::{dummy_spanned, Span, DUMMY_SP};
|
2016-06-28 08:54:23 -05:00
|
|
|
use syntax::util::ThinVec;
|
2018-06-01 08:13:53 -05:00
|
|
|
use crate::utils::{in_macro, paths, match_type, snippet_opt, span_lint_and_then, SpanlessEq, get_trait_def_id, implements_trait};
|
2016-03-23 06:19:13 -05:00
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for boolean expressions that can be written more
|
|
|
|
/// concisely.
|
2016-03-23 06:19:13 -05:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** Readability of boolean expressions suffers from
|
|
|
|
/// unnecessary duplication.
|
2016-03-23 06:19:13 -05:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Known problems:** Ignores short circuiting behavior of `||` and
|
|
|
|
/// `&&`. Ignores `|`, `&` and `^`.
|
2016-03-23 06:19:13 -05:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// if a && true // should be: if a
|
|
|
|
/// if !(a == b) // should be: if a != b
|
2016-08-08 06:34:21 -05:00
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 03:18:36 -05:00
|
|
|
pub NONMINIMAL_BOOL,
|
2018-03-28 08:24:26 -05:00
|
|
|
complexity,
|
2016-08-06 03:18:36 -05:00
|
|
|
"boolean expressions that can be written more concisely"
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for boolean expressions that contain terminals that
|
|
|
|
/// can be eliminated.
|
2016-03-24 04:45:24 -05:00
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Why is this bad?** This is most likely a logic bug.
|
2016-03-24 04:45:24 -05:00
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Known problems:** Ignores short circuiting behavior.
|
2016-03-24 04:45:24 -05:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// if a && b || a { ... }
|
|
|
|
/// ```
|
|
|
|
/// The `b` is unnecessary, the expression is equivalent to `if a`.
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-08-06 03:18:36 -05:00
|
|
|
pub LOGIC_BUG,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2016-08-06 03:18:36 -05:00
|
|
|
"boolean expressions that contain terminals which can be eliminated"
|
2016-03-24 04:45:24 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 13:55:15 -06:00
|
|
|
// For each pairs, both orders are considered.
|
|
|
|
const METHODS_WITH_NEGATION: [(&str, &str); 2] = [
|
2017-11-07 15:43:24 -06:00
|
|
|
("is_some", "is_none"),
|
|
|
|
("is_err", "is_ok"),
|
|
|
|
];
|
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2016-03-23 06:19:13 -05:00
|
|
|
pub struct NonminimalBool;
|
|
|
|
|
|
|
|
impl LintPass for NonminimalBool {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-03-24 04:45:24 -05:00
|
|
|
lint_array!(NONMINIMAL_BOOL, LOGIC_BUG)
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonminimalBool {
|
2017-01-13 10:04:56 -06:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
_: intravisit::FnKind<'tcx>,
|
|
|
|
_: &'tcx FnDecl,
|
|
|
|
body: &'tcx Body,
|
|
|
|
_: Span,
|
2017-08-09 02:30:56 -05:00
|
|
|
_: NodeId,
|
2017-01-13 10:04:56 -06:00
|
|
|
) {
|
2018-03-15 10:07:15 -05:00
|
|
|
NonminimalBoolVisitor { cx }.visit_body(body)
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:32:21 -06:00
|
|
|
struct NonminimalBoolVisitor<'a, 'tcx: 'a> {
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
|
|
|
|
use quine_mc_cluskey::Bool;
|
2016-03-23 08:50:08 -05:00
|
|
|
struct Hir2Qmm<'a, 'tcx: 'a, 'v> {
|
|
|
|
terminals: Vec<&'v Expr>,
|
2016-04-14 13:14:03 -05:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2016-03-23 08:50:08 -05:00
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
|
2016-03-23 08:50:08 -05:00
|
|
|
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
|
|
|
|
fn extract(&mut self, op: BinOp_, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
|
2016-03-23 06:19:13 -05:00
|
|
|
for a in a {
|
|
|
|
if let ExprBinary(binop, ref lhs, ref rhs) = a.node {
|
|
|
|
if binop.node == op {
|
|
|
|
v = self.extract(op, &[lhs, rhs], v)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.push(self.run(a)?);
|
|
|
|
}
|
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
|
2016-03-23 08:50:08 -05:00
|
|
|
fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
|
2016-03-24 03:37:16 -05:00
|
|
|
// prevent folding of `cfg!` macros and the like
|
2017-03-31 17:14:04 -05:00
|
|
|
if !in_macro(e.span) {
|
2016-03-24 03:37:16 -05:00
|
|
|
match e.node {
|
|
|
|
ExprUnary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)),
|
2017-09-05 04:33:04 -05:00
|
|
|
ExprBinary(binop, ref lhs, ref rhs) => match binop.node {
|
|
|
|
BiOr => return Ok(Bool::Or(self.extract(BiOr, &[lhs, rhs], Vec::new())?)),
|
|
|
|
BiAnd => return Ok(Bool::And(self.extract(BiAnd, &[lhs, rhs], Vec::new())?)),
|
|
|
|
_ => (),
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2017-09-05 04:33:04 -05:00
|
|
|
ExprLit(ref lit) => match lit.node {
|
|
|
|
LitKind::Bool(true) => return Ok(Bool::True),
|
|
|
|
LitKind::Bool(false) => return Ok(Bool::False),
|
|
|
|
_ => (),
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-04-14 13:14:03 -05:00
|
|
|
_ => (),
|
2016-03-24 03:37:16 -05:00
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2016-03-24 10:02:26 -05:00
|
|
|
for (n, expr) in self.terminals.iter().enumerate() {
|
|
|
|
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e, expr) {
|
2017-09-05 04:33:04 -05:00
|
|
|
#[allow(cast_possible_truncation)]
|
|
|
|
return Ok(Bool::Term(n as u8));
|
2016-03-24 10:02:26 -05:00
|
|
|
}
|
2016-03-24 10:29:20 -05:00
|
|
|
let negated = match e.node {
|
|
|
|
ExprBinary(binop, ref lhs, ref rhs) => {
|
2018-06-01 08:13:53 -05:00
|
|
|
|
2018-06-03 11:46:11 -05:00
|
|
|
if !implements_ord(self.cx, lhs) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-01 08:13:53 -05:00
|
|
|
|
2016-04-14 13:14:03 -05:00
|
|
|
let mk_expr = |op| {
|
|
|
|
Expr {
|
|
|
|
id: DUMMY_NODE_ID,
|
2017-08-15 04:10:49 -05:00
|
|
|
hir_id: DUMMY_HIR_ID,
|
2016-04-14 13:14:03 -05:00
|
|
|
span: DUMMY_SP,
|
2016-06-28 08:54:23 -05:00
|
|
|
attrs: ThinVec::new(),
|
2016-04-14 13:14:03 -05:00
|
|
|
node: ExprBinary(dummy_spanned(op), lhs.clone(), rhs.clone()),
|
|
|
|
}
|
2016-03-24 10:29:20 -05:00
|
|
|
};
|
|
|
|
match binop.node {
|
|
|
|
BiEq => mk_expr(BiNe),
|
|
|
|
BiNe => mk_expr(BiEq),
|
|
|
|
BiGt => mk_expr(BiLe),
|
|
|
|
BiGe => mk_expr(BiLt),
|
|
|
|
BiLt => mk_expr(BiGe),
|
|
|
|
BiLe => mk_expr(BiGt),
|
|
|
|
_ => continue,
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-24 10:29:20 -05:00
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(&negated, expr) {
|
2017-09-05 04:33:04 -05:00
|
|
|
#[allow(cast_possible_truncation)]
|
|
|
|
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
|
2016-03-24 10:29:20 -05:00
|
|
|
}
|
2016-03-23 08:50:08 -05:00
|
|
|
}
|
|
|
|
let n = self.terminals.len();
|
|
|
|
self.terminals.push(e);
|
2016-03-23 06:19:13 -05:00
|
|
|
if n < 32 {
|
2017-09-05 04:33:04 -05:00
|
|
|
#[allow(cast_possible_truncation)]
|
|
|
|
Ok(Bool::Term(n as u8))
|
2016-03-23 06:19:13 -05:00
|
|
|
} else {
|
|
|
|
Err("too many literals".to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:18:32 -06:00
|
|
|
struct SuggestContext<'a, 'tcx: 'a, 'v> {
|
|
|
|
terminals: &'v [&'v Expr],
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
output: String,
|
|
|
|
simplified: bool,
|
2017-11-16 15:08:08 -06:00
|
|
|
}
|
|
|
|
|
2017-11-17 15:18:32 -06:00
|
|
|
impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
2017-11-29 09:03:05 -06:00
|
|
|
fn snip(&self, e: &Expr) -> Option<String> {
|
|
|
|
snippet_opt(self.cx, e.span)
|
2017-11-17 15:18:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn simplify_not(&self, expr: &Expr) -> Option<String> {
|
|
|
|
match expr.node {
|
|
|
|
ExprBinary(binop, ref lhs, ref rhs) => {
|
2018-06-01 08:13:53 -05:00
|
|
|
|
2018-06-03 11:46:11 -05:00
|
|
|
if !implements_ord(self.cx, lhs) {
|
|
|
|
return None;
|
|
|
|
}
|
2018-06-01 08:13:53 -05:00
|
|
|
|
2017-11-17 15:18:32 -06:00
|
|
|
match binop.node {
|
|
|
|
BiEq => Some(" != "),
|
|
|
|
BiNe => Some(" == "),
|
|
|
|
BiLt => Some(" >= "),
|
|
|
|
BiGt => Some(" <= "),
|
|
|
|
BiLe => Some(" > "),
|
|
|
|
BiGe => Some(" < "),
|
|
|
|
_ => None,
|
2017-11-29 09:03:05 -06:00
|
|
|
}.and_then(|op| Some(format!("{}{}{}", self.snip(lhs)?, op, self.snip(rhs)?)))
|
2017-11-17 15:18:32 -06:00
|
|
|
},
|
|
|
|
ExprMethodCall(ref path, _, ref args) if args.len() == 1 => {
|
2018-04-07 05:52:18 -05:00
|
|
|
let type_of_receiver = self.cx.tables.expr_ty(&args[0]);
|
|
|
|
if !match_type(self.cx, type_of_receiver, &paths::OPTION) &&
|
|
|
|
!match_type(self.cx, type_of_receiver, &paths::RESULT) {
|
|
|
|
return None;
|
|
|
|
}
|
2017-11-17 15:18:32 -06:00
|
|
|
METHODS_WITH_NEGATION
|
|
|
|
.iter().cloned()
|
|
|
|
.flat_map(|(a, b)| vec![(a, b), (b, a)])
|
2018-06-28 08:46:58 -05:00
|
|
|
.find(|&(a, _)| a == path.ident.as_str())
|
2017-11-29 09:03:05 -06:00
|
|
|
.and_then(|(_, neg_method)| Some(format!("{}.{}()", self.snip(&args[0])?, neg_method)))
|
2017-11-17 15:18:32 -06:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:03:05 -06:00
|
|
|
fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
|
2016-03-23 06:19:13 -05:00
|
|
|
use quine_mc_cluskey::Bool::*;
|
|
|
|
match *suggestion {
|
|
|
|
True => {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push_str("true");
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:19:13 -05:00
|
|
|
False => {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push_str("false");
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2017-09-05 04:33:04 -05:00
|
|
|
Not(ref inner) => match **inner {
|
|
|
|
And(_) | Or(_) => {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push('!');
|
2017-11-17 15:42:25 -06:00
|
|
|
self.output.push('(');
|
|
|
|
self.recurse(inner);
|
|
|
|
self.output.push(')');
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2017-11-16 15:08:08 -06:00
|
|
|
Term(n) => {
|
2017-11-17 15:42:25 -06:00
|
|
|
let terminal = self.terminals[n as usize];
|
|
|
|
if let Some(str) = self.simplify_not(terminal) {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.simplified = true;
|
|
|
|
self.output.push_str(&str)
|
2017-11-16 15:08:08 -06:00
|
|
|
} else {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push('!');
|
2017-11-29 09:03:05 -06:00
|
|
|
let snip = self.snip(terminal)?;
|
2017-11-17 15:52:11 -06:00
|
|
|
self.output.push_str(&snip);
|
2017-11-16 15:08:08 -06:00
|
|
|
}
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2017-11-16 15:08:08 -06:00
|
|
|
True | False | Not(_) => {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push('!');
|
2017-11-29 09:03:05 -06:00
|
|
|
self.recurse(inner)?;
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:19:13 -05:00
|
|
|
And(ref v) => {
|
2017-11-16 15:08:08 -06:00
|
|
|
for (index, inner) in v.iter().enumerate() {
|
|
|
|
if index > 0 {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push_str(" && ");
|
2017-11-16 15:08:08 -06:00
|
|
|
}
|
2016-03-29 10:18:47 -05:00
|
|
|
if let Or(_) = *inner {
|
2017-11-17 15:42:25 -06:00
|
|
|
self.output.push('(');
|
|
|
|
self.recurse(inner);
|
|
|
|
self.output.push(')');
|
2016-03-29 10:18:47 -05:00
|
|
|
} else {
|
2017-11-17 15:42:25 -06:00
|
|
|
self.recurse(inner);
|
2016-03-29 10:18:47 -05:00
|
|
|
}
|
2016-03-23 06:49:16 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:19:13 -05:00
|
|
|
Or(ref v) => {
|
2017-11-16 15:08:08 -06:00
|
|
|
for (index, inner) in v.iter().enumerate() {
|
|
|
|
if index > 0 {
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push_str(" || ");
|
2017-11-16 15:08:08 -06:00
|
|
|
}
|
2017-11-17 15:42:25 -06:00
|
|
|
self.recurse(inner);
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:19:13 -05:00
|
|
|
Term(n) => {
|
2017-11-29 09:03:05 -06:00
|
|
|
let snip = self.snip(self.terminals[n as usize])?;
|
2017-11-17 15:18:32 -06:00
|
|
|
self.output.push_str(&snip);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2017-11-29 09:03:05 -06:00
|
|
|
Some(())
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2017-11-17 15:18:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The boolean part of the return indicates whether some simplifications have been applied.
|
|
|
|
fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> (String, bool) {
|
|
|
|
let mut suggest_context = SuggestContext {
|
2018-03-15 10:07:15 -05:00
|
|
|
terminals,
|
|
|
|
cx,
|
2017-11-17 15:18:32 -06:00
|
|
|
output: String::new(),
|
|
|
|
simplified: false,
|
|
|
|
};
|
2017-11-17 15:42:25 -06:00
|
|
|
suggest_context.recurse(suggestion);
|
2017-11-17 15:18:32 -06:00
|
|
|
(suggest_context.output, suggest_context.simplified)
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 06:52:17 -05:00
|
|
|
fn simple_negate(b: Bool) -> Bool {
|
|
|
|
use quine_mc_cluskey::Bool::*;
|
|
|
|
match b {
|
|
|
|
True => False,
|
|
|
|
False => True,
|
|
|
|
t @ Term(_) => Not(Box::new(t)),
|
|
|
|
And(mut v) => {
|
|
|
|
for el in &mut v {
|
|
|
|
*el = simple_negate(::std::mem::replace(el, True));
|
|
|
|
}
|
|
|
|
Or(v)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:52:17 -05:00
|
|
|
Or(mut v) => {
|
|
|
|
for el in &mut v {
|
|
|
|
*el = simple_negate(::std::mem::replace(el, True));
|
|
|
|
}
|
|
|
|
And(v)
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:52:17 -05:00
|
|
|
Not(inner) => *inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 09:37:17 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Stats {
|
|
|
|
terminals: [usize; 32],
|
|
|
|
negations: usize,
|
|
|
|
ops: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn terminal_stats(b: &Bool) -> Stats {
|
|
|
|
fn recurse(b: &Bool, stats: &mut Stats) {
|
2016-03-24 04:45:24 -05:00
|
|
|
match *b {
|
2016-03-24 09:37:17 -05:00
|
|
|
True | False => stats.ops += 1,
|
|
|
|
Not(ref inner) => {
|
|
|
|
match **inner {
|
|
|
|
And(_) | Or(_) => stats.ops += 1, // brackets are also operations
|
|
|
|
_ => stats.negations += 1,
|
|
|
|
}
|
|
|
|
recurse(inner, stats);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-24 09:37:17 -05:00
|
|
|
And(ref v) | Or(ref v) => {
|
|
|
|
stats.ops += v.len() - 1;
|
|
|
|
for inner in v {
|
|
|
|
recurse(inner, stats);
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-24 09:37:17 -05:00
|
|
|
Term(n) => stats.terminals[n as usize] += 1,
|
2016-03-24 04:45:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
use quine_mc_cluskey::Bool::*;
|
2016-03-24 09:37:17 -05:00
|
|
|
let mut stats = Stats::default();
|
2016-03-24 04:45:24 -05:00
|
|
|
recurse(b, &mut stats);
|
|
|
|
stats
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:19:13 -05:00
|
|
|
impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
|
|
|
|
fn bool_expr(&self, e: &Expr) {
|
2016-03-23 08:50:08 -05:00
|
|
|
let mut h2q = Hir2Qmm {
|
|
|
|
terminals: Vec::new(),
|
2016-12-06 04:32:21 -06:00
|
|
|
cx: self.cx,
|
2016-03-23 08:50:08 -05:00
|
|
|
};
|
2016-03-23 06:19:13 -05:00
|
|
|
if let Ok(expr) = h2q.run(e) {
|
2016-04-01 10:54:26 -05:00
|
|
|
if h2q.terminals.len() > 8 {
|
|
|
|
// QMC has exponentially slow behavior as the number of terminals increases
|
|
|
|
// 8 is reasonable, it takes approximately 0.2 seconds.
|
|
|
|
// See #825
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 04:45:24 -05:00
|
|
|
let stats = terminal_stats(&expr);
|
2016-03-23 06:52:17 -05:00
|
|
|
let mut simplified = expr.simplify();
|
|
|
|
for simple in Bool::Not(Box::new(expr.clone())).simplify() {
|
2016-03-24 04:54:48 -05:00
|
|
|
match simple {
|
2016-12-20 11:21:30 -06:00
|
|
|
Bool::Not(_) | Bool::True | Bool::False => {},
|
2016-03-24 04:54:48 -05:00
|
|
|
_ => simplified.push(Bool::Not(Box::new(simple.clone()))),
|
|
|
|
}
|
2016-03-23 06:52:17 -05:00
|
|
|
let simple_negated = simple_negate(simple);
|
|
|
|
if simplified.iter().any(|s| *s == simple_negated) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
simplified.push(simple_negated);
|
|
|
|
}
|
2016-03-24 09:37:17 -05:00
|
|
|
let mut improvements = Vec::new();
|
|
|
|
'simplified: for suggestion in &simplified {
|
2016-04-26 10:05:39 -05:00
|
|
|
let simplified_stats = terminal_stats(suggestion);
|
2016-03-24 09:37:17 -05:00
|
|
|
let mut improvement = false;
|
|
|
|
for i in 0..32 {
|
2016-06-05 18:42:39 -05:00
|
|
|
// ignore any "simplifications" that end up requiring a terminal more often
|
|
|
|
// than in the original expression
|
2016-03-24 09:37:17 -05:00
|
|
|
if stats.terminals[i] < simplified_stats.terminals[i] {
|
|
|
|
continue 'simplified;
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2016-03-24 09:37:17 -05:00
|
|
|
if stats.terminals[i] != 0 && simplified_stats.terminals[i] == 0 {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint_and_then(
|
|
|
|
self.cx,
|
|
|
|
LOGIC_BUG,
|
|
|
|
e.span,
|
|
|
|
"this boolean expression contains a logic bug",
|
|
|
|
|db| {
|
|
|
|
db.span_help(
|
|
|
|
h2q.terminals[i].span,
|
|
|
|
"this expression can be optimized out by applying boolean operations to the \
|
2017-09-05 04:33:04 -05:00
|
|
|
outer expression",
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
|
|
|
db.span_suggestion(
|
|
|
|
e.span,
|
|
|
|
"it would look like the following",
|
2017-11-14 15:14:08 -06:00
|
|
|
suggest(self.cx, suggestion, &h2q.terminals).0,
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2016-03-24 09:37:17 -05:00
|
|
|
// don't also lint `NONMINIMAL_BOOL`
|
|
|
|
return;
|
2016-03-24 04:45:24 -05:00
|
|
|
}
|
2016-06-05 18:42:39 -05:00
|
|
|
// if the number of occurrences of a terminal decreases or any of the stats
|
|
|
|
// decreases while none increases
|
2017-11-04 14:55:56 -05:00
|
|
|
improvement |= (stats.terminals[i] > simplified_stats.terminals[i])
|
|
|
|
|| (stats.negations > simplified_stats.negations && stats.ops == simplified_stats.ops)
|
|
|
|
|| (stats.ops > simplified_stats.ops && stats.negations == simplified_stats.negations);
|
2016-03-24 04:45:24 -05:00
|
|
|
}
|
2016-03-24 09:37:17 -05:00
|
|
|
if improvement {
|
|
|
|
improvements.push(suggestion);
|
2016-03-24 04:45:24 -05:00
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2017-11-14 15:14:08 -06:00
|
|
|
let nonminimal_bool_lint = |suggestions| {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint_and_then(
|
|
|
|
self.cx,
|
|
|
|
NONMINIMAL_BOOL,
|
|
|
|
e.span,
|
|
|
|
"this boolean expression can be simplified",
|
2017-11-14 15:14:08 -06:00
|
|
|
|db| { db.span_suggestions(e.span, "try", suggestions); },
|
|
|
|
);
|
|
|
|
};
|
|
|
|
if improvements.is_empty() {
|
|
|
|
let suggest = suggest(self.cx, &expr, &h2q.terminals);
|
|
|
|
if suggest.1 {
|
|
|
|
nonminimal_bool_lint(vec![suggest.0])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonminimal_bool_lint(
|
|
|
|
improvements
|
|
|
|
.into_iter()
|
|
|
|
.map(|suggestion| suggest(self.cx, suggestion, &h2q.terminals).0)
|
|
|
|
.collect()
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-03-24 09:37:17 -05:00
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:32:21 -06:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, e: &'tcx Expr) {
|
2017-03-31 17:14:04 -05:00
|
|
|
if in_macro(e.span) {
|
2016-04-14 13:14:03 -05:00
|
|
|
return;
|
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
match e.node {
|
|
|
|
ExprBinary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e),
|
2017-09-05 04:33:04 -05:00
|
|
|
ExprUnary(UnNot, ref inner) => if self.cx.tables.node_types()[inner.hir_id].is_bool() {
|
|
|
|
self.bool_expr(e);
|
|
|
|
} else {
|
|
|
|
walk_expr(self, e);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-03-23 06:19:13 -05:00
|
|
|
_ => walk_expr(self, e),
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 04:32:21 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2017-01-13 10:04:56 -06:00
|
|
|
NestedVisitorMap::None
|
2016-12-06 04:32:21 -06:00
|
|
|
}
|
2016-03-23 06:19:13 -05:00
|
|
|
}
|
2018-06-01 08:13:53 -05:00
|
|
|
|
|
|
|
|
2018-06-03 11:46:11 -05:00
|
|
|
fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool {
|
2018-06-01 08:13:53 -05:00
|
|
|
let ty = cx.tables.expr_ty(expr);
|
2018-06-03 11:46:11 -05:00
|
|
|
get_trait_def_id(cx, &paths::ORD)
|
|
|
|
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
|
2018-06-01 08:13:53 -05:00
|
|
|
}
|