From bdf3887d22a7352c20d97cef9cf9f8f4ac41ccff Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 14 Nov 2017 17:07:04 +0100 Subject: [PATCH] Move 'handle_method_call_in_not' code into 'suggest' --- clippy_lints/src/booleans.rs | 84 +++++++++++++---------------- tests/ui/booleans.rs | 2 + tests/ui/booleans.stderr | 24 ++------- tests/ui/format.stderr | 12 ----- tests/ui/needless_range_loop.stderr | 4 +- tests/ui/print_with_newline.stderr | 18 ------- 6 files changed, 45 insertions(+), 99 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index e1e8bce1f7d..5de3246f1a9 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -177,26 +177,45 @@ fn recurse(brackets: bool, cx: &LateContext, suggestion: &Bool, terminals: &[&Ex s.push('!'); recurse(true, cx, inner, terminals, s) }, - Term(n) => if let ExprBinary(binop, ref lhs, ref rhs) = terminals[n as usize].node { - let op = match binop.node { - BiEq => " != ", - BiNe => " == ", - BiLt => " >= ", - BiGt => " <= ", - BiLe => " > ", - BiGe => " < ", - _ => { + Term(n) => match terminals[n as usize].node { + ExprBinary(binop, ref lhs, ref rhs) => { + let op = match binop.node { + BiEq => " != ", + BiNe => " == ", + BiLt => " >= ", + BiGt => " <= ", + BiLe => " > ", + BiGe => " < ", + _ => { + s.push('!'); + return recurse(true, cx, inner, terminals, s); + }, + }; + s.push_str(&snip(lhs)); + s.push_str(op); + s.push_str(&snip(rhs)); + s + }, + ExprMethodCall(ref path, _, ref args) if args.len() == 1 => { + let negation = METHODS_WITH_NEGATION + .iter().cloned() + .flat_map(|(a, b)| vec![(a, b), (b, a)]) + .find(|&(a, _)| a == path.name.as_str()); + if let Some((_, negation_method)) = negation { + s.push_str(&snip(&args[0])); + s.push('.'); + s.push_str(negation_method); + s.push_str("()"); + s + } else { s.push('!'); - return recurse(true, cx, inner, terminals, s); - }, - }; - s.push_str(&snip(lhs)); - s.push_str(op); - s.push_str(&snip(rhs)); - s - } else { - s.push('!'); - recurse(false, cx, inner, terminals, s) + recurse(false, cx, inner, terminals, s) + } + }, + _ => { + s.push('!'); + recurse(false, cx, inner, terminals, s) + }, }, _ => { s.push('!'); @@ -402,32 +421,6 @@ fn bool_expr(&self, e: &Expr) { } } } - - fn handle_method_call_in_not(&mut self, e: &'tcx Expr, inner: &'tcx Expr) { - if let ExprMethodCall(ref path, _, ref args) = inner.node { - if args.len() == 1 { - METHODS_WITH_NEGATION.iter().for_each(|&(method1, method2)| { - for &(method, negation_method) in &[(method1, method2), (method2, method1)] { - if method == path.name.as_str() { - span_lint_and_then( - self.cx, - NONMINIMAL_BOOL, - e.span, - "this boolean expression can be simplified", - |db| { - db.span_suggestion( - e.span, - "try", - negation_method.to_owned() - ); - } - ) - } - } - }) - } - } - } } impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> { @@ -438,7 +431,6 @@ fn visit_expr(&mut self, e: &'tcx Expr) { match e.node { ExprBinary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e), ExprUnary(UnNot, ref inner) => if self.cx.tables.node_types()[inner.hir_id].is_bool() { - self.handle_method_call_in_not(e, inner); self.bool_expr(e); } else { walk_expr(self, e); diff --git a/tests/ui/booleans.rs b/tests/ui/booleans.rs index a3c37fecfdc..52ce90dd63d 100644 --- a/tests/ui/booleans.rs +++ b/tests/ui/booleans.rs @@ -51,4 +51,6 @@ fn methods_with_negation() { let _ = !b.is_err(); let _ = b.is_ok(); let _ = !b.is_ok(); + let c = false; + let _ = !(a.is_some() && !c); } diff --git a/tests/ui/booleans.stderr b/tests/ui/booleans.stderr index b7256ee0f3a..e50961323e5 100644 --- a/tests/ui/booleans.stderr +++ b/tests/ui/booleans.stderr @@ -131,26 +131,8 @@ help: try | ^^^^^^^^^^^^^^^^^^^ error: this boolean expression can be simplified - --> $DIR/booleans.rs:47:13 + --> $DIR/booleans.rs:55:13 | -47 | let _ = !a.is_some(); - | ^^^^^^^^^^^^ help: try: `is_none` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:49:13 - | -49 | let _ = !a.is_none(); - | ^^^^^^^^^^^^ help: try: `is_some` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:51:13 - | -51 | let _ = !b.is_err(); - | ^^^^^^^^^^^ help: try: `is_ok` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:53:13 - | -53 | let _ = !b.is_ok(); - | ^^^^^^^^^^ help: try: `is_err` +55 | let _ = !(a.is_some() && !c); + | ^^^^^^^^^^^^^^^^^^^^ help: try: `c || a.is_none()` diff --git a/tests/ui/format.stderr b/tests/ui/format.stderr index 67d97f295d8..558e9e83c33 100644 --- a/tests/ui/format.stderr +++ b/tests/ui/format.stderr @@ -6,15 +6,3 @@ error: useless use of `format!` | = note: `-D useless-format` implied by `-D warnings` -error: useless use of `format!` - --> $DIR/format.rs:8:5 - | -8 | format!("{}", "foo"); - | ^^^^^^^^^^^^^^^^^^^^^ - -error: useless use of `format!` - --> $DIR/format.rs:15:5 - | -15 | format!("{}", arg); - | ^^^^^^^^^^^^^^^^^^^ - diff --git a/tests/ui/needless_range_loop.stderr b/tests/ui/needless_range_loop.stderr index 97328f3d4d1..94ee5f613fc 100644 --- a/tests/ui/needless_range_loop.stderr +++ b/tests/ui/needless_range_loop.stderr @@ -23,7 +23,7 @@ error: the loop variable `i` is only used to index `ms`. help: consider using an iterator | 29 | for in &mut ms { - | ^^^^^^ + | error: the loop variable `i` is only used to index `ms`. --> $DIR/needless_range_loop.rs:35:5 @@ -37,5 +37,5 @@ error: the loop variable `i` is only used to index `ms`. help: consider using an iterator | 35 | for in &mut ms { - | ^^^^^^ + | diff --git a/tests/ui/print_with_newline.stderr b/tests/ui/print_with_newline.stderr index 2ade3ae4ef5..0148a470e0d 100644 --- a/tests/ui/print_with_newline.stderr +++ b/tests/ui/print_with_newline.stderr @@ -6,21 +6,3 @@ error: using `print!()` with a format string that ends in a newline, consider us | = note: `-D print-with-newline` implied by `-D warnings` -error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead - --> $DIR/print_with_newline.rs:7:5 - | -7 | print!("Hello {}/n", "world"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead - --> $DIR/print_with_newline.rs:8:5 - | -8 | print!("Hello {} {}/n/n", "world", "#2"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead - --> $DIR/print_with_newline.rs:9:5 - | -9 | print!("{}/n", 1265); - | ^^^^^^^^^^^^^^^^^^^^^ -