Skip to content

Commit

Permalink
Rollup merge of #65914 - estebank:type-alias-bounds-sugg, r=davidtwco
Browse files Browse the repository at this point in the history
Use structured suggestion for unnecessary bounds in type aliases
  • Loading branch information
tmandry committed Oct 30, 2019
2 parents d34aa17 + 58b67c8 commit 40f8f56
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
17 changes: 13 additions & 4 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds {
.map(|pred| pred.span()).collect();
let mut err = cx.struct_span_lint(TYPE_ALIAS_BOUNDS, spans,
"where clauses are not enforced in type aliases");
err.help("the clause will not be checked when the type alias is used, \
and should be removed");
err.span_suggestion(
type_alias_generics.where_clause.span_for_predicates_or_empty_place(),
"the clause will not be checked when the type alias is used, and should be removed",
String::new(),
Applicability::MachineApplicable,
);
if !suggested_changing_assoc_types {
TypeAliasBounds::suggest_changing_assoc_types(ty, &mut err);
suggested_changing_assoc_types = true;
Expand All @@ -1136,14 +1140,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds {
// The parameters must not have bounds
for param in type_alias_generics.params.iter() {
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
let suggestion = spans.iter().map(|sp| {
let start = param.span.between(*sp); // Include the `:` in `T: Bound`.
(start.to(*sp), String::new())
}).collect();
if !spans.is_empty() {
let mut err = cx.struct_span_lint(
TYPE_ALIAS_BOUNDS,
spans,
"bounds on generic parameters are not enforced in type aliases",
);
err.help("the bound will not be checked when the type alias is used, \
and should be removed");
let msg = "the bound will not be checked when the type alias is used, \
and should be removed";
err.multipart_suggestion(&msg, suggestion, Applicability::MachineApplicable);
if !suggested_changing_assoc_types {
TypeAliasBounds::suggest_changing_assoc_types(ty, &mut err);
suggested_changing_assoc_types = true;
Expand Down
60 changes: 48 additions & 12 deletions src/test/ui/associated-type-bounds/type-alias.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,129 @@ LL | type _TaWhere1<T> where T: Iterator<Item: Copy> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(type_alias_bounds)]` on by default
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type _TaWhere1<T> = T;
| --

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias.rs:6:25
|
LL | type _TaWhere2<T> where T: Iterator<Item: 'static> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type _TaWhere2<T> = T;
| --

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias.rs:7:25
|
LL | type _TaWhere3<T> where T: Iterator<Item: 'static> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type _TaWhere3<T> = T;
| --

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

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias.rs:9:25
|
LL | type _TaWhere5<T> where T: Iterator<Item: for<'a> Into<&'a u8>> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type _TaWhere5<T> = T;
| --

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias.rs:10:25
|
LL | type _TaWhere6<T> where T: Iterator<Item: Iterator<Item: Copy>> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type _TaWhere6<T> = T;
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias.rs:12:20
|
LL | type _TaInline1<T: Iterator<Item: Copy>> = T;
| ^^^^^^^^^^^^^^^^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type _TaInline1<T> = T;
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias.rs:13:20
|
LL | type _TaInline2<T: Iterator<Item: 'static>> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type _TaInline2<T> = T;
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias.rs:14:20
|
LL | type _TaInline3<T: Iterator<Item: 'static>> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type _TaInline3<T> = T;
| --

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

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias.rs:16:20
|
LL | type _TaInline5<T: Iterator<Item: for<'a> Into<&'a u8>>> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type _TaInline5<T> = T;
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias.rs:17:20
|
LL | type _TaInline6<T: Iterator<Item: Iterator<Item: Copy>>> = T;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type _TaInline6<T> = T;
| --

10 changes: 8 additions & 2 deletions src/test/ui/privacy/private-in-public-warn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,21 @@ LL | pub type Alias<T: PrivTr> = T;
| ^^^^^^
|
= note: `#[warn(type_alias_bounds)]` on by default
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | pub type Alias<T> = T;
| --

warning: where clauses are not enforced in type aliases
--> $DIR/private-in-public-warn.rs:75:29
|
LL | pub type Alias<T> where T: PrivTr = T;
| ^^^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | pub type Alias<T> = T;
| --

error: aborting due to 36 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ LL | type Y where i32: Foo = ();
| ^^^^^^^^
|
= note: `#[warn(type_alias_bounds)]` on by default
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type Y = ();
| --

warning: Trait bound i32: Foo does not depend on any type or lifetime parameters
--> $DIR/trivial-bounds-inconsistent.rs:22:19
Expand Down
45 changes: 36 additions & 9 deletions src/test/ui/type/type-alias-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,106 @@ LL | type SVec<T: Send + Send> = Vec<T>;
| ^^^^ ^^^^
|
= note: `#[warn(type_alias_bounds)]` on by default
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type SVec<T> = Vec<T>;
| -- --

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias-bounds.rs:10:21
|
LL | type S2Vec<T> where T: Send = Vec<T>;
| ^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type S2Vec<T> = Vec<T>;
| --

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

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

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias-bounds.rs:16:25
|
LL | type W2Vec<'b, T> where T: 'b, T: 'b = (&'b u32, Vec<T>);
| ^^^^^ ^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type W2Vec<'b, T> = (&'b u32, Vec<T>);
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias-bounds.rs:47:12
|
LL | type T1<U: Bound> = U::Assoc;
| ^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
--> $DIR/type-alias-bounds.rs:47:21
|
LL | type T1<U: Bound> = U::Assoc;
| ^^^^^^^^
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type T1<U> = U::Assoc;
| --

warning: where clauses are not enforced in type aliases
--> $DIR/type-alias-bounds.rs:48:18
|
LL | type T2<U> where U: Bound = U::Assoc;
| ^^^^^^^^
|
= help: the clause will not be checked when the type alias is used, and should be removed
help: use fully disambiguated paths (i.e., `<T as Trait>::Assoc`) to refer to associated types in type aliases
--> $DIR/type-alias-bounds.rs:48:29
|
LL | type T2<U> where U: Bound = U::Assoc;
| ^^^^^^^^
help: the clause will not be checked when the type alias is used, and should be removed
|
LL | type T2<U> = U::Assoc;
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias-bounds.rs:56:12
|
LL | type T5<U: Bound> = <U as Bound>::Assoc;
| ^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type T5<U> = <U as Bound>::Assoc;
| --

warning: bounds on generic parameters are not enforced in type aliases
--> $DIR/type-alias-bounds.rs:57:12
|
LL | type T6<U: Bound> = ::std::vec::Vec<U>;
| ^^^^^
|
= help: the bound will not be checked when the type alias is used, and should be removed
help: the bound will not be checked when the type alias is used, and should be removed
|
LL | type T6<U> = ::std::vec::Vec<U>;
| --

0 comments on commit 40f8f56

Please sign in to comment.