mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-29 03:27:44 +00:00
Do raise a same-arms warning when the two arms are separated by an arm with a guard, fix #1996.
This commit is contained in:
parent
876d6d8c43
commit
904f27a2ea
@ -178,22 +178,26 @@ fn lint_same_cond(cx: &LateContext, conds: &[&Expr]) {
|
|||||||
|
|
||||||
/// Implementation if `MATCH_SAME_ARMS`.
|
/// Implementation if `MATCH_SAME_ARMS`.
|
||||||
fn lint_match_arms(cx: &LateContext, expr: &Expr) {
|
fn lint_match_arms(cx: &LateContext, expr: &Expr) {
|
||||||
let hash = |arm: &Arm| -> u64 {
|
if let ExprMatch(_, ref arms, MatchSource::Normal) = expr.node {
|
||||||
|
let hash = |&(_, arm): &(usize, &Arm)| -> u64 {
|
||||||
let mut h = SpanlessHash::new(cx);
|
let mut h = SpanlessHash::new(cx);
|
||||||
h.hash_expr(&arm.body);
|
h.hash_expr(&arm.body);
|
||||||
h.finish()
|
h.finish()
|
||||||
};
|
};
|
||||||
|
|
||||||
let eq = |lhs: &Arm, rhs: &Arm| -> bool {
|
let eq = |&(lindex, lhs): &(usize, &Arm), &(rindex, rhs): &(usize, &Arm)| -> bool {
|
||||||
|
let min_index = usize::min(lindex, rindex);
|
||||||
|
let max_index = usize::max(rindex, rindex);
|
||||||
// Arms with a guard are ignored, those can’t always be merged together
|
// Arms with a guard are ignored, those can’t always be merged together
|
||||||
lhs.guard.is_none() && rhs.guard.is_none() &&
|
// This is also the case for arms in-between each there is an arm with a guard
|
||||||
|
(min_index..=max_index).all(|index| arms[index].guard.is_none()) &&
|
||||||
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) &&
|
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 {
|
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect();
|
||||||
if let Some((i, j)) = search_same(arms, hash, eq) {
|
if let Some((&(_, i), &(_, j))) = search_same(&indexed_arms, hash, eq) {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
MATCH_SAME_ARMS,
|
MATCH_SAME_ARMS,
|
||||||
|
@ -285,7 +285,7 @@ fn match_wild_err_arm() {
|
|||||||
Err(_) => println!("err")
|
Err(_) => println!("err")
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is a current false positive, see #1996
|
// this used to be a false positive, see #1996
|
||||||
match x {
|
match x {
|
||||||
Ok(3) => println!("ok"),
|
Ok(3) => println!("ok"),
|
||||||
Ok(x) if x*x == 64 => println!("ok 64"),
|
Ok(x) if x*x == 64 => println!("ok 64"),
|
||||||
|
@ -390,24 +390,6 @@ note: consider refactoring into `Ok(3) | Ok(_)`
|
|||||||
| ^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^
|
||||||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
|
||||||
|
|
||||||
error: this `match` has identical arm bodies
|
|
||||||
--> $DIR/matches.rs:292:18
|
|
||||||
|
|
|
||||||
292 | Ok(_) => println!("ok"),
|
|
||||||
| ^^^^^^^^^^^^^^
|
|
||||||
|
|
|
||||||
note: same as this
|
|
||||||
--> $DIR/matches.rs:290:18
|
|
||||||
|
|
|
||||||
290 | Ok(3) => println!("ok"),
|
|
||||||
| ^^^^^^^^^^^^^^
|
|
||||||
note: consider refactoring into `Ok(3) | Ok(_)`
|
|
||||||
--> $DIR/matches.rs:290:18
|
|
||||||
|
|
|
||||||
290 | Ok(3) => println!("ok"),
|
|
||||||
| ^^^^^^^^^^^^^^
|
|
||||||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
|
|
||||||
|
|
||||||
error: this `match` has identical arm bodies
|
error: this `match` has identical arm bodies
|
||||||
--> $DIR/matches.rs:298:29
|
--> $DIR/matches.rs:298:29
|
||||||
|
|
|
|
||||||
|
@ -112,7 +112,7 @@ error: trivial regex
|
|||||||
error: trivial regex
|
error: trivial regex
|
||||||
--> $DIR/regex.rs:62:40
|
--> $DIR/regex.rs:62:40
|
||||||
|
|
|
|
||||||
62 | let trivial_backslash = Regex::new("a/.b");
|
62 | let trivial_backslash = Regex::new("a//.b");
|
||||||
| ^^^^^^^
|
| ^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using consider using `str::contains`
|
= help: consider using consider using `str::contains`
|
||||||
|
Loading…
Reference in New Issue
Block a user