From 4650361fb627f7dd6b8d5c1cee8a7a12a050ba80 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sat, 23 Jun 2018 16:46:41 -0700 Subject: [PATCH] use structured suggestion for pattern-named-the-same-as-variant warning --- src/librustc_mir/hair/pattern/check_match.rs | 12 +++--- src/test/ui/issue-19100.fixed | 40 ++++++++++++++++++++ src/test/ui/issue-19100.rs | 1 + src/test/ui/issue-19100.stderr | 12 ++---- src/test/ui/issue-30302.stderr | 4 +- 5 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 src/test/ui/issue-19100.fixed diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 2c58bd8e79b0..b96cc352bdb6 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -23,7 +23,7 @@ use rustc::session::Session; use rustc::ty::{self, Ty, TyCtxt}; use rustc::ty::subst::Substs; use rustc::lint; -use rustc_errors::DiagnosticBuilder; +use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc::util::common::ErrorReported; use rustc::hir::def::*; @@ -328,10 +328,12 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchVisitor, pat: &Pat) { "pattern binding `{}` is named the same as one \ of the variants of the type `{}`", name.node, ty_path); - help!(err, - "if you meant to match on a variant, \ - consider making the path in the pattern qualified: `{}::{}`", - ty_path, name.node); + err.span_suggestion_with_applicability( + p.span, + "to match on the variant, qualify the path", + format!("{}::{}", ty_path, name.node), + Applicability::MachineApplicable + ); err.emit(); } } diff --git a/src/test/ui/issue-19100.fixed b/src/test/ui/issue-19100.fixed new file mode 100644 index 000000000000..3ced913ae11c --- /dev/null +++ b/src/test/ui/issue-19100.fixed @@ -0,0 +1,40 @@ +// Copyright 2014 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-pass +// run-rustfix + +#![allow(non_snake_case)] +#![allow(dead_code)] +#![allow(unused_variables)] + +#[derive(Copy, Clone)] +enum Foo { + Bar, + Baz +} + +impl Foo { + fn foo(&self) { + match self { + & +Foo::Bar if true +//~^ WARN pattern binding `Bar` is named the same as one of the variants of the type `Foo` +=> println!("bar"), + & +Foo::Baz if false +//~^ WARN pattern binding `Baz` is named the same as one of the variants of the type `Foo` +=> println!("baz"), +_ => () + } + } +} + +fn main() {} diff --git a/src/test/ui/issue-19100.rs b/src/test/ui/issue-19100.rs index 2032f23e6a30..e073bf907616 100644 --- a/src/test/ui/issue-19100.rs +++ b/src/test/ui/issue-19100.rs @@ -9,6 +9,7 @@ // except according to those terms. // run-pass +// run-rustfix #![allow(non_snake_case)] #![allow(dead_code)] diff --git a/src/test/ui/issue-19100.stderr b/src/test/ui/issue-19100.stderr index 96835f69b78c..34dc29c63df7 100644 --- a/src/test/ui/issue-19100.stderr +++ b/src/test/ui/issue-19100.stderr @@ -1,16 +1,12 @@ warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo` - --> $DIR/issue-19100.rs:27:1 + --> $DIR/issue-19100.rs:28:1 | LL | Bar if true - | ^^^ - | - = help: if you meant to match on a variant, consider making the path in the pattern qualified: `Foo::Bar` + | ^^^ help: to match on the variant, qualify the path: `Foo::Bar` warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo` - --> $DIR/issue-19100.rs:31:1 + --> $DIR/issue-19100.rs:32:1 | LL | Baz if false - | ^^^ - | - = help: if you meant to match on a variant, consider making the path in the pattern qualified: `Foo::Baz` + | ^^^ help: to match on the variant, qualify the path: `Foo::Baz` diff --git a/src/test/ui/issue-30302.stderr b/src/test/ui/issue-30302.stderr index 42dfdadf9c46..fa3cb92b180e 100644 --- a/src/test/ui/issue-30302.stderr +++ b/src/test/ui/issue-30302.stderr @@ -2,9 +2,7 @@ warning[E0170]: pattern binding `Nil` is named the same as one of the variants o --> $DIR/issue-30302.rs:23:9 | LL | Nil => true, - | ^^^ - | - = help: if you meant to match on a variant, consider making the path in the pattern qualified: `Stack::Nil` + | ^^^ help: to match on the variant, qualify the path: `Stack::Nil` error: unreachable pattern --> $DIR/issue-30302.rs:25:9