Skip to content

Commit

Permalink
fixup! Support trait bounds with type parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Hsu committed May 7, 2022
1 parent c9c31f3 commit 89192b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
40 changes: 19 additions & 21 deletions clippy_lints/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use rustc_data_structures::unhash::UnhashMap;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{
GenericArg, GenericBound, GenericBound, Generics, Item, ItemKind, Node, ParamName, Path, PathSegment, QPath,
TraitItem, Ty, TyKind, WherePredicate,
GenericArg, GenericBound, Generics, Item, ItemKind, Node, Path, PathSegment, QPath, TraitItem, Ty, TyKind,
WherePredicate,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
Expand Down Expand Up @@ -95,7 +95,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.62.0"]
pub REPEATED_WHERE_CLAUSE_OR_TRAIT_BOUND,
pedantic,
nursery,
"Traits are repeated within trait bounds or where clause"
}

Expand Down Expand Up @@ -282,27 +282,25 @@ fn check_trait_bound_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
}

#[derive(PartialEq, Eq, Hash, Debug)]
struct ComparableBound(Res, Vec<Res>, Vec<ComparableBound>);
struct ComparableBound(
Res,
Vec<Res>,
// Vec<ComparableBound>
);

fn check_bounds_or_where_duplication(cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
if gen.span.from_expansion() {
return;
}

for param in gen.params {
if let ParamName::Plain(_) = param.name {
// other alternatives are errors and elided which won't have duplicates
rollup_traits(cx, param.bounds, "this trait bound contains repeated elements");
}
}

for predicate in gen.where_clause.predicates {
for predicate in gen.predicates {
if let WherePredicate::BoundPredicate(ref bound_predicate) = predicate {
rollup_traits(
cx,
bound_predicate.bounds,
"this where clause contains repeated elements",
);
let msg = if predicate.in_where_clause() {
"these where clauses contain repeated elements"
} else {
"these bounds contain repeated elements"
};
rollup_traits(cx, bound_predicate.bounds, msg);
}
}
}
Expand Down Expand Up @@ -336,10 +334,10 @@ fn try_into_comparable_bound(bound: &GenericBound<'_>) -> Option<ComparableBound
})
.flatten()
.collect(),
t.bound_generic_params
.iter()
.flat_map(|param| param.bounds.iter().filter_map(try_into_comparable_bound))
.collect(),
// t.bound_generic_params
// .iter()
// .flat_map(|param| param.bounds.iter().filter_map(try_into_comparable_bound))
// .collect(),
))
} else {
None
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/repeated_where_clause_or_trait_bound.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: this trait bound contains repeated elements
error: these bounds contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:6:15
|
LL | fn bad_foo<T: Clone + Clone + Clone + Copy, U: Clone + Copy>(arg0: T, argo1: U) {
Expand All @@ -10,31 +10,31 @@ note: the lint level is defined here
LL | #![deny(clippy::repeated_where_clause_or_trait_bound)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: this where clause contains repeated elements
error: these where clauses contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:12:8
|
LL | T: Clone + Clone + Clone + Copy,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone + Copy`

error: this where clause contains repeated elements
error: these where clauses contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:47:15
|
LL | Self: Clone + Clone + Clone;
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone`

error: this trait bound contains repeated elements
error: these bounds contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:61:24
|
LL | trait BadTraitBound<T: Clone + Clone + Clone + Copy, U: Clone + Copy> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone + Copy`

error: this where clause contains repeated elements
error: these where clauses contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:68:12
|
LL | T: Clone + Clone + Clone + Copy,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Clone + Copy`

error: this trait bound contains repeated elements
error: these bounds contain repeated elements
--> $DIR/repeated_where_clause_or_trait_bound.rs:101:19
|
LL | fn bad_generic<T: GenericTrait<u64> + GenericTrait<u32> + GenericTrait<u64>>(arg0: T) {
Expand Down

0 comments on commit 89192b9

Please sign in to comment.