added brackets and fixed compiler comments

This commit is contained in:
Oliver Schneider 2016-03-29 16:55:38 +02:00
parent 216edbae59
commit b05dd13f2c
2 changed files with 64 additions and 42 deletions

View file

@ -129,6 +129,15 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
}
}
macro_rules! brackets {
($val:expr => $($name:ident),*) => {
match $val {
$($name(_) => true,)*
_ => false,
}
}
}
fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
fn recurse(brackets: bool, cx: &LateContext, suggestion: &Bool, terminals: &[&Expr], mut s: String) -> String {
use quine_mc_cluskey::Bool::*;
@ -143,16 +152,16 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
},
Not(ref inner) => {
s.push('!');
recurse(true, cx, inner, terminals, s)
recurse(brackets!(**inner => And, Or, Term), cx, inner, terminals, s)
},
And(ref v) => {
if brackets {
s.push('(');
}
s = recurse(true, cx, &v[0], terminals, s);
s = recurse(brackets!(v[0] => Or), cx, &v[0], terminals, s);
for inner in &v[1..] {
s.push_str(" && ");
s = recurse(true, cx, inner, terminals, s);
s = recurse(brackets!(*inner => Or), cx, inner, terminals, s);
}
if brackets {
s.push(')');
@ -163,10 +172,10 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
if brackets {
s.push('(');
}
s = recurse(true, cx, &v[0], terminals, s);
s = recurse(false, cx, &v[0], terminals, s);
for inner in &v[1..] {
s.push_str(" || ");
s = recurse(true, cx, inner, terminals, s);
s = recurse(false, cx, inner, terminals, s);
}
if brackets {
s.push(')');
@ -174,7 +183,17 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
s
},
Term(n) => {
if brackets {
if let ExprBinary(..) = terminals[n as usize].node {
s.push('(');
}
}
s.push_str(&snippet_opt(cx, terminals[n as usize].span).expect("don't try to improve booleans created by macros"));
if brackets {
if let ExprBinary(..) = terminals[n as usize].node {
s.push(')');
}
}
s
}
}

View file

@ -10,30 +10,30 @@ fn main() {
let d: bool = unimplemented!();
let e: bool = unimplemented!();
let _ = a && b || a; //~ ERROR this boolean expression contains a logic bug
//|~ HELP for further information visit
//|~ HELP this expression can be optimized out
//|~ HELP it would look like the following
//|~ SUGGESTION let _ = a;
//~| HELP for further information visit
//~| HELP this expression can be optimized out
//~| HELP it would look like the following
//~| SUGGESTION let _ = a;
let _ = !(a && b);
let _ = !true; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = false;
//~| HELP for further information visit
//~| SUGGESTION let _ = false;
let _ = !false; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = true;
//~| HELP for further information visit
//~| SUGGESTION let _ = true;
let _ = !!a; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = a;
//~| HELP for further information visit
//~| SUGGESTION let _ = a;
let _ = false && a; //~ ERROR this boolean expression contains a logic bug
//|~ HELP for further information visit
//|~ HELP this expression can be optimized out
//|~ HELP it would look like the following
//|~ SUGGESTION let _ = false;
//~| HELP for further information visit
//~| HELP this expression can be optimized out
//~| HELP it would look like the following
//~| SUGGESTION let _ = false;
let _ = false || a; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = a;
//~| HELP for further information visit
//~| SUGGESTION let _ = a;
// don't lint on cfgs
let _ = cfg!(you_shall_not_not_pass) && a;
@ -43,8 +43,8 @@ fn main() {
let _ = !(a && b || c);
let _ = !(!a && b); //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = !b || a;
//~| HELP for further information visit
//~| SUGGESTION let _ = !b || a;
}
#[allow(unused, many_single_char_names)]
@ -55,30 +55,33 @@ fn equality_stuff() {
let d: i32 = unimplemented!();
let e: i32 = unimplemented!();
let _ = a == b && a != b; //~ ERROR this boolean expression contains a logic bug
//|~ HELP for further information visit
//|~ HELP this expression can be optimized out
//|~ HELP it would look like the following
//|~ SUGGESTION let _ = false;
//~| HELP for further information visit
//~| HELP this expression can be optimized out
//~| HELP it would look like the following
//~| SUGGESTION let _ = false;
let _ = a == b && c == 5 && a == b; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = c == 5 && a == b;
//~| HELP for further information visit
//~| SUGGESTION let _ = a == b && c == 5;
let _ = a == b && c == 5 && b == a; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = c == 5 && a == b;
//~| HELP for further information visit
//~| SUGGESTION let _ = a == b && c == 5;
//~| HELP try
//~| SUGGESTION let _ = !(!(c == 5) || !(a == b));
let _ = a < b && a >= b; //~ ERROR this boolean expression contains a logic bug
//|~ HELP for further information visit
//|~ HELP this expression can be optimized out
//|~ HELP it would look like the following
//|~ SUGGESTION let _ = false;
//~| HELP for further information visit
//~| HELP this expression can be optimized out
//~| HELP it would look like the following
//~| SUGGESTION let _ = false;
let _ = a > b && a <= b; //~ ERROR this boolean expression contains a logic bug
//|~ HELP for further information visit
//|~ HELP this expression can be optimized out
//|~ HELP it would look like the following
//|~ SUGGESTION let _ = false;
//~| HELP for further information visit
//~| HELP this expression can be optimized out
//~| HELP it would look like the following
//~| SUGGESTION let _ = false;
let _ = a > b && a == b;
let _ = a != b || !(a != b || c == d); //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = !c == d || a != b;
//|~ SUGGESTION let _ = !(!a != b && c == d);
//~| HELP for further information visit
//~| SUGGESTION let _ = !(c == d) || a != b;
//~| HELP try
//~| SUGGESTION let _ = !(!(a != b) && c == d);
}