Skip to content

Commit 07c6ee6

Browse files
authored
Rollup merge of rust-lang#65914 - estebank:type-alias-bounds-sugg, r=davidtwco
Use structured suggestion for unnecessary bounds in type aliases
2 parents d79fb1f + 58b67c8 commit 07c6ee6

File tree

5 files changed

+109
-28
lines changed

5 files changed

+109
-28
lines changed

src/librustc_lint/builtin.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds {
11251125
.map(|pred| pred.span()).collect();
11261126
let mut err = cx.struct_span_lint(TYPE_ALIAS_BOUNDS, spans,
11271127
"where clauses are not enforced in type aliases");
1128-
err.help("the clause will not be checked when the type alias is used, \
1129-
and should be removed");
1128+
err.span_suggestion(
1129+
type_alias_generics.where_clause.span_for_predicates_or_empty_place(),
1130+
"the clause will not be checked when the type alias is used, and should be removed",
1131+
String::new(),
1132+
Applicability::MachineApplicable,
1133+
);
11301134
if !suggested_changing_assoc_types {
11311135
TypeAliasBounds::suggest_changing_assoc_types(ty, &mut err);
11321136
suggested_changing_assoc_types = true;
@@ -1136,14 +1140,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds {
11361140
// The parameters must not have bounds
11371141
for param in type_alias_generics.params.iter() {
11381142
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
1143+
let suggestion = spans.iter().map(|sp| {
1144+
let start = param.span.between(*sp); // Include the `:` in `T: Bound`.
1145+
(start.to(*sp), String::new())
1146+
}).collect();
11391147
if !spans.is_empty() {
11401148
let mut err = cx.struct_span_lint(
11411149
TYPE_ALIAS_BOUNDS,
11421150
spans,
11431151
"bounds on generic parameters are not enforced in type aliases",
11441152
);
1145-
err.help("the bound will not be checked when the type alias is used, \
1146-
and should be removed");
1153+
let msg = "the bound will not be checked when the type alias is used, \
1154+
and should be removed";
1155+
err.multipart_suggestion(&msg, suggestion, Applicability::MachineApplicable);
11471156
if !suggested_changing_assoc_types {
11481157
TypeAliasBounds::suggest_changing_assoc_types(ty, &mut err);
11491158
suggested_changing_assoc_types = true;

src/test/ui/associated-type-bounds/type-alias.stderr

+48-12
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,129 @@ LL | type _TaWhere1<T> where T: Iterator<Item: Copy> = T;
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `#[warn(type_alias_bounds)]` on by default
8-
= help: the clause will not be checked when the type alias is used, and should be removed
8+
help: the clause will not be checked when the type alias is used, and should be removed
9+
|
10+
LL | type _TaWhere1<T> = T;
11+
| --
912

1013
warning: where clauses are not enforced in type aliases
1114
--> $DIR/type-alias.rs:6:25
1215
|
1316
LL | type _TaWhere2<T> where T: Iterator<Item: 'static> = T;
1417
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1518
|
16-
= help: the clause will not be checked when the type alias is used, and should be removed
19+
help: the clause will not be checked when the type alias is used, and should be removed
20+
|
21+
LL | type _TaWhere2<T> = T;
22+
| --
1723

1824
warning: where clauses are not enforced in type aliases
1925
--> $DIR/type-alias.rs:7:25
2026
|
2127
LL | type _TaWhere3<T> where T: Iterator<Item: 'static> = T;
2228
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2329
|
24-
= help: the clause will not be checked when the type alias is used, and should be removed
30+
help: the clause will not be checked when the type alias is used, and should be removed
31+
|
32+
LL | type _TaWhere3<T> = T;
33+
| --
2534

2635
warning: where clauses are not enforced in type aliases
2736
--> $DIR/type-alias.rs:8:25
2837
|
2938
LL | type _TaWhere4<T> where T: Iterator<Item: 'static + Copy + Send> = T;
3039
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3140
|
32-
= help: the clause will not be checked when the type alias is used, and should be removed
41+
help: the clause will not be checked when the type alias is used, and should be removed
42+
|
43+
LL | type _TaWhere4<T> = T;
44+
| --
3345

3446
warning: where clauses are not enforced in type aliases
3547
--> $DIR/type-alias.rs:9:25
3648
|
3749
LL | type _TaWhere5<T> where T: Iterator<Item: for<'a> Into<&'a u8>> = T;
3850
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3951
|
40-
= help: the clause will not be checked when the type alias is used, and should be removed
52+
help: the clause will not be checked when the type alias is used, and should be removed
53+
|
54+
LL | type _TaWhere5<T> = T;
55+
| --
4156

4257
warning: where clauses are not enforced in type aliases
4358
--> $DIR/type-alias.rs:10:25
4459
|
4560
LL | type _TaWhere6<T> where T: Iterator<Item: Iterator<Item: Copy>> = T;
4661
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4762
|
48-
= help: the clause will not be checked when the type alias is used, and should be removed
63+
help: the clause will not be checked when the type alias is used, and should be removed
64+
|
65+
LL | type _TaWhere6<T> = T;
66+
| --
4967

5068
warning: bounds on generic parameters are not enforced in type aliases
5169
--> $DIR/type-alias.rs:12:20
5270
|
5371
LL | type _TaInline1<T: Iterator<Item: Copy>> = T;
5472
| ^^^^^^^^^^^^^^^^^^^^
5573
|
56-
= help: the bound will not be checked when the type alias is used, and should be removed
74+
help: the bound will not be checked when the type alias is used, and should be removed
75+
|
76+
LL | type _TaInline1<T> = T;
77+
| --
5778

5879
warning: bounds on generic parameters are not enforced in type aliases
5980
--> $DIR/type-alias.rs:13:20
6081
|
6182
LL | type _TaInline2<T: Iterator<Item: 'static>> = T;
6283
| ^^^^^^^^^^^^^^^^^^^^^^^
6384
|
64-
= help: the bound will not be checked when the type alias is used, and should be removed
85+
help: the bound will not be checked when the type alias is used, and should be removed
86+
|
87+
LL | type _TaInline2<T> = T;
88+
| --
6589

6690
warning: bounds on generic parameters are not enforced in type aliases
6791
--> $DIR/type-alias.rs:14:20
6892
|
6993
LL | type _TaInline3<T: Iterator<Item: 'static>> = T;
7094
| ^^^^^^^^^^^^^^^^^^^^^^^
7195
|
72-
= help: the bound will not be checked when the type alias is used, and should be removed
96+
help: the bound will not be checked when the type alias is used, and should be removed
97+
|
98+
LL | type _TaInline3<T> = T;
99+
| --
73100

74101
warning: bounds on generic parameters are not enforced in type aliases
75102
--> $DIR/type-alias.rs:15:20
76103
|
77104
LL | type _TaInline4<T: Iterator<Item: 'static + Copy + Send>> = T;
78105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79106
|
80-
= help: the bound will not be checked when the type alias is used, and should be removed
107+
help: the bound will not be checked when the type alias is used, and should be removed
108+
|
109+
LL | type _TaInline4<T> = T;
110+
| --
81111

82112
warning: bounds on generic parameters are not enforced in type aliases
83113
--> $DIR/type-alias.rs:16:20
84114
|
85115
LL | type _TaInline5<T: Iterator<Item: for<'a> Into<&'a u8>>> = T;
86116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87117
|
88-
= help: the bound will not be checked when the type alias is used, and should be removed
118+
help: the bound will not be checked when the type alias is used, and should be removed
119+
|
120+
LL | type _TaInline5<T> = T;
121+
| --
89122

90123
warning: bounds on generic parameters are not enforced in type aliases
91124
--> $DIR/type-alias.rs:17:20
92125
|
93126
LL | type _TaInline6<T: Iterator<Item: Iterator<Item: Copy>>> = T;
94127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
95128
|
96-
= help: the bound will not be checked when the type alias is used, and should be removed
129+
help: the bound will not be checked when the type alias is used, and should be removed
130+
|
131+
LL | type _TaInline6<T> = T;
132+
| --
97133

src/test/ui/privacy/private-in-public-warn.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,21 @@ LL | pub type Alias<T: PrivTr> = T;
340340
| ^^^^^^
341341
|
342342
= note: `#[warn(type_alias_bounds)]` on by default
343-
= help: the bound will not be checked when the type alias is used, and should be removed
343+
help: the bound will not be checked when the type alias is used, and should be removed
344+
|
345+
LL | pub type Alias<T> = T;
346+
| --
344347

345348
warning: where clauses are not enforced in type aliases
346349
--> $DIR/private-in-public-warn.rs:75:29
347350
|
348351
LL | pub type Alias<T> where T: PrivTr = T;
349352
| ^^^^^^^^^
350353
|
351-
= help: the clause will not be checked when the type alias is used, and should be removed
354+
help: the clause will not be checked when the type alias is used, and should be removed
355+
|
356+
LL | pub type Alias<T> = T;
357+
| --
352358

353359
error: aborting due to 36 previous errors
354360

src/test/ui/trivial-bounds/trivial-bounds-inconsistent.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ LL | type Y where i32: Foo = ();
3131
| ^^^^^^^^
3232
|
3333
= note: `#[warn(type_alias_bounds)]` on by default
34-
= help: the clause will not be checked when the type alias is used, and should be removed
34+
help: the clause will not be checked when the type alias is used, and should be removed
35+
|
36+
LL | type Y = ();
37+
| --
3538

3639
warning: Trait bound i32: Foo does not depend on any type or lifetime parameters
3740
--> $DIR/trivial-bounds-inconsistent.rs:22:19

src/test/ui/type/type-alias-bounds.stderr

+36-9
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,106 @@ LL | type SVec<T: Send + Send> = Vec<T>;
55
| ^^^^ ^^^^
66
|
77
= note: `#[warn(type_alias_bounds)]` on by default
8-
= help: the bound will not be checked when the type alias is used, and should be removed
8+
help: the bound will not be checked when the type alias is used, and should be removed
9+
|
10+
LL | type SVec<T> = Vec<T>;
11+
| -- --
912

1013
warning: where clauses are not enforced in type aliases
1114
--> $DIR/type-alias-bounds.rs:10:21
1215
|
1316
LL | type S2Vec<T> where T: Send = Vec<T>;
1417
| ^^^^^^^
1518
|
16-
= help: the clause will not be checked when the type alias is used, and should be removed
19+
help: the clause will not be checked when the type alias is used, and should be removed
20+
|
21+
LL | type S2Vec<T> = Vec<T>;
22+
| --
1723

1824
warning: bounds on generic parameters are not enforced in type aliases
1925
--> $DIR/type-alias-bounds.rs:12:19
2026
|
2127
LL | type VVec<'b, 'a: 'b + 'b> = (&'b u32, Vec<&'a i32>);
2228
| ^^ ^^
2329
|
24-
= help: the bound will not be checked when the type alias is used, and should be removed
30+
help: the bound will not be checked when the type alias is used, and should be removed
31+
|
32+
LL | type VVec<'b, 'a> = (&'b u32, Vec<&'a i32>);
33+
| -- --
2534

2635
warning: bounds on generic parameters are not enforced in type aliases
2736
--> $DIR/type-alias-bounds.rs:14:18
2837
|
2938
LL | type WVec<'b, T: 'b + 'b> = (&'b u32, Vec<T>);
3039
| ^^ ^^
3140
|
32-
= help: the bound will not be checked when the type alias is used, and should be removed
41+
help: the bound will not be checked when the type alias is used, and should be removed
42+
|
43+
LL | type WVec<'b, T> = (&'b u32, Vec<T>);
44+
| -- --
3345

3446
warning: where clauses are not enforced in type aliases
3547
--> $DIR/type-alias-bounds.rs:16:25
3648
|
3749
LL | type W2Vec<'b, T> where T: 'b, T: 'b = (&'b u32, Vec<T>);
3850
| ^^^^^ ^^^^^
3951
|
40-
= help: the clause will not be checked when the type alias is used, and should be removed
52+
help: the clause will not be checked when the type alias is used, and should be removed
53+
|
54+
LL | type W2Vec<'b, T> = (&'b u32, Vec<T>);
55+
| --
4156

4257
warning: bounds on generic parameters are not enforced in type aliases
4358
--> $DIR/type-alias-bounds.rs:47:12
4459
|
4560
LL | type T1<U: Bound> = U::Assoc;
4661
| ^^^^^
4762
|
48-
= help: the bound will not be checked when the type alias is used, and should be removed
4963
help: use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
5064
--> $DIR/type-alias-bounds.rs:47:21
5165
|
5266
LL | type T1<U: Bound> = U::Assoc;
5367
| ^^^^^^^^
68+
help: the bound will not be checked when the type alias is used, and should be removed
69+
|
70+
LL | type T1<U> = U::Assoc;
71+
| --
5472

5573
warning: where clauses are not enforced in type aliases
5674
--> $DIR/type-alias-bounds.rs:48:18
5775
|
5876
LL | type T2<U> where U: Bound = U::Assoc;
5977
| ^^^^^^^^
6078
|
61-
= help: the clause will not be checked when the type alias is used, and should be removed
6279
help: use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
6380
--> $DIR/type-alias-bounds.rs:48:29
6481
|
6582
LL | type T2<U> where U: Bound = U::Assoc;
6683
| ^^^^^^^^
84+
help: the clause will not be checked when the type alias is used, and should be removed
85+
|
86+
LL | type T2<U> = U::Assoc;
87+
| --
6788

6889
warning: bounds on generic parameters are not enforced in type aliases
6990
--> $DIR/type-alias-bounds.rs:56:12
7091
|
7192
LL | type T5<U: Bound> = <U as Bound>::Assoc;
7293
| ^^^^^
7394
|
74-
= help: the bound will not be checked when the type alias is used, and should be removed
95+
help: the bound will not be checked when the type alias is used, and should be removed
96+
|
97+
LL | type T5<U> = <U as Bound>::Assoc;
98+
| --
7599

76100
warning: bounds on generic parameters are not enforced in type aliases
77101
--> $DIR/type-alias-bounds.rs:57:12
78102
|
79103
LL | type T6<U: Bound> = ::std::vec::Vec<U>;
80104
| ^^^^^
81105
|
82-
= help: the bound will not be checked when the type alias is used, and should be removed
106+
help: the bound will not be checked when the type alias is used, and should be removed
107+
|
108+
LL | type T6<U> = ::std::vec::Vec<U>;
109+
| --
83110

0 commit comments

Comments
 (0)