Skip to content

Commit 918c0d5

Browse files
committed
check earlier for PartialEq where Rhs != Self
1 parent a14be49 commit 918c0d5

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

clippy_lints/src/non_canonical_impls.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_errors::Applicability;
77
use rustc_hir::def_id::LocalDefId;
88
use rustc_hir::{Block, Body, Expr, ExprKind, ImplItem, ImplItemKind, Item, LangItem, Node, UnOp};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
10-
use rustc_middle::ty::{EarlyBinder, TraitRef};
10+
use rustc_middle::ty::EarlyBinder;
1111
use rustc_session::declare_lint_pass;
1212
use rustc_span::sym;
1313
use rustc_span::symbol::kw;
@@ -131,11 +131,18 @@ impl LateLintPass<'_> for NonCanonicalImpls {
131131
{
132132
check_clone_on_copy(cx, impl_item, block);
133133
} else if trait_name == Some(sym::PartialOrd)
134+
// If `Self` and `Rhs` are not the same type, then a corresponding `Ord` impl is not possible,
135+
// since it doesn't have an `Rhs`
136+
&& match trait_impl.args.as_slice() {
137+
[_] => true,
138+
[lhs, rhs] => lhs == rhs,
139+
_ => false,
140+
}
134141
&& impl_item.ident.name == sym::partial_cmp
135142
&& let Some(ord_def_id) = cx.tcx.get_diagnostic_item(sym::Ord)
136143
&& implements_trait(cx, trait_impl.self_ty(), ord_def_id, &[])
137144
{
138-
check_partial_ord_on_ord(cx, impl_item, item, &trait_impl, body, block);
145+
check_partial_ord_on_ord(cx, impl_item, item, body, block);
139146
}
140147
}
141148
}
@@ -181,7 +188,6 @@ fn check_partial_ord_on_ord<'tcx>(
181188
cx: &LateContext<'tcx>,
182189
impl_item: &ImplItem<'_>,
183190
item: &Item<'_>,
184-
trait_impl: &TraitRef<'_>,
185191
body: &Body<'_>,
186192
block: &Block<'tcx>,
187193
) {
@@ -207,13 +213,6 @@ fn check_partial_ord_on_ord<'tcx>(
207213
{
208214
return;
209215
}
210-
// If `Self` and `Rhs` are not the same type, bail. This makes creating a valid
211-
// suggestion tons more complex.
212-
else if let [lhs, rhs, ..] = trait_impl.args.as_slice()
213-
&& lhs != rhs
214-
{
215-
return;
216-
}
217216

218217
span_lint_and_then(
219218
cx,

0 commit comments

Comments
 (0)