Add a couple small tests to the match-same-arm lint.

This commit is contained in:
laurent 2017-11-29 20:42:37 +00:00
parent 273ddafac5
commit f8dbd32433
3 changed files with 42 additions and 8 deletions

View File

@ -203,14 +203,10 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
db.span_note(i.body.span, "same as this");
// Note: this does not use `span_suggestion` on purpose: there is no clean way
// to
// remove the other arm. Building a span and suggest to replace it to "" makes
// an
// even more confusing error message. Also in order not to make up a span for
// the
// whole pattern, the suggestion is only shown when there is only one pattern.
// The
// user should know about `|` if they are already using it…
// to remove the other arm. Building a span and suggest to replace it to ""
// makes an even more confusing error message. Also in order not to make up a
// span for the whole pattern, the suggestion is only shown when there is only
// one pattern. The user should know about `|` if they are already using it…
if i.pats.len() == 1 && j.pats.len() == 1 {
let lhs = snippet(cx, i.pats[0].span, "<pat1>");

View File

@ -277,6 +277,26 @@ fn match_wild_err_arm() {
Ok(_) => println!("ok"),
Err(_) => {unreachable!();}
}
// no warning because of the guard
match x {
Ok(x) if x*x == 64 => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => println!("err")
}
match (x, Some(1i32)) {
(Ok(x), Some(_)) => println!("ok {}", x),
(Ok(_), Some(x)) => println!("ok {}", x),
_ => println!("err")
}
// no warning because of the different types for x
match (x, Some(1.0f64)) {
(Ok(x), Some(_)) => println!("ok {}", x),
(Ok(_), Some(x)) => println!("ok {}", x),
_ => println!("err")
}
}
fn main() {

View File

@ -390,3 +390,21 @@ 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)
error: this `match` has identical arm bodies
--> $DIR/matches.rs:290:29
|
290 | (Ok(_), Some(x)) => println!("ok {}", x),
| ^^^^^^^^^^^^^^^^^^^^
|
note: same as this
--> $DIR/matches.rs:289:29
|
289 | (Ok(x), Some(_)) => println!("ok {}", x),
| ^^^^^^^^^^^^^^^^^^^^
note: consider refactoring into `(Ok(x), Some(_)) | (Ok(_), Some(x))`
--> $DIR/matches.rs:289:29
|
289 | (Ok(x), Some(_)) => println!("ok {}", x),
| ^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)