Move 'handle_method_call_in_not' code into 'suggest'
This commit is contained in:
@@ -177,7 +177,8 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
|
|||||||
s.push('!');
|
s.push('!');
|
||||||
recurse(true, cx, inner, terminals, s)
|
recurse(true, cx, inner, terminals, s)
|
||||||
},
|
},
|
||||||
Term(n) => if let ExprBinary(binop, ref lhs, ref rhs) = terminals[n as usize].node {
|
Term(n) => match terminals[n as usize].node {
|
||||||
|
ExprBinary(binop, ref lhs, ref rhs) => {
|
||||||
let op = match binop.node {
|
let op = match binop.node {
|
||||||
BiEq => " != ",
|
BiEq => " != ",
|
||||||
BiNe => " == ",
|
BiNe => " == ",
|
||||||
@@ -194,9 +195,27 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
|
|||||||
s.push_str(op);
|
s.push_str(op);
|
||||||
s.push_str(&snip(rhs));
|
s.push_str(&snip(rhs));
|
||||||
s
|
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 {
|
} else {
|
||||||
s.push('!');
|
s.push('!');
|
||||||
recurse(false, cx, inner, terminals, s)
|
recurse(false, cx, inner, terminals, s)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
s.push('!');
|
||||||
|
recurse(false, cx, inner, terminals, s)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
s.push('!');
|
s.push('!');
|
||||||
@@ -402,32 +421,6 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
|
||||||
@@ -438,7 +431,6 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
|
|||||||
match e.node {
|
match e.node {
|
||||||
ExprBinary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e),
|
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() {
|
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);
|
self.bool_expr(e);
|
||||||
} else {
|
} else {
|
||||||
walk_expr(self, e);
|
walk_expr(self, e);
|
||||||
|
|||||||
@@ -51,4 +51,6 @@ fn methods_with_negation() {
|
|||||||
let _ = !b.is_err();
|
let _ = !b.is_err();
|
||||||
let _ = b.is_ok();
|
let _ = b.is_ok();
|
||||||
let _ = !b.is_ok();
|
let _ = !b.is_ok();
|
||||||
|
let c = false;
|
||||||
|
let _ = !(a.is_some() && !c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,26 +131,8 @@ help: try
|
|||||||
| ^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: this boolean expression can be simplified
|
error: this boolean expression can be simplified
|
||||||
--> $DIR/booleans.rs:47:13
|
--> $DIR/booleans.rs:55:13
|
||||||
|
|
|
|
||||||
47 | let _ = !a.is_some();
|
55 | let _ = !(a.is_some() && !c);
|
||||||
| ^^^^^^^^^^^^ help: try: `is_none`
|
| ^^^^^^^^^^^^^^^^^^^^ help: try: `c || a.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`
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,15 +6,3 @@ error: useless use of `format!`
|
|||||||
|
|
|
|
||||||
= note: `-D useless-format` implied by `-D warnings`
|
= 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);
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ error: the loop variable `i` is only used to index `ms`.
|
|||||||
help: consider using an iterator
|
help: consider using an iterator
|
||||||
|
|
|
|
||||||
29 | for <item> in &mut ms {
|
29 | for <item> in &mut ms {
|
||||||
| ^^^^^^
|
|
|
||||||
|
|
||||||
error: the loop variable `i` is only used to index `ms`.
|
error: the loop variable `i` is only used to index `ms`.
|
||||||
--> $DIR/needless_range_loop.rs:35:5
|
--> $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
|
help: consider using an iterator
|
||||||
|
|
|
|
||||||
35 | for <item> in &mut ms {
|
35 | for <item> in &mut ms {
|
||||||
| ^^^^^^
|
|
|
||||||
|
|
||||||
|
|||||||
@@ -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`
|
= 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);
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user