Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match check: note "catchall" patterns in unreachable error #33323

Merged
merged 1 commit into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,15 @@ fn check_arms(cx: &MatchCheckCtxt,
},

hir::MatchSource::Normal => {
span_err!(cx.tcx.sess, pat.span, E0001, "unreachable pattern")
let mut err = struct_span_err!(cx.tcx.sess, pat.span, E0001,
"unreachable pattern");
// if we had a catchall pattern, hint at that
for row in &seen.0 {
if pat_is_catchall(&cx.tcx.def_map.borrow(), row[0]) {
span_note!(err, row[0].span, "this pattern matches any value");
}
}
err.emit();
},

hir::MatchSource::TryDesugar => {
Expand All @@ -361,7 +369,18 @@ fn check_arms(cx: &MatchCheckCtxt,
}
}

fn raw_pat<'a>(p: &'a Pat) -> &'a Pat {
/// Checks for common cases of "catchall" patterns that may not be intended as such.
fn pat_is_catchall(dm: &DefMap, p: &Pat) -> bool {
match p.node {
PatKind::Ident(_, _, None) => pat_is_binding(dm, p),
PatKind::Ident(_, _, Some(ref s)) => pat_is_catchall(dm, &s),
PatKind::Ref(ref s, _) => pat_is_catchall(dm, &s),
PatKind::Tup(ref v) => v.iter().all(|p| pat_is_catchall(dm, &p)),
_ => false
}
}

fn raw_pat(p: &Pat) -> &Pat {
match p.node {
PatKind::Ident(_, _, Some(ref s)) => raw_pat(&s),
_ => p
Expand Down
49 changes: 49 additions & 0 deletions src/test/compile-fail/issue-31221.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

enum Enum {
Var1,
Var2,
}

fn main() {
use Enum::*;
let s = Var1;
match s {
Var1 => (),
Var3 => (),
//~^ NOTE this pattern matches any value
Var2 => (),
//~^ ERROR unreachable pattern
};
match &s {
&Var1 => (),
&Var3 => (),
//~^ NOTE this pattern matches any value
&Var2 => (),
//~^ ERROR unreachable pattern
};
let t = (Var1, Var1);
match t {
(Var1, b) => (),
(c, d) => (),
//~^ NOTE this pattern matches any value
anything => ()
//~^ ERROR unreachable pattern
};
// `_` need not emit a note, it is pretty obvious already.
let t = (Var1, Var1);
match t {
(Var1, b) => (),
_ => (),
anything => ()
//~^ ERROR unreachable pattern
};
}