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 suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
fn recurse(brackets: bool, cx: &LateContext, suggestion: &Bool, terminals: &[&Expr], mut s: String) -> String { fn recurse(brackets: bool, cx: &LateContext, suggestion: &Bool, terminals: &[&Expr], mut s: String) -> String {
use quine_mc_cluskey::Bool::*; use quine_mc_cluskey::Bool::*;
@ -143,16 +152,16 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
}, },
Not(ref inner) => { Not(ref inner) => {
s.push('!'); s.push('!');
recurse(true, cx, inner, terminals, s) recurse(brackets!(**inner => And, Or, Term), cx, inner, terminals, s)
}, },
And(ref v) => { And(ref v) => {
if brackets { if brackets {
s.push('('); 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..] { for inner in &v[1..] {
s.push_str(" && "); s.push_str(" && ");
s = recurse(true, cx, inner, terminals, s); s = recurse(brackets!(*inner => Or), cx, inner, terminals, s);
} }
if brackets { if brackets {
s.push(')'); s.push(')');
@ -163,10 +172,10 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
if brackets { if brackets {
s.push('('); s.push('(');
} }
s = recurse(true, cx, &v[0], terminals, s); s = recurse(false, cx, &v[0], terminals, s);
for inner in &v[1..] { for inner in &v[1..] {
s.push_str(" || "); s.push_str(" || ");
s = recurse(true, cx, inner, terminals, s); s = recurse(false, cx, inner, terminals, s);
} }
if brackets { if brackets {
s.push(')'); s.push(')');
@ -174,7 +183,17 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
s s
}, },
Term(n) => { 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")); 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 s
} }
} }

View File

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