From b05dd13f2c5199bc2e97a5b5e6910b1cde4be988 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 29 Mar 2016 16:55:38 +0200 Subject: [PATCH] added brackets and fixed compiler comments --- src/booleans.rs | 29 ++++++++++--- tests/compile-fail/booleans.rs | 77 ++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/src/booleans.rs b/src/booleans.rs index b603d5188aad..0cfab9a53571 100644 --- a/src/booleans.rs +++ b/src/booleans.rs @@ -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 } } diff --git a/tests/compile-fail/booleans.rs b/tests/compile-fail/booleans.rs index 008e6dcdda90..f4760a6fd465 100644 --- a/tests/compile-fail/booleans.rs +++ b/tests/compile-fail/booleans.rs @@ -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); }