]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #51750 - zackmdavis:superstructure, r=oli-obk
authorbors <bors@rust-lang.org>
Mon, 25 Jun 2018 13:43:22 +0000 (13:43 +0000)
committerbors <bors@rust-lang.org>
Mon, 25 Jun 2018 13:43:22 +0000 (13:43 +0000)
three diagnostics upgrades

 * reword `...` expression syntax error to not imply that you should use it in patterns either (#51043) and make it a structured suggestion
 * shorten the top-line message for the trivial-casts lint by tucking the advisory sentence into a help note
 * structured suggestion for pattern-named-the-same-as-variant warning

r? @oli-obk

14 files changed:
src/librustc_mir/hair/pattern/check_match.rs
src/librustc_typeck/check/cast.rs
src/libsyntax/parse/parser.rs
src/test/parse-fail/range_inclusive_dotdotdot.rs
src/test/ui/issue-19100.fixed [new file with mode: 0644]
src/test/ui/issue-19100.rs
src/test/ui/issue-19100.stderr
src/test/ui/issue-30302.stderr
src/test/ui/lint/trivial-casts-featuring-type-ascription.rs [new file with mode: 0644]
src/test/ui/lint/trivial-casts-featuring-type-ascription.stderr [new file with mode: 0644]
src/test/ui/lint/trivial-casts.rs [new file with mode: 0644]
src/test/ui/lint/trivial-casts.stderr [new file with mode: 0644]
src/test/ui/suggestions/dotdotdot-expr.rs [new file with mode: 0644]
src/test/ui/suggestions/dotdotdot-expr.stderr [new file with mode: 0644]

index 2c58bd8e79b08ac7d4de1c7d4fb3c71577de9d83..b96cc352bdb60374499a42ef41efe306d7607c6a 100644 (file)
@@ -23,7 +23,7 @@
 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();
                 }
             }
index f0d7ca8ebf14f6c5ee29d6a15b5c1437fff3b57a..07e19c84a95073dff0b4734812dd885ea28667c8 100644 (file)
@@ -365,28 +365,27 @@ fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
     fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
         let t_cast = self.cast_ty;
         let t_expr = self.expr_ty;
-        if t_cast.is_numeric() && t_expr.is_numeric() {
-            fcx.tcx.lint_node(
-                lint::builtin::TRIVIAL_NUMERIC_CASTS,
-                self.expr.id,
-                self.span,
-                &format!("trivial numeric cast: `{}` as `{}`. Cast can be \
-                          replaced by coercion, this might require type \
-                          ascription or a temporary variable",
-                         fcx.ty_to_string(t_expr),
-                         fcx.ty_to_string(t_cast)));
+        let type_asc_or = if fcx.tcx.features().type_ascription {
+            "type ascription or "
         } else {
-            fcx.tcx.lint_node(
-                lint::builtin::TRIVIAL_CASTS,
-                self.expr.id,
-                self.span,
-                &format!("trivial cast: `{}` as `{}`. Cast can be \
-                          replaced by coercion, this might require type \
-                          ascription or a temporary variable",
-                         fcx.ty_to_string(t_expr),
-                         fcx.ty_to_string(t_cast)));
-        }
-
+            ""
+        };
+        let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
+            ("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
+        } else {
+            ("", lint::builtin::TRIVIAL_CASTS)
+        };
+        let mut err = fcx.tcx.struct_span_lint_node(
+            lint,
+            self.expr.id,
+            self.span,
+            &format!("trivial {}cast: `{}` as `{}`",
+                     adjective,
+                     fcx.ty_to_string(t_expr),
+                     fcx.ty_to_string(t_cast)));
+        err.help(&format!("cast can be replaced by coercion; this might \
+                           require {}a temporary variable", type_asc_or));
+        err.emit();
     }
 
     pub fn check(mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
index 5970f94b97d52f12f4cfc9737033c8703900875a..955bdbdcf917765d8620b4a730070b1da4812d2e 100644 (file)
@@ -4800,12 +4800,14 @@ fn warn_missing_semicolon(&self) {
 
     fn err_dotdotdot_syntax(&self, span: Span) {
         self.diagnostic().struct_span_err(span, {
-            "`...` syntax cannot be used in expressions"
-        }).help({
-            "Use `..` if you need an exclusive range (a < b)"
-        }).help({
-            "or `..=` if you need an inclusive range (a <= b)"
-        }).emit();
+            "unexpected token: `...`"
+        }).span_suggestion_with_applicability(
+            span, "use `..` for an exclusive range", "..".to_owned(),
+            Applicability::MaybeIncorrect
+        ).span_suggestion_with_applicability(
+            span, "or `..=` for an inclusive range", "..=".to_owned(),
+            Applicability::MaybeIncorrect
+        ).emit();
     }
 
     // Parse bounds of a type parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
index fa6474717d3f0320afbcb2663fdc1d2b2580b9b6..34b96a59c2dde784b5f7b4bc39beb0cfa63f10e2 100644 (file)
 use std::ops::RangeToInclusive;
 
 fn return_range_to() -> RangeToInclusive<i32> {
-    return ...1; //~ERROR `...` syntax cannot be used in expressions
-                 //~^HELP  Use `..` if you need an exclusive range (a < b)
-                 //~^^HELP or `..=` if you need an inclusive range (a <= b)
+    return ...1; //~ERROR unexpected token: `...`
+                 //~^HELP  use `..` for an exclusive range
+                 //~^^HELP or `..=` for an inclusive range
 }
 
 pub fn main() {
-    let x = ...0;    //~ERROR `...` syntax cannot be used in expressions
-                     //~^HELP  Use `..` if you need an exclusive range (a < b)
-                     //~^^HELP or `..=` if you need an inclusive range (a <= b)
+    let x = ...0;    //~ERROR unexpected token: `...`
+                     //~^HELP  use `..` for an exclusive range
+                     //~^^HELP or `..=` for an inclusive range
 
-    let x = 5...5;   //~ERROR `...` syntax cannot be used in expressions
-                     //~^HELP  Use `..` if you need an exclusive range (a < b)
-                     //~^^HELP or `..=` if you need an inclusive range (a <= b)
+    let x = 5...5;   //~ERROR unexpected token: `...`
+                     //~^HELP  use `..` for an exclusive range
+                     //~^^HELP or `..=` for an inclusive range
 
-    for _ in 0...1 {} //~ERROR `...` syntax cannot be used in expressions
-                     //~^HELP  Use `..` if you need an exclusive range (a < b)
-                     //~^^HELP or `..=` if you need an inclusive range (a <= b)
+    for _ in 0...1 {} //~ERROR unexpected token: `...`
+                     //~^HELP  use `..` for an exclusive range
+                     //~^^HELP or `..=` for an inclusive range
 }
-
diff --git a/src/test/ui/issue-19100.fixed b/src/test/ui/issue-19100.fixed
new file mode 100644 (file)
index 0000000..3ced913
--- /dev/null
@@ -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 <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.
+
+// 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() {}
index 2032f23e6a30e0feb33e0c151c0e2ba5853e32b5..e073bf9076160eb23e9d9641c68e93ae9017a4a1 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 // run-pass
+// run-rustfix
 
 #![allow(non_snake_case)]
 #![allow(dead_code)]
index 96835f69b78c772be9bf57b17cb294b3c464591d..34dc29c63df72029d7b34183ec9748ee939bbb50 100644 (file)
@@ -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`
 
index 42dfdadf9c46f1d505a3a436cdf3ad26d82ba787..fa3cb92b180e06e2be7903c3864e6c995309b1a5 100644 (file)
@@ -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
diff --git a/src/test/ui/lint/trivial-casts-featuring-type-ascription.rs b/src/test/ui/lint/trivial-casts-featuring-type-ascription.rs
new file mode 100644 (file)
index 0000000..fba3724
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2018 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.
+
+#![deny(trivial_casts, trivial_numeric_casts)]
+#![feature(type_ascription)]
+
+fn main() {
+    let lugubrious = 12i32 as i32;
+    //~^ ERROR trivial numeric cast
+    let haunted: &u32 = &99;
+    let _ = haunted as *const u32;
+    //~^ ERROR trivial cast
+}
diff --git a/src/test/ui/lint/trivial-casts-featuring-type-ascription.stderr b/src/test/ui/lint/trivial-casts-featuring-type-ascription.stderr
new file mode 100644 (file)
index 0000000..a77135c
--- /dev/null
@@ -0,0 +1,28 @@
+error: trivial numeric cast: `i32` as `i32`
+  --> $DIR/trivial-casts-featuring-type-ascription.rs:15:22
+   |
+LL |     let lugubrious = 12i32 as i32;
+   |                      ^^^^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/trivial-casts-featuring-type-ascription.rs:11:24
+   |
+LL | #![deny(trivial_casts, trivial_numeric_casts)]
+   |                        ^^^^^^^^^^^^^^^^^^^^^
+   = help: cast can be replaced by coercion; this might require type ascription or a temporary variable
+
+error: trivial cast: `&u32` as `*const u32`
+  --> $DIR/trivial-casts-featuring-type-ascription.rs:18:13
+   |
+LL |     let _ = haunted as *const u32;
+   |             ^^^^^^^^^^^^^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/trivial-casts-featuring-type-ascription.rs:11:9
+   |
+LL | #![deny(trivial_casts, trivial_numeric_casts)]
+   |         ^^^^^^^^^^^^^
+   = help: cast can be replaced by coercion; this might require type ascription or a temporary variable
+
+error: aborting due to 2 previous errors
+
diff --git a/src/test/ui/lint/trivial-casts.rs b/src/test/ui/lint/trivial-casts.rs
new file mode 100644 (file)
index 0000000..759b282
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2018 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.
+
+#![deny(trivial_casts, trivial_numeric_casts)]
+
+fn main() {
+    let lugubrious = 12i32 as i32;
+    //~^ ERROR trivial numeric cast
+    let haunted: &u32 = &99;
+    let _ = haunted as *const u32;
+    //~^ ERROR trivial cast
+}
diff --git a/src/test/ui/lint/trivial-casts.stderr b/src/test/ui/lint/trivial-casts.stderr
new file mode 100644 (file)
index 0000000..d52869f
--- /dev/null
@@ -0,0 +1,28 @@
+error: trivial numeric cast: `i32` as `i32`
+  --> $DIR/trivial-casts.rs:14:22
+   |
+LL |     let lugubrious = 12i32 as i32;
+   |                      ^^^^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/trivial-casts.rs:11:24
+   |
+LL | #![deny(trivial_casts, trivial_numeric_casts)]
+   |                        ^^^^^^^^^^^^^^^^^^^^^
+   = help: cast can be replaced by coercion; this might require a temporary variable
+
+error: trivial cast: `&u32` as `*const u32`
+  --> $DIR/trivial-casts.rs:17:13
+   |
+LL |     let _ = haunted as *const u32;
+   |             ^^^^^^^^^^^^^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/trivial-casts.rs:11:9
+   |
+LL | #![deny(trivial_casts, trivial_numeric_casts)]
+   |         ^^^^^^^^^^^^^
+   = help: cast can be replaced by coercion; this might require a temporary variable
+
+error: aborting due to 2 previous errors
+
diff --git a/src/test/ui/suggestions/dotdotdot-expr.rs b/src/test/ui/suggestions/dotdotdot-expr.rs
new file mode 100644 (file)
index 0000000..afb73a5
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2018 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.
+
+fn main() {
+    let _redemptive = 1...21;
+    //~^ ERROR unexpected token
+}
diff --git a/src/test/ui/suggestions/dotdotdot-expr.stderr b/src/test/ui/suggestions/dotdotdot-expr.stderr
new file mode 100644 (file)
index 0000000..3315538
--- /dev/null
@@ -0,0 +1,16 @@
+error: unexpected token: `...`
+  --> $DIR/dotdotdot-expr.rs:12:24
+   |
+LL |     let _redemptive = 1...21;
+   |                        ^^^
+help: use `..` for an exclusive range
+   |
+LL |     let _redemptive = 1..21;
+   |                        ^^
+help: or `..=` for an inclusive range
+   |
+LL |     let _redemptive = 1..=21;
+   |                        ^^^
+
+error: aborting due to previous error
+