Move 'handle_method_call_in_not' code into 'suggest'

This commit is contained in:
Oliver Schneider 2017-11-14 17:07:04 +01:00
parent 561d47f338
commit bdf3887d22
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
6 changed files with 45 additions and 99 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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()`

View File

@ -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);
| ^^^^^^^^^^^^^^^^^^^

View File

@ -23,7 +23,7 @@ error: the loop variable `i` is only used to index `ms`.
help: consider using an iterator
|
29 | for <item> 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 <item> in &mut ms {
| ^^^^^^
|

View File

@ -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);
| ^^^^^^^^^^^^^^^^^^^^^