Skip to content

Commit 8acec1f

Browse files
committed
Auto merge of #51750 - zackmdavis:superstructure, r=oli-obk
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
2 parents ecfe370 + 0b39a82 commit 8acec1f

14 files changed

+218
-56
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/librustc_typeck/check/cast.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -365,28 +365,27 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
365365
fn trivial_cast_lint(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
366366
let t_cast = self.cast_ty;
367367
let t_expr = self.expr_ty;
368-
if t_cast.is_numeric() && t_expr.is_numeric() {
369-
fcx.tcx.lint_node(
370-
lint::builtin::TRIVIAL_NUMERIC_CASTS,
371-
self.expr.id,
372-
self.span,
373-
&format!("trivial numeric cast: `{}` as `{}`. Cast can be \
374-
replaced by coercion, this might require type \
375-
ascription or a temporary variable",
376-
fcx.ty_to_string(t_expr),
377-
fcx.ty_to_string(t_cast)));
368+
let type_asc_or = if fcx.tcx.features().type_ascription {
369+
"type ascription or "
378370
} else {
379-
fcx.tcx.lint_node(
380-
lint::builtin::TRIVIAL_CASTS,
381-
self.expr.id,
382-
self.span,
383-
&format!("trivial cast: `{}` as `{}`. Cast can be \
384-
replaced by coercion, this might require type \
385-
ascription or a temporary variable",
386-
fcx.ty_to_string(t_expr),
387-
fcx.ty_to_string(t_cast)));
388-
}
389-
371+
""
372+
};
373+
let (adjective, lint) = if t_cast.is_numeric() && t_expr.is_numeric() {
374+
("numeric ", lint::builtin::TRIVIAL_NUMERIC_CASTS)
375+
} else {
376+
("", lint::builtin::TRIVIAL_CASTS)
377+
};
378+
let mut err = fcx.tcx.struct_span_lint_node(
379+
lint,
380+
self.expr.id,
381+
self.span,
382+
&format!("trivial {}cast: `{}` as `{}`",
383+
adjective,
384+
fcx.ty_to_string(t_expr),
385+
fcx.ty_to_string(t_cast)));
386+
err.help(&format!("cast can be replaced by coercion; this might \
387+
require {}a temporary variable", type_asc_or));
388+
err.emit();
390389
}
391390

392391
pub fn check(mut self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {

src/libsyntax/parse/parser.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -4800,12 +4800,14 @@ impl<'a> Parser<'a> {
48004800

48014801
fn err_dotdotdot_syntax(&self, span: Span) {
48024802
self.diagnostic().struct_span_err(span, {
4803-
"`...` syntax cannot be used in expressions"
4804-
}).help({
4805-
"Use `..` if you need an exclusive range (a < b)"
4806-
}).help({
4807-
"or `..=` if you need an inclusive range (a <= b)"
4808-
}).emit();
4803+
"unexpected token: `...`"
4804+
}).span_suggestion_with_applicability(
4805+
span, "use `..` for an exclusive range", "..".to_owned(),
4806+
Applicability::MaybeIncorrect
4807+
).span_suggestion_with_applicability(
4808+
span, "or `..=` for an inclusive range", "..=".to_owned(),
4809+
Applicability::MaybeIncorrect
4810+
).emit();
48094811
}
48104812

48114813
// Parse bounds of a type parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.

src/test/parse-fail/range_inclusive_dotdotdot.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515
use std::ops::RangeToInclusive;
1616

1717
fn return_range_to() -> RangeToInclusive<i32> {
18-
return ...1; //~ERROR `...` syntax cannot be used in expressions
19-
//~^HELP Use `..` if you need an exclusive range (a < b)
20-
//~^^HELP or `..=` if you need an inclusive range (a <= b)
18+
return ...1; //~ERROR unexpected token: `...`
19+
//~^HELP use `..` for an exclusive range
20+
//~^^HELP or `..=` for an inclusive range
2121
}
2222

2323
pub fn main() {
24-
let x = ...0; //~ERROR `...` syntax cannot be used in expressions
25-
//~^HELP Use `..` if you need an exclusive range (a < b)
26-
//~^^HELP or `..=` if you need an inclusive range (a <= b)
24+
let x = ...0; //~ERROR unexpected token: `...`
25+
//~^HELP use `..` for an exclusive range
26+
//~^^HELP or `..=` for an inclusive range
2727

28-
let x = 5...5; //~ERROR `...` syntax cannot be used in expressions
29-
//~^HELP Use `..` if you need an exclusive range (a < b)
30-
//~^^HELP or `..=` if you need an inclusive range (a <= b)
28+
let x = 5...5; //~ERROR unexpected token: `...`
29+
//~^HELP use `..` for an exclusive range
30+
//~^^HELP or `..=` for an inclusive range
3131

32-
for _ in 0...1 {} //~ERROR `...` syntax cannot be used in expressions
33-
//~^HELP Use `..` if you need an exclusive range (a < b)
34-
//~^^HELP or `..=` if you need an inclusive range (a <= b)
32+
for _ in 0...1 {} //~ERROR unexpected token: `...`
33+
//~^HELP use `..` for an exclusive range
34+
//~^^HELP or `..=` for an inclusive range
3535
}
36-

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 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+
#![deny(trivial_casts, trivial_numeric_casts)]
12+
#![feature(type_ascription)]
13+
14+
fn main() {
15+
let lugubrious = 12i32 as i32;
16+
//~^ ERROR trivial numeric cast
17+
let haunted: &u32 = &99;
18+
let _ = haunted as *const u32;
19+
//~^ ERROR trivial cast
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: trivial numeric cast: `i32` as `i32`
2+
--> $DIR/trivial-casts-featuring-type-ascription.rs:15:22
3+
|
4+
LL | let lugubrious = 12i32 as i32;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/trivial-casts-featuring-type-ascription.rs:11:24
9+
|
10+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
= help: cast can be replaced by coercion; this might require type ascription or a temporary variable
13+
14+
error: trivial cast: `&u32` as `*const u32`
15+
--> $DIR/trivial-casts-featuring-type-ascription.rs:18:13
16+
|
17+
LL | let _ = haunted as *const u32;
18+
| ^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
note: lint level defined here
21+
--> $DIR/trivial-casts-featuring-type-ascription.rs:11:9
22+
|
23+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
24+
| ^^^^^^^^^^^^^
25+
= help: cast can be replaced by coercion; this might require type ascription or a temporary variable
26+
27+
error: aborting due to 2 previous errors
28+

src/test/ui/lint/trivial-casts.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 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+
#![deny(trivial_casts, trivial_numeric_casts)]
12+
13+
fn main() {
14+
let lugubrious = 12i32 as i32;
15+
//~^ ERROR trivial numeric cast
16+
let haunted: &u32 = &99;
17+
let _ = haunted as *const u32;
18+
//~^ ERROR trivial cast
19+
}

src/test/ui/lint/trivial-casts.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: trivial numeric cast: `i32` as `i32`
2+
--> $DIR/trivial-casts.rs:14:22
3+
|
4+
LL | let lugubrious = 12i32 as i32;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/trivial-casts.rs:11:24
9+
|
10+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
= help: cast can be replaced by coercion; this might require a temporary variable
13+
14+
error: trivial cast: `&u32` as `*const u32`
15+
--> $DIR/trivial-casts.rs:17:13
16+
|
17+
LL | let _ = haunted as *const u32;
18+
| ^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
note: lint level defined here
21+
--> $DIR/trivial-casts.rs:11:9
22+
|
23+
LL | #![deny(trivial_casts, trivial_numeric_casts)]
24+
| ^^^^^^^^^^^^^
25+
= help: cast can be replaced by coercion; this might require a temporary variable
26+
27+
error: aborting due to 2 previous errors
28+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 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+
fn main() {
12+
let _redemptive = 1...21;
13+
//~^ ERROR unexpected token
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: unexpected token: `...`
2+
--> $DIR/dotdotdot-expr.rs:12:24
3+
|
4+
LL | let _redemptive = 1...21;
5+
| ^^^
6+
help: use `..` for an exclusive range
7+
|
8+
LL | let _redemptive = 1..21;
9+
| ^^
10+
help: or `..=` for an inclusive range
11+
|
12+
LL | let _redemptive = 1..=21;
13+
| ^^^
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)