Skip to content

Commit 4650361

Browse files
committed
use structured suggestion for pattern-named-the-same-as-variant warning
1 parent 3874676 commit 4650361

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

src/librustc_mir/hair/pattern/check_match.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc::session::Session;
2323
use rustc::ty::{self, Ty, TyCtxt};
2424
use rustc::ty::subst::Substs;
2525
use rustc::lint;
26-
use rustc_errors::DiagnosticBuilder;
26+
use rustc_errors::{Applicability, DiagnosticBuilder};
2727
use rustc::util::common::ErrorReported;
2828

2929
use rustc::hir::def::*;
@@ -328,10 +328,12 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchVisitor, pat: &Pat) {
328328
"pattern binding `{}` is named the same as one \
329329
of the variants of the type `{}`",
330330
name.node, ty_path);
331-
help!(err,
332-
"if you meant to match on a variant, \
333-
consider making the path in the pattern qualified: `{}::{}`",
334-
ty_path, name.node);
331+
err.span_suggestion_with_applicability(
332+
p.span,
333+
"to match on the variant, qualify the path",
334+
format!("{}::{}", ty_path, name.node),
335+
Applicability::MachineApplicable
336+
);
335337
err.emit();
336338
}
337339
}

src/test/ui/issue-19100.fixed

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// run-pass
12+
// run-rustfix
13+
14+
#![allow(non_snake_case)]
15+
#![allow(dead_code)]
16+
#![allow(unused_variables)]
17+
18+
#[derive(Copy, Clone)]
19+
enum Foo {
20+
Bar,
21+
Baz
22+
}
23+
24+
impl Foo {
25+
fn foo(&self) {
26+
match self {
27+
&
28+
Foo::Bar if true
29+
//~^ WARN pattern binding `Bar` is named the same as one of the variants of the type `Foo`
30+
=> println!("bar"),
31+
&
32+
Foo::Baz if false
33+
//~^ WARN pattern binding `Baz` is named the same as one of the variants of the type `Foo`
34+
=> println!("baz"),
35+
_ => ()
36+
}
37+
}
38+
}
39+
40+
fn main() {}

src/test/ui/issue-19100.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// run-pass
12+
// run-rustfix
1213

1314
#![allow(non_snake_case)]
1415
#![allow(dead_code)]

src/test/ui/issue-19100.stderr

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
2-
--> $DIR/issue-19100.rs:27:1
2+
--> $DIR/issue-19100.rs:28:1
33
|
44
LL | Bar if true
5-
| ^^^
6-
|
7-
= help: if you meant to match on a variant, consider making the path in the pattern qualified: `Foo::Bar`
5+
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
86

97
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
10-
--> $DIR/issue-19100.rs:31:1
8+
--> $DIR/issue-19100.rs:32:1
119
|
1210
LL | Baz if false
13-
| ^^^
14-
|
15-
= help: if you meant to match on a variant, consider making the path in the pattern qualified: `Foo::Baz`
11+
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
1612

src/test/ui/issue-30302.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ warning[E0170]: pattern binding `Nil` is named the same as one of the variants o
22
--> $DIR/issue-30302.rs:23:9
33
|
44
LL | Nil => true,
5-
| ^^^
6-
|
7-
= help: if you meant to match on a variant, consider making the path in the pattern qualified: `Stack::Nil`
5+
| ^^^ help: to match on the variant, qualify the path: `Stack::Nil`
86

97
error: unreachable pattern
108
--> $DIR/issue-30302.rs:25:9

0 commit comments

Comments
 (0)