Skip to content

Commit 20f2a89

Browse files
committed
Auto merge of #8224 - Jarcho:type_repetition_in_bounds_8162, r=llogiq
Fix `type_repetition_in_bounds` fixes #7360 fixes #8162 fixes #8056 changelog: Check for full equality in `type_repetition_in_bounds` rather than just equal hashes
2 parents d5dcda2 + 2dd216a commit 20f2a89

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

clippy_lints/src/trait_bounds.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::source::{snippet, snippet_with_applicability};
3-
use clippy_utils::SpanlessHash;
3+
use clippy_utils::{SpanlessEq, SpanlessHash};
4+
use core::hash::{Hash, Hasher};
45
use if_chain::if_chain;
56
use rustc_data_structures::fx::FxHashMap;
67
use rustc_data_structures::unhash::UnhashMap;
78
use rustc_errors::Applicability;
8-
use rustc_hir::{def::Res, GenericBound, Generics, ParamName, Path, QPath, TyKind, WherePredicate};
9+
use rustc_hir::{def::Res, GenericBound, Generics, ParamName, Path, QPath, Ty, TyKind, WherePredicate};
910
use rustc_lint::{LateContext, LateLintPass};
1011
use rustc_session::{declare_tool_lint, impl_lint_pass};
1112
use rustc_span::Span;
@@ -94,24 +95,40 @@ fn get_trait_res_span_from_bound(bound: &GenericBound<'_>) -> Option<(Res, Span)
9495
}
9596

9697
impl TraitBounds {
97-
fn check_type_repetition(self, cx: &LateContext<'_>, gen: &'_ Generics<'_>) {
98+
fn check_type_repetition<'tcx>(self, cx: &LateContext<'tcx>, gen: &'tcx Generics<'_>) {
99+
struct SpanlessTy<'cx, 'tcx> {
100+
ty: &'tcx Ty<'tcx>,
101+
cx: &'cx LateContext<'tcx>,
102+
}
103+
impl PartialEq for SpanlessTy<'_, '_> {
104+
fn eq(&self, other: &Self) -> bool {
105+
let mut eq = SpanlessEq::new(self.cx);
106+
eq.inter_expr().eq_ty(self.ty, other.ty)
107+
}
108+
}
109+
impl Hash for SpanlessTy<'_, '_> {
110+
fn hash<H: Hasher>(&self, h: &mut H) {
111+
let mut t = SpanlessHash::new(self.cx);
112+
t.hash_ty(self.ty);
113+
h.write_u64(t.finish());
114+
}
115+
}
116+
impl Eq for SpanlessTy<'_, '_> {}
117+
98118
if gen.span.from_expansion() {
99119
return;
100120
}
101-
let hash = |ty| -> u64 {
102-
let mut hasher = SpanlessHash::new(cx);
103-
hasher.hash_ty(ty);
104-
hasher.finish()
105-
};
106-
let mut map: UnhashMap<u64, Vec<&GenericBound<'_>>> = UnhashMap::default();
121+
let mut map: UnhashMap<SpanlessTy<'_, '_>, Vec<&GenericBound<'_>>> = UnhashMap::default();
107122
let mut applicability = Applicability::MaybeIncorrect;
108123
for bound in gen.where_clause.predicates {
109124
if_chain! {
110125
if let WherePredicate::BoundPredicate(ref p) = bound;
111126
if p.bounds.len() as u64 <= self.max_trait_bounds;
112127
if !p.span.from_expansion();
113-
let h = hash(p.bounded_ty);
114-
if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>());
128+
if let Some(ref v) = map.insert(
129+
SpanlessTy { ty: p.bounded_ty, cx },
130+
p.bounds.iter().collect::<Vec<_>>()
131+
);
115132

116133
then {
117134
let mut hint_string = format!(

clippy_utils/src/hir_utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl HirEqInterExpr<'_, '_, '_> {
388388
}
389389

390390
#[allow(clippy::similar_names)]
391-
fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
391+
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
392392
match (&left.kind, &right.kind) {
393393
(&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec),
394394
(&TyKind::Array(lt, ref ll_id), &TyKind::Array(rt, ref rl_id)) => {
@@ -845,6 +845,8 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
845845
pub fn hash_path(&mut self, path: &Path<'_>) {
846846
match path.res {
847847
// constant hash since equality is dependant on inter-expression context
848+
// e.g. The expressions `if let Some(x) = foo() {}` and `if let Some(y) = foo() {}` are considered equal
849+
// even though the binding names are different and they have different `HirId`s.
848850
Res::Local(_) => 1_usize.hash(&mut self.s),
849851
_ => {
850852
for seg in path.segments {

tests/ui/type_repetition_in_bounds.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,14 @@ mod issue4326 {
6969
}
7070
}
7171

72+
// Issue #7360
73+
struct Foo<T, U>
74+
where
75+
T: Clone,
76+
U: Clone,
77+
{
78+
t: T,
79+
u: U,
80+
}
81+
7282
fn main() {}

0 commit comments

Comments
 (0)