2016-03-24 14:36:50 +00:00
|
|
|
use rustc::lint::{LintArray, LateLintPass, LateContext, LintPass};
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::hir::intravisit::*;
|
2016-03-24 15:29:20 +00:00
|
|
|
use syntax::ast::{LitKind, DUMMY_NODE_ID};
|
|
|
|
use syntax::codemap::{DUMMY_SP, dummy_spanned};
|
2016-03-23 13:50:47 +00:00
|
|
|
use utils::{span_lint_and_then, in_macro, snippet_opt, SpanlessEq};
|
2016-03-23 11:19:13 +00:00
|
|
|
|
|
|
|
/// **What it does:** This lint checks for boolean expressions that can be written more concisely
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Readability of boolean expressions suffers from unnecesessary duplication
|
|
|
|
///
|
2016-03-30 10:55:59 +00:00
|
|
|
/// **Known problems:** Ignores short circuting behavior of `||` and `&&`. Ignores `|`, `&` and `^`.
|
2016-03-23 11:19:13 +00:00
|
|
|
///
|
2016-03-30 10:55:59 +00:00
|
|
|
/// **Example:** `if a && true` should be `if a` and `!(a == b)` should be `a != b`
|
2016-03-23 11:19:13 +00:00
|
|
|
declare_lint! {
|
2016-03-30 10:55:59 +00:00
|
|
|
pub NONMINIMAL_BOOL, Allow,
|
2016-03-23 11:19:13 +00:00
|
|
|
"checks for boolean expressions that can be written more concisely"
|
|
|
|
}
|
|
|
|
|
2016-03-24 09:45:24 +00:00
|
|
|
/// **What it does:** This lint checks for boolean expressions that contain terminals that can be eliminated
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** This is most likely a logic bug
|
|
|
|
///
|
|
|
|
/// **Known problems:** Ignores short circuiting behavior
|
|
|
|
///
|
|
|
|
/// **Example:** The `b` in `if a && b || a` is unnecessary because the expression is equivalent to `if a`
|
|
|
|
declare_lint! {
|
|
|
|
pub LOGIC_BUG, Warn,
|
|
|
|
"checks for boolean expressions that contain terminals which can be eliminated"
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:19:13 +00:00
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct NonminimalBool;
|
|
|
|
|
|
|
|
impl LintPass for NonminimalBool {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-03-24 09:45:24 +00:00
|
|
|
lint_array!(NONMINIMAL_BOOL, LOGIC_BUG)
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass for NonminimalBool {
|
2016-03-23 13:45:51 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext, item: &Item) {
|
|
|
|
NonminimalBoolVisitor(cx).visit_item(item)
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NonminimalBoolVisitor<'a, 'tcx: 'a>(&'a LateContext<'a, 'tcx>);
|
|
|
|
|
|
|
|
use quine_mc_cluskey::Bool;
|
2016-03-23 13:50:08 +00:00
|
|
|
struct Hir2Qmm<'a, 'tcx: 'a, 'v> {
|
|
|
|
terminals: Vec<&'v Expr>,
|
2016-04-14 18:14:03 +00:00
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2016-03-23 13:50:08 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
|
2016-03-23 13:50:08 +00: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 11:19:13 +00: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 13:50:08 +00:00
|
|
|
fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
|
2016-03-24 08:37:16 +00:00
|
|
|
// prevent folding of `cfg!` macros and the like
|
|
|
|
if !in_macro(self.cx, e.span) {
|
|
|
|
match e.node {
|
|
|
|
ExprUnary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)),
|
|
|
|
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-04-14 18:14:03 +00:00
|
|
|
_ => (),
|
2016-03-24 08:37:16 +00:00
|
|
|
}
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-24 08:37:16 +00:00
|
|
|
ExprLit(ref lit) => {
|
|
|
|
match lit.node {
|
|
|
|
LitKind::Bool(true) => return Ok(Bool::True),
|
|
|
|
LitKind::Bool(false) => return Ok(Bool::False),
|
2016-04-14 18:14:03 +00:00
|
|
|
_ => (),
|
2016-03-24 08:37:16 +00:00
|
|
|
}
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
2016-03-24 08:37:16 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
2016-03-24 15:02:26 +00:00
|
|
|
for (n, expr) in self.terminals.iter().enumerate() {
|
|
|
|
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e, expr) {
|
|
|
|
#[allow(cast_possible_truncation)]
|
|
|
|
return Ok(Bool::Term(n as u8));
|
|
|
|
}
|
2016-03-24 15:29:20 +00:00
|
|
|
let negated = match e.node {
|
|
|
|
ExprBinary(binop, ref lhs, ref rhs) => {
|
2016-04-14 18:14:03 +00:00
|
|
|
let mk_expr = |op| {
|
|
|
|
Expr {
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
span: DUMMY_SP,
|
|
|
|
attrs: None,
|
|
|
|
node: ExprBinary(dummy_spanned(op), lhs.clone(), rhs.clone()),
|
|
|
|
}
|
2016-03-24 15:29:20 +00: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-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-24 15:29:20 +00:00
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(&negated, expr) {
|
|
|
|
#[allow(cast_possible_truncation)]
|
|
|
|
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
|
|
|
|
}
|
2016-03-23 13:50:08 +00:00
|
|
|
}
|
|
|
|
let n = self.terminals.len();
|
|
|
|
self.terminals.push(e);
|
2016-03-23 11:19:13 +00:00
|
|
|
if n < 32 {
|
|
|
|
#[allow(cast_possible_truncation)]
|
|
|
|
Ok(Bool::Term(n as u8))
|
|
|
|
} else {
|
|
|
|
Err("too many literals".to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
|
2016-03-23 11:49:16 +00:00
|
|
|
fn recurse(brackets: bool, cx: &LateContext, suggestion: &Bool, terminals: &[&Expr], mut s: String) -> String {
|
2016-03-23 11:19:13 +00:00
|
|
|
use quine_mc_cluskey::Bool::*;
|
2016-03-29 15:18:47 +00:00
|
|
|
let snip = |e: &Expr| snippet_opt(cx, e.span).expect("don't try to improve booleans created by macros");
|
2016-03-23 11:19:13 +00:00
|
|
|
match *suggestion {
|
|
|
|
True => {
|
2016-03-24 14:36:50 +00:00
|
|
|
s.push_str("true");
|
2016-03-23 11:19:13 +00:00
|
|
|
s
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
False => {
|
2016-03-24 14:36:50 +00:00
|
|
|
s.push_str("false");
|
2016-03-23 11:19:13 +00:00
|
|
|
s
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
Not(ref inner) => {
|
2016-03-29 15:18:47 +00:00
|
|
|
match **inner {
|
|
|
|
And(_) | Or(_) => {
|
|
|
|
s.push('!');
|
|
|
|
recurse(true, cx, inner, terminals, s)
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-29 15:18:47 +00:00
|
|
|
Term(n) => {
|
2016-03-29 15:20:30 +00:00
|
|
|
if let ExprBinary(binop, ref lhs, ref rhs) = terminals[n as usize].node {
|
|
|
|
let op = match binop.node {
|
|
|
|
BiEq => " != ",
|
|
|
|
BiNe => " == ",
|
|
|
|
BiLt => " >= ",
|
|
|
|
BiGt => " <= ",
|
|
|
|
BiLe => " > ",
|
|
|
|
BiGe => " < ",
|
|
|
|
_ => {
|
|
|
|
s.push('!');
|
2016-04-14 18:14:03 +00:00
|
|
|
return recurse(true, cx, inner, terminals, s);
|
|
|
|
}
|
2016-03-29 15:20:30 +00:00
|
|
|
};
|
|
|
|
s.push_str(&snip(lhs));
|
|
|
|
s.push_str(op);
|
|
|
|
s.push_str(&snip(rhs));
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
s.push('!');
|
|
|
|
recurse(false, cx, inner, terminals, s)
|
2016-03-29 15:18:47 +00:00
|
|
|
}
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-29 15:18:47 +00:00
|
|
|
_ => {
|
|
|
|
s.push('!');
|
|
|
|
recurse(false, cx, inner, terminals, s)
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-29 15:18:47 +00:00
|
|
|
}
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
And(ref v) => {
|
2016-03-23 11:49:16 +00:00
|
|
|
if brackets {
|
|
|
|
s.push('(');
|
|
|
|
}
|
2016-03-29 15:18:47 +00:00
|
|
|
if let Or(_) = v[0] {
|
|
|
|
s = recurse(true, cx, &v[0], terminals, s);
|
|
|
|
} else {
|
|
|
|
s = recurse(false, cx, &v[0], terminals, s);
|
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
for inner in &v[1..] {
|
2016-03-24 14:36:50 +00:00
|
|
|
s.push_str(" && ");
|
2016-03-29 15:18:47 +00:00
|
|
|
if let Or(_) = *inner {
|
|
|
|
s = recurse(true, cx, inner, terminals, s);
|
|
|
|
} else {
|
|
|
|
s = recurse(false, cx, inner, terminals, s);
|
|
|
|
}
|
2016-03-23 11:49:16 +00:00
|
|
|
}
|
|
|
|
if brackets {
|
|
|
|
s.push(')');
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
|
|
|
s
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
Or(ref v) => {
|
2016-03-23 11:49:16 +00:00
|
|
|
if brackets {
|
|
|
|
s.push('(');
|
|
|
|
}
|
2016-03-29 14:55:38 +00:00
|
|
|
s = recurse(false, cx, &v[0], terminals, s);
|
2016-03-23 11:19:13 +00:00
|
|
|
for inner in &v[1..] {
|
2016-03-24 14:36:50 +00:00
|
|
|
s.push_str(" || ");
|
2016-03-29 14:55:38 +00:00
|
|
|
s = recurse(false, cx, inner, terminals, s);
|
2016-03-23 11:49:16 +00:00
|
|
|
}
|
|
|
|
if brackets {
|
|
|
|
s.push(')');
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
|
|
|
s
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
Term(n) => {
|
2016-03-29 14:55:38 +00:00
|
|
|
if brackets {
|
|
|
|
if let ExprBinary(..) = terminals[n as usize].node {
|
|
|
|
s.push('(');
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 15:18:47 +00:00
|
|
|
s.push_str(&snip(&terminals[n as usize]));
|
2016-03-29 14:55:38 +00:00
|
|
|
if brackets {
|
|
|
|
if let ExprBinary(..) = terminals[n as usize].node {
|
|
|
|
s.push(')');
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 11:49:16 +00:00
|
|
|
recurse(false, cx, suggestion, terminals, String::new())
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 11:52:17 +00: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-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:52:17 +00:00
|
|
|
Or(mut v) => {
|
|
|
|
for el in &mut v {
|
|
|
|
*el = simple_negate(::std::mem::replace(el, True));
|
|
|
|
}
|
|
|
|
And(v)
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:52:17 +00:00
|
|
|
Not(inner) => *inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:37:17 +00: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 09:45:24 +00:00
|
|
|
match *b {
|
2016-03-24 14:37:17 +00: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-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
And(ref v) | Or(ref v) => {
|
|
|
|
stats.ops += v.len() - 1;
|
|
|
|
for inner in v {
|
|
|
|
recurse(inner, stats);
|
|
|
|
}
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
Term(n) => stats.terminals[n as usize] += 1,
|
2016-03-24 09:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
use quine_mc_cluskey::Bool::*;
|
2016-03-24 14:37:17 +00:00
|
|
|
let mut stats = Stats::default();
|
2016-03-24 09:45:24 +00:00
|
|
|
recurse(b, &mut stats);
|
|
|
|
stats
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:19:13 +00:00
|
|
|
impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
|
|
|
|
fn bool_expr(&self, e: &Expr) {
|
2016-03-23 13:50:08 +00:00
|
|
|
let mut h2q = Hir2Qmm {
|
|
|
|
terminals: Vec::new(),
|
|
|
|
cx: self.0,
|
|
|
|
};
|
2016-03-23 11:19:13 +00:00
|
|
|
if let Ok(expr) = h2q.run(e) {
|
2016-04-01 15:54:26 +00: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 09:45:24 +00:00
|
|
|
let stats = terminal_stats(&expr);
|
2016-03-23 11:52:17 +00:00
|
|
|
let mut simplified = expr.simplify();
|
|
|
|
for simple in Bool::Not(Box::new(expr.clone())).simplify() {
|
2016-03-24 09:54:48 +00:00
|
|
|
match simple {
|
2016-04-14 18:14:03 +00:00
|
|
|
Bool::Not(_) | Bool::True | Bool::False => {}
|
2016-03-24 09:54:48 +00:00
|
|
|
_ => simplified.push(Bool::Not(Box::new(simple.clone()))),
|
|
|
|
}
|
2016-03-23 11:52:17 +00:00
|
|
|
let simple_negated = simple_negate(simple);
|
|
|
|
if simplified.iter().any(|s| *s == simple_negated) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
simplified.push(simple_negated);
|
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
let mut improvements = Vec::new();
|
|
|
|
'simplified: for suggestion in &simplified {
|
|
|
|
let simplified_stats = terminal_stats(&suggestion);
|
|
|
|
let mut improvement = false;
|
|
|
|
for i in 0..32 {
|
|
|
|
// ignore any "simplifications" that end up requiring a terminal more often than in the original expression
|
|
|
|
if stats.terminals[i] < simplified_stats.terminals[i] {
|
|
|
|
continue 'simplified;
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
if stats.terminals[i] != 0 && simplified_stats.terminals[i] == 0 {
|
2016-04-14 18:14:03 +00:00
|
|
|
span_lint_and_then(self.0,
|
|
|
|
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 outer expression");
|
|
|
|
db.span_suggestion(e.span,
|
|
|
|
"it would look like the following",
|
|
|
|
suggest(self.0, suggestion, &h2q.terminals));
|
|
|
|
});
|
2016-03-24 14:37:17 +00:00
|
|
|
// don't also lint `NONMINIMAL_BOOL`
|
|
|
|
return;
|
2016-03-24 09:45:24 +00:00
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
// if the number of occurrences of a terminal decreases or any of the stats decreases while none increases
|
2016-03-29 14:27:06 +00:00
|
|
|
improvement |= (stats.terminals[i] > simplified_stats.terminals[i]) ||
|
2016-04-14 18:14:03 +00:00
|
|
|
(stats.negations > simplified_stats.negations &&
|
|
|
|
stats.ops == simplified_stats.ops) ||
|
|
|
|
(stats.ops > simplified_stats.ops && stats.negations == simplified_stats.negations);
|
2016-03-24 09:45:24 +00:00
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
if improvement {
|
|
|
|
improvements.push(suggestion);
|
2016-03-24 09:45:24 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
2016-03-24 14:37:17 +00:00
|
|
|
if !improvements.is_empty() {
|
2016-04-14 18:14:03 +00:00
|
|
|
span_lint_and_then(self.0,
|
|
|
|
NONMINIMAL_BOOL,
|
|
|
|
e.span,
|
|
|
|
"this boolean expression can be simplified",
|
|
|
|
|db| {
|
|
|
|
for suggestion in &improvements {
|
|
|
|
db.span_suggestion(e.span,
|
|
|
|
"try",
|
|
|
|
suggest(self.0, suggestion, &h2q.terminals));
|
|
|
|
}
|
|
|
|
});
|
2016-03-24 14:37:17 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'v, 'tcx> Visitor<'v> for NonminimalBoolVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, e: &'v Expr) {
|
2016-04-14 18:14:03 +00:00
|
|
|
if in_macro(self.0, e.span) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
match e.node {
|
|
|
|
ExprBinary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e),
|
|
|
|
ExprUnary(UnNot, ref inner) => {
|
|
|
|
if self.0.tcx.node_types()[&inner.id].is_bool() {
|
|
|
|
self.bool_expr(e);
|
|
|
|
} else {
|
|
|
|
walk_expr(self, e);
|
|
|
|
}
|
2016-04-14 18:14:03 +00:00
|
|
|
}
|
2016-03-23 11:19:13 +00:00
|
|
|
_ => walk_expr(self, e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|