Skip to content

Commit 16880f7

Browse files
committed
check earlier for PartialEq where Rhs != Self
1 parent 097f2fd commit 16880f7

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

clippy_lints/src/non_canonical_impls.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::{is_from_proc_macro, last_path_segment, std_or_core};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Block, Body, Expr, ExprKind, ImplItem, ImplItemKind, Item, LangItem, Node, UnOp};
77
use rustc_lint::{LateContext, LateLintPass, LintContext};
8-
use rustc_middle::ty::{EarlyBinder, TraitRef, TypeckResults};
8+
use rustc_middle::ty::{EarlyBinder, TypeckResults};
99
use rustc_session::declare_lint_pass;
1010
use rustc_span::sym;
1111
use rustc_span::symbol::kw;
@@ -129,11 +129,14 @@ impl LateLintPass<'_> for NonCanonicalImpls {
129129
{
130130
check_clone_on_copy(cx, impl_item, block);
131131
} else if trait_name == Some(sym::PartialOrd)
132+
// If `Self` and `Rhs` are not the same type, then a corresponding `Ord` impl is not possible,
133+
// since it doesn't have an `Rhs`
134+
&& let [lhs, rhs] = trait_impl.args.as_slice() && lhs == rhs
132135
&& impl_item.ident.name == sym::partial_cmp
133136
&& let Some(ord_def_id) = cx.tcx.get_diagnostic_item(sym::Ord)
134137
&& implements_trait(cx, trait_impl.self_ty(), ord_def_id, &[])
135138
{
136-
check_partial_ord_on_ord(cx, impl_item, item, &trait_impl, body, block);
139+
check_partial_ord_on_ord(cx, impl_item, item, body, block);
137140
}
138141
}
139142
}
@@ -179,7 +182,6 @@ fn check_partial_ord_on_ord<'tcx>(
179182
cx: &LateContext<'tcx>,
180183
impl_item: &ImplItem<'_>,
181184
item: &Item<'_>,
182-
trait_impl: &TraitRef<'_>,
183185
body: &Body<'_>,
184186
block: &Block<'tcx>,
185187
) {
@@ -205,13 +207,6 @@ fn check_partial_ord_on_ord<'tcx>(
205207
{
206208
return;
207209
}
208-
// If `Self` and `Rhs` are not the same type, bail. This makes creating a valid
209-
// suggestion tons more complex.
210-
else if let [lhs, rhs, ..] = trait_impl.args.as_slice()
211-
&& lhs != rhs
212-
{
213-
return;
214-
}
215210

216211
span_lint_and_then(
217212
cx,

0 commit comments

Comments
 (0)