Merge pull request #808 from mcarton/copies

Fix false positive in `MATCH_SAME_ARMS` and guards
This commit is contained in:
Manish Goregaokar 2016-03-29 08:39:24 +05:30
commit 94f10ee69a
2 changed files with 15 additions and 2 deletions

View File

@ -132,13 +132,15 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
}; };
let eq = |lhs: &Arm, rhs: &Arm| -> bool { let eq = |lhs: &Arm, rhs: &Arm| -> bool {
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) && // Arms with a guard are ignored, those cant always be merged together
lhs.guard.is_none() && rhs.guard.is_none() &&
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) &&
// all patterns should have the same bindings // all patterns should have the same bindings
bindings(cx, &lhs.pats[0]) == bindings(cx, &rhs.pats[0]) bindings(cx, &lhs.pats[0]) == bindings(cx, &rhs.pats[0])
}; };
if let ExprMatch(_, ref arms, MatchSource::Normal) = expr.node { if let ExprMatch(_, ref arms, MatchSource::Normal) = expr.node {
if let Some((i, j)) = search_same(&**arms, hash, eq) { if let Some((i, j)) = search_same(&arms, hash, eq) {
span_note_and_lint(cx, span_note_and_lint(cx,
MATCH_SAME_ARMS, MATCH_SAME_ARMS,
j.body.span, j.body.span,

View File

@ -142,12 +142,23 @@ fn if_same_then_else() -> Result<&'static str, ()> {
_ => true, _ => true,
}; };
let _ = match Some(42) {
Some(_) => 24,
None => 24,
};
let _ = match Some(42) { let _ = match Some(42) {
Some(42) => 24, Some(42) => 24,
Some(a) => 24, // bindings are different Some(a) => 24, // bindings are different
None => 0, None => 0,
}; };
let _ = match Some(42) {
Some(a) if a > 0 => 24,
Some(a) => 24, // one arm has a guard
None => 0,
};
match (Some(42), Some(42)) { match (Some(42), Some(42)) {
(Some(a), None) => bar(a), (Some(a), None) => bar(a),
(None, Some(a)) => bar(a), //~ERROR this `match` has identical arm bodies (None, Some(a)) => bar(a), //~ERROR this `match` has identical arm bodies