Skip to content

Commit ead2c4f

Browse files
committed
fix incorrect suggestion for maybe trait bounds
1 parent f2bef55 commit ead2c4f

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use rustc_data_structures::unhash::UnhashMap;
99
use rustc_errors::Applicability;
1010
use rustc_hir::def::Res;
1111
use rustc_hir::{
12-
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath, TraitItem, Ty, TyKind,
13-
WherePredicate,
12+
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath, TraitBoundModifier,
13+
TraitItem, Ty, TyKind, WherePredicate,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_session::{declare_tool_lint, impl_lint_pass};
17-
use rustc_span::Span;
17+
use rustc_span::{BytePos, Span};
1818

1919
declare_clippy_lint! {
2020
/// ### What it does
@@ -242,8 +242,17 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
242242
}
243243

244244
fn get_trait_info_from_bound<'a>(bound: &'a GenericBound<'_>) -> Option<(Res, &'a [PathSegment<'a>], Span)> {
245-
if let GenericBound::Trait(t, _) = bound {
246-
Some((t.trait_ref.path.res, t.trait_ref.path.segments, t.span))
245+
if let GenericBound::Trait(t, tbm) = bound {
246+
let trait_path = t.trait_ref.path;
247+
let trait_span = {
248+
let path_span = trait_path.span;
249+
if let TraitBoundModifier::Maybe = tbm {
250+
path_span.with_lo(path_span.lo() - BytePos(1)) // include the `?`
251+
} else {
252+
path_span
253+
}
254+
};
255+
Some((trait_path.res, trait_path.segments, trait_span))
247256
} else {
248257
None
249258
}

tests/ui/type_repetition_in_bounds.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ where
7979
u: U,
8080
}
8181

82+
// Check for the `?` in `?Sized`
83+
pub fn f<T: ?Sized>()
84+
where
85+
T: Clone,
86+
{
87+
}
88+
pub fn g<T: Clone>()
89+
where
90+
T: ?Sized,
91+
{
92+
}
93+
8294
// This should not lint
8395
fn impl_trait(_: impl AsRef<str>, _: impl AsRef<str>) {}
8496

tests/ui/type_repetition_in_bounds.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,21 @@ LL | Self: Copy + Default + Ord,
1919
|
2020
= help: consider combining the bounds: `Self: Clone + Copy + Default + Ord`
2121

22-
error: aborting due to 2 previous errors
22+
error: this type has already been used as a bound predicate
23+
--> $DIR/type_repetition_in_bounds.rs:85:5
24+
|
25+
LL | T: Clone,
26+
| ^^^^^^^^
27+
|
28+
= help: consider combining the bounds: `T: ?Sized + Clone`
29+
30+
error: this type has already been used as a bound predicate
31+
--> $DIR/type_repetition_in_bounds.rs:90:5
32+
|
33+
LL | T: ?Sized,
34+
| ^^^^^^^^^
35+
|
36+
= help: consider combining the bounds: `T: Clone + ?Sized`
37+
38+
error: aborting due to 4 previous errors
2339

0 commit comments

Comments
 (0)