fix incorrect suggestion for maybe trait bounds

This commit is contained in:
Marcel Hellwig 2022-07-07 12:24:17 +02:00
parent f2bef55389
commit ead2c4f122
3 changed files with 43 additions and 6 deletions

View File

@ -9,12 +9,12 @@ use rustc_data_structures::unhash::UnhashMap;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath, TraitItem, Ty, TyKind,
WherePredicate,
GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, PredicateOrigin, QPath, TraitBoundModifier,
TraitItem, Ty, TyKind, WherePredicate,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;
use rustc_span::{BytePos, Span};
declare_clippy_lint! {
/// ### What it does
@ -242,8 +242,17 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
}
fn get_trait_info_from_bound<'a>(bound: &'a GenericBound<'_>) -> Option<(Res, &'a [PathSegment<'a>], Span)> {
if let GenericBound::Trait(t, _) = bound {
Some((t.trait_ref.path.res, t.trait_ref.path.segments, t.span))
if let GenericBound::Trait(t, tbm) = bound {
let trait_path = t.trait_ref.path;
let trait_span = {
let path_span = trait_path.span;
if let TraitBoundModifier::Maybe = tbm {
path_span.with_lo(path_span.lo() - BytePos(1)) // include the `?`
} else {
path_span
}
};
Some((trait_path.res, trait_path.segments, trait_span))
} else {
None
}

View File

@ -79,6 +79,18 @@ where
u: U,
}
// Check for the `?` in `?Sized`
pub fn f<T: ?Sized>()
where
T: Clone,
{
}
pub fn g<T: Clone>()
where
T: ?Sized,
{
}
// This should not lint
fn impl_trait(_: impl AsRef<str>, _: impl AsRef<str>) {}

View File

@ -19,5 +19,21 @@ LL | Self: Copy + Default + Ord,
|
= help: consider combining the bounds: `Self: Clone + Copy + Default + Ord`
error: aborting due to 2 previous errors
error: this type has already been used as a bound predicate
--> $DIR/type_repetition_in_bounds.rs:85:5
|
LL | T: Clone,
| ^^^^^^^^
|
= help: consider combining the bounds: `T: ?Sized + Clone`
error: this type has already been used as a bound predicate
--> $DIR/type_repetition_in_bounds.rs:90:5
|
LL | T: ?Sized,
| ^^^^^^^^^
|
= help: consider combining the bounds: `T: Clone + ?Sized`
error: aborting due to 4 previous errors