Merge pull request #2251 from LaurentMazare/master

Add a couple small tests to the match-same-arm lint + fix a small issue in search_same.
This commit is contained in:
Oliver Schneider 2017-11-30 10:53:48 +01:00 committed by GitHub
commit 1c95a7cf35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 15 deletions

View File

@ -74,9 +74,8 @@ impl PartialEq for Constant {
} }
}, },
(&Constant::Bool(l), &Constant::Bool(r)) => l == r, (&Constant::Bool(l), &Constant::Bool(r)) => l == r,
(&Constant::Vec(ref l), &Constant::Vec(ref r)) => l == r, (&Constant::Vec(ref l), &Constant::Vec(ref r)) | (&Constant::Tuple(ref l), &Constant::Tuple(ref r)) => l == r,
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => ls == rs && lv == rv, (&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => ls == rs && lv == rv,
(&Constant::Tuple(ref l), &Constant::Tuple(ref r)) => l == r,
_ => false, // TODO: Are there inter-type equalities? _ => false, // TODO: Are there inter-type equalities?
} }
} }

View File

@ -203,14 +203,10 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
db.span_note(i.body.span, "same as this"); db.span_note(i.body.span, "same as this");
// Note: this does not use `span_suggestion` on purpose: there is no clean way // Note: this does not use `span_suggestion` on purpose: there is no clean way
// to // to remove the other arm. Building a span and suggest to replace it to ""
// remove the other arm. Building a span and suggest to replace it to "" makes // makes an even more confusing error message. Also in order not to make up a
// an // span for the whole pattern, the suggestion is only shown when there is only
// even more confusing error message. Also in order not to make up a span for // one pattern. The user should know about `|` if they are already using it…
// 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 { if i.pats.len() == 1 && j.pats.len() == 1 {
let lhs = snippet(cx, i.pats[0].span, "<pat1>"); let lhs = snippet(cx, i.pats[0].span, "<pat1>");
@ -329,10 +325,13 @@ where
for expr in exprs { for expr in exprs {
match map.entry(hash(expr)) { match map.entry(hash(expr)) {
Entry::Occupied(o) => for o in o.get() { Entry::Occupied(mut o) => {
if eq(o, expr) { for o in o.get() {
return Some((o, expr)); if eq(o, expr) {
return Some((o, expr));
}
} }
o.get_mut().push(expr);
}, },
Entry::Vacant(v) => { Entry::Vacant(v) => {
v.insert(vec![expr]); v.insert(vec![expr]);

View File

@ -206,8 +206,7 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
End(Link(_, _)) => in_link = None, End(Link(_, _)) => in_link = None,
Start(_tag) | End(_tag) => (), // We don't care about other tags Start(_tag) | End(_tag) => (), // We don't care about other tags
Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it
SoftBreak => (), SoftBreak | HardBreak => (),
HardBreak => (),
FootnoteReference(text) | Text(text) => { FootnoteReference(text) | Text(text) => {
if Some(&text) == in_link.as_ref() { if Some(&text) == in_link.as_ref() {
// Probably a link of the form `<http://example.com>` // Probably a link of the form `<http://example.com>`

View File

@ -277,6 +277,42 @@ fn match_wild_err_arm() {
Ok(_) => println!("ok"), Ok(_) => println!("ok"),
Err(_) => {unreachable!();} Err(_) => {unreachable!();}
} }
// no warning because of the guard
match x {
Ok(x) if x*x == 64 => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => println!("err")
}
// this is a current false positive, see #1996
match x {
Ok(3) => println!("ok"),
Ok(x) if x*x == 64 => println!("ok 64"),
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")
}
// because of a bug, no warning was generated for this case before #2251
match x {
Ok(_tmp) => println!("ok"),
Ok(3) => println!("ok"),
Ok(_) => println!("ok"),
Err(_) => {unreachable!();}
}
} }
fn main() { fn main() {

View File

@ -390,3 +390,57 @@ 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
--> $DIR/matches.rs:298:29
|
298 | (Ok(_), Some(x)) => println!("ok {}", x),
| ^^^^^^^^^^^^^^^^^^^^
|
note: same as this
--> $DIR/matches.rs:297:29
|
297 | (Ok(x), Some(_)) => println!("ok {}", x),
| ^^^^^^^^^^^^^^^^^^^^
note: consider refactoring into `(Ok(x), Some(_)) | (Ok(_), Some(x))`
--> $DIR/matches.rs:297:29
|
297 | (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)
error: this `match` has identical arm bodies
--> $DIR/matches.rs:313:18
|
313 | Ok(_) => println!("ok"),
| ^^^^^^^^^^^^^^
|
note: same as this
--> $DIR/matches.rs:312:18
|
312 | Ok(3) => println!("ok"),
| ^^^^^^^^^^^^^^
note: consider refactoring into `Ok(3) | Ok(_)`
--> $DIR/matches.rs:312:18
|
312 | 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)