Skip to content

Commit d37a6d8

Browse files
committed
Suggest usage of angle brackets
1 parent 3235446 commit d37a6d8

10 files changed

+48
-24
lines changed

src/librustc/hir/lowering.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
//! in the HIR, especially for multiple identifiers.
3232
3333
use dep_graph::DepGraph;
34+
use errors::Applicability;
3435
use hir::{self, ParamName};
3536
use hir::HirVec;
3637
use hir::map::{DefKey, DefPathData, Definitions};
@@ -1823,9 +1824,17 @@ impl<'a> LoweringContext<'a> {
18231824
(hir::GenericArgs::none(), true)
18241825
}
18251826
ParenthesizedGenericArgs::Err => {
1826-
struct_span_err!(self.sess, data.span, E0214, "{}", msg)
1827-
.span_label(data.span, "only traits may use parentheses")
1828-
.emit();
1827+
let mut err = struct_span_err!(self.sess, data.span, E0214, "{}", msg);
1828+
err.span_label(data.span, "only traits may use parentheses");
1829+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span) {
1830+
err.span_suggestion_with_applicability(
1831+
data.span,
1832+
"use angle brackets instead",
1833+
format!("<{}>", &snippet[1..snippet.len() - 1]),
1834+
Applicability::MaybeIncorrect,
1835+
);
1836+
};
1837+
err.emit();
18291838
(self.lower_angle_bracketed_parameter_data(
18301839
&data.as_angle_bracketed_args(),
18311840
param_mode,

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2176,11 +2176,11 @@ impl<'a> Parser<'a> {
21762176
style != PathStyle::Mod && self.check(&token::ModSep)
21772177
&& self.look_ahead(1, |t| is_args_start(t)) {
21782178
// Generic arguments are found - `<`, `(`, `::<` or `::(`.
2179-
let lo = self.span;
21802179
if self.eat(&token::ModSep) && style == PathStyle::Type && enable_warning {
21812180
self.diagnostic().struct_span_warn(self.prev_span, "unnecessary path disambiguator")
21822181
.span_label(self.prev_span, "try removing `::`").emit();
21832182
}
2183+
let lo = self.span;
21842184

21852185
let args = if self.eat_lt() {
21862186
// `<'a, T, A = U>`

src/test/ui/error-codes/E0214.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait
22
--> $DIR/E0214.rs:2:15
33
|
44
LL | let v: Vec(&str) = vec!["foo"];
5-
| ^^^^^^ only traits may use parentheses
5+
| ^^^^^^
6+
| |
7+
| only traits may use parentheses
8+
| help: use angle brackets instead: `<&str>`
69

710
error: aborting due to previous error
811

src/test/ui/issues/issue-23589.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait
22
--> $DIR/issue-23589.rs:2:15
33
|
44
LL | let v: Vec(&str) = vec!['1', '2'];
5-
| ^^^^^^ only traits may use parentheses
5+
| ^^^^^^
6+
| |
7+
| only traits may use parentheses
8+
| help: use angle brackets instead: `<&str>`
69

710
error[E0308]: mismatched types
811
--> $DIR/issue-23589.rs:2:29

src/test/ui/issues/issue-32995.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ LL | let b: ::std::boxed()::Box<_> = Box::new(1);
1818
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
1919

2020
error: parenthesized parameters may only be used with a trait
21-
--> $DIR/issue-32995.rs:12:23
21+
--> $DIR/issue-32995.rs:12:25
2222
|
2323
LL | let p = ::std::str::()::from_utf8(b"foo").unwrap();
24-
| ^^^^
24+
| ^^
2525
|
2626
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2727
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
2828

2929
error: parenthesized parameters may only be used with a trait
30-
--> $DIR/issue-32995.rs:16:34
30+
--> $DIR/issue-32995.rs:16:36
3131
|
3232
LL | let p = ::std::str::from_utf8::()(b"foo").unwrap();
33-
| ^^^^
33+
| ^^
3434
|
3535
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3636
= note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
error: field expressions may not have generic arguments
2-
--> $DIR/type-parameters-in-field-exprs.rs:13:8
2+
--> $DIR/type-parameters-in-field-exprs.rs:13:10
33
|
44
LL | f.x::<isize>;
5-
| ^^^^^^^^^
5+
| ^^^^^^^
66

77
error: field expressions may not have generic arguments
8-
--> $DIR/type-parameters-in-field-exprs.rs:15:8
8+
--> $DIR/type-parameters-in-field-exprs.rs:15:10
99
|
1010
LL | f.x::<>;
11-
| ^^^^
11+
| ^^
1212

1313
error: field expressions may not have generic arguments
14-
--> $DIR/type-parameters-in-field-exprs.rs:17:8
14+
--> $DIR/type-parameters-in-field-exprs.rs:17:10
1515
|
1616
LL | f.x::();
17-
| ^^^^
17+
| ^^
1818

1919
error: aborting due to 3 previous errors
2020

src/test/ui/span/macro-ty-params.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error: generic arguments in macro path
2-
--> $DIR/macro-ty-params.rs:10:8
2+
--> $DIR/macro-ty-params.rs:10:10
33
|
44
LL | foo::<T>!(); //~ ERROR generic arguments in macro path
5-
| ^^^^^
5+
| ^^^
66

77
error: generic arguments in macro path
8-
--> $DIR/macro-ty-params.rs:11:8
8+
--> $DIR/macro-ty-params.rs:11:10
99
|
1010
LL | foo::<>!(); //~ ERROR generic arguments in macro path
11-
| ^^^^
11+
| ^^
1212

1313
error: unexpected generic arguments in path
1414
--> $DIR/macro-ty-params.rs:12:8

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-1.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait
22
--> $DIR/unboxed-closure-sugar-used-on-struct-1.rs:8:19
33
|
44
LL | let x: Box<Bar()> = panic!();
5-
| ^^ only traits may use parentheses
5+
| ^^
6+
| |
7+
| only traits may use parentheses
8+
| help: use angle brackets instead: `<>`
69

710
error[E0107]: wrong number of type arguments: expected 1, found 0
811
--> $DIR/unboxed-closure-sugar-used-on-struct-1.rs:8:16

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct-3.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error[E0214]: parenthesized parameters may only be used with a trait
2-
--> $DIR/unboxed-closure-sugar-used-on-struct-3.rs:14:16
2+
--> $DIR/unboxed-closure-sugar-used-on-struct-3.rs:14:18
33
|
44
LL | let b = Bar::(isize, usize)::new(); // OK too (for the parser)
5-
| ^^^^^^^^^^^^^^^^ only traits may use parentheses
5+
| ^^^^^^^^^^^^^^
6+
| |
7+
| only traits may use parentheses
8+
| help: use angle brackets instead: `<isize, usize>`
69

710
error: aborting due to previous error
811

src/test/ui/unboxed-closures/unboxed-closure-sugar-used-on-struct.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0214]: parenthesized parameters may only be used with a trait
22
--> $DIR/unboxed-closure-sugar-used-on-struct.rs:7:18
33
|
44
LL | fn foo(b: Box<Bar()>) {
5-
| ^^ only traits may use parentheses
5+
| ^^
6+
| |
7+
| only traits may use parentheses
8+
| help: use angle brackets instead: `<>`
69

710
error[E0107]: wrong number of type arguments: expected 1, found 0
811
--> $DIR/unboxed-closure-sugar-used-on-struct.rs:7:15

0 commit comments

Comments
 (0)