Skip to content

Commit 83248e5

Browse files
authored
Fix messages for type_repetition_in_bounds (#14752)
changelog: [`type_repetition_in_bounds`]: include all generic bounds in suggestion, clearer message which now includes the repeated type name Fixes #14744
2 parents 459364b + c6a23f8 commit 83248e5

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_hir::{
1515
};
1616
use rustc_lint::{LateContext, LateLintPass};
1717
use rustc_session::impl_lint_pass;
18-
use rustc_span::{BytePos, Span};
18+
use rustc_span::Span;
1919

2020
declare_clippy_lint! {
2121
/// ### What it does
@@ -282,18 +282,18 @@ impl TraitBounds {
282282
.iter()
283283
.copied()
284284
.chain(p.bounds.iter())
285-
.filter_map(get_trait_info_from_bound)
286-
.map(|(_, _, span)| snippet_with_applicability(cx, span, "..", &mut applicability))
285+
.map(|bound| snippet_with_applicability(cx, bound.span(), "_", &mut applicability))
287286
.join(" + ");
288287
let hint_string = format!(
289288
"consider combining the bounds: `{}: {trait_bounds}`",
290289
snippet(cx, p.bounded_ty.span, "_"),
291290
);
291+
let ty_name = snippet(cx, p.bounded_ty.span, "_");
292292
span_lint_and_help(
293293
cx,
294294
TYPE_REPETITION_IN_BOUNDS,
295295
bound.span,
296-
"this type has already been used as a bound predicate",
296+
format!("type `{ty_name}` has already been used as a bound predicate"),
297297
None,
298298
hint_string,
299299
);
@@ -395,15 +395,7 @@ impl Hash for ComparableTraitRef<'_, '_> {
395395
fn get_trait_info_from_bound<'a>(bound: &'a GenericBound<'_>) -> Option<(Res, &'a [PathSegment<'a>], Span)> {
396396
if let GenericBound::Trait(t) = bound {
397397
let trait_path = t.trait_ref.path;
398-
let trait_span = {
399-
let path_span = trait_path.span;
400-
if let BoundPolarity::Maybe(_) = t.modifiers.polarity {
401-
path_span.with_lo(path_span.lo() - BytePos(1)) // include the `?`
402-
} else {
403-
path_span
404-
}
405-
};
406-
Some((trait_path.res, trait_path.segments, trait_span))
398+
Some((trait_path.res, trait_path.segments, t.span))
407399
} else {
408400
None
409401
}

tests/ui-toml/type_repetition_in_bounds/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn f2<T>()
1212
where
1313
T: Copy + Clone + Sync + Send + ?Sized,
1414
T: Unpin + PartialEq,
15-
//~^ ERROR: this type has already been used as a bound predicate
15+
//~^ type_repetition_in_bounds
1616
{
1717
}
1818

tests/ui-toml/type_repetition_in_bounds/main.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: this type has already been used as a bound predicate
1+
error: type `T` has already been used as a bound predicate
22
--> tests/ui-toml/type_repetition_in_bounds/main.rs:14:5
33
|
44
LL | T: Unpin + PartialEq,

tests/ui/type_repetition_in_bounds.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn foo<T>(_t: T)
1212
where
1313
T: Copy,
1414
T: Clone,
15-
//~^ ERROR: this type has already been used as a bound predicate
15+
//~^ type_repetition_in_bounds
1616
{
1717
unimplemented!();
1818
}
@@ -30,7 +30,7 @@ trait LintBounds
3030
where
3131
Self: Clone,
3232
Self: Copy + Default + Ord,
33-
//~^ ERROR: this type has already been used as a bound predicate
33+
//~^ type_repetition_in_bounds
3434
Self: Add<Output = Self> + AddAssign + Sub<Output = Self> + SubAssign,
3535
Self: Mul<Output = Self> + MulAssign + Div<Output = Self> + DivAssign,
3636
{
@@ -105,13 +105,13 @@ where
105105
pub fn f<T: ?Sized>()
106106
where
107107
T: Clone,
108-
//~^ ERROR: this type has already been used as a bound predicate
108+
//~^ type_repetition_in_bounds
109109
{
110110
}
111111
pub fn g<T: Clone>()
112112
where
113113
T: ?Sized,
114-
//~^ ERROR: this type has already been used as a bound predicate
114+
//~^ type_repetition_in_bounds
115115
{
116116
}
117117

@@ -137,10 +137,27 @@ mod issue8772_pass {
137137
pub fn f<T: ?Sized, U>(arg: usize)
138138
where
139139
T: Trait<Option<usize>, Box<[String]>, bool> + 'static,
140-
//~^ ERROR: this type has already been used as a bound predicate
140+
//~^ type_repetition_in_bounds
141141
U: Clone + Sync + 'static,
142142
{
143143
}
144144
}
145145

146+
struct Issue14744<'a, K: 'a>
147+
where
148+
K: Clone,
149+
{
150+
phantom: std::marker::PhantomData<&'a K>,
151+
}
152+
//~^^^^ type_repetition_in_bounds
153+
154+
struct ComplexType<T>
155+
where
156+
Vec<T>: Clone,
157+
Vec<T>: Clone,
158+
{
159+
t: T,
160+
}
161+
//~^^^^ type_repetition_in_bounds
162+
146163
fn main() {}
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: this type has already been used as a bound predicate
1+
error: type `T` has already been used as a bound predicate
22
--> tests/ui/type_repetition_in_bounds.rs:14:5
33
|
44
LL | T: Clone,
@@ -11,37 +11,53 @@ note: the lint level is defined here
1111
LL | #![deny(clippy::type_repetition_in_bounds)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

14-
error: this type has already been used as a bound predicate
14+
error: type `Self` has already been used as a bound predicate
1515
--> tests/ui/type_repetition_in_bounds.rs:32:5
1616
|
1717
LL | Self: Copy + Default + Ord,
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
2020
= help: consider combining the bounds: `Self: Clone + Copy + Default + Ord`
2121

22-
error: this type has already been used as a bound predicate
22+
error: type `T` has already been used as a bound predicate
2323
--> tests/ui/type_repetition_in_bounds.rs:107:5
2424
|
2525
LL | T: Clone,
2626
| ^^^^^^^^
2727
|
2828
= help: consider combining the bounds: `T: ?Sized + Clone`
2929

30-
error: this type has already been used as a bound predicate
30+
error: type `T` has already been used as a bound predicate
3131
--> tests/ui/type_repetition_in_bounds.rs:113:5
3232
|
3333
LL | T: ?Sized,
3434
| ^^^^^^^^^
3535
|
3636
= help: consider combining the bounds: `T: Clone + ?Sized`
3737

38-
error: this type has already been used as a bound predicate
38+
error: type `T` has already been used as a bound predicate
3939
--> tests/ui/type_repetition_in_bounds.rs:139:9
4040
|
4141
LL | T: Trait<Option<usize>, Box<[String]>, bool> + 'static,
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4343
|
44-
= help: consider combining the bounds: `T: ?Sized + Trait<Option<usize>, Box<[String]>, bool>`
44+
= help: consider combining the bounds: `T: ?Sized + Trait<Option<usize>, Box<[String]>, bool> + 'static`
4545

46-
error: aborting due to 5 previous errors
46+
error: type `K` has already been used as a bound predicate
47+
--> tests/ui/type_repetition_in_bounds.rs:148:5
48+
|
49+
LL | K: Clone,
50+
| ^^^^^^^^
51+
|
52+
= help: consider combining the bounds: `K: 'a + Clone`
53+
54+
error: type `Vec<T>` has already been used as a bound predicate
55+
--> tests/ui/type_repetition_in_bounds.rs:157:5
56+
|
57+
LL | Vec<T>: Clone,
58+
| ^^^^^^^^^^^^^
59+
|
60+
= help: consider combining the bounds: `Vec<T>: Clone + Clone`
61+
62+
error: aborting due to 7 previous errors
4763

0 commit comments

Comments
 (0)