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
6 changed files with 45 additions and 99 deletions

View File

@@ -177,26 +177,45 @@ fn suggest(cx: &LateContext, suggestion: &Bool, terminals: &[&Expr]) -> String {
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 @@ 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> {
@@ -438,7 +431,6 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
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);