Skip to content

Commit 2dc0129

Browse files
committed
Allow Self::cmp(self, other) as a correct impl
1 parent d4a6634 commit 2dc0129

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

clippy_lints/src/incorrect_impls.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2+
use clippy_utils::paths::ORD_CMP;
23
use clippy_utils::ty::implements_trait;
3-
use clippy_utils::{get_parent_node, is_res_lang_ctor, last_path_segment, path_res};
4+
use clippy_utils::{get_parent_node, is_res_lang_ctor, last_path_segment, match_def_path, path_res};
45
use rustc_errors::Applicability;
5-
use rustc_hir::def::Res;
6+
use rustc_hir::def_id::LocalDefId;
67
use rustc_hir::{Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, UnOp};
78
use rustc_hir_analysis::hir_ty_to_ty;
89
use rustc_lint::{LateContext, LateLintPass};
@@ -60,6 +61,10 @@ declare_clippy_lint! {
6061
/// wrapping the result of `cmp` in `Some` for `partial_cmp`. Not doing this may silently
6162
/// introduce an error upon refactoring.
6263
///
64+
/// ### Known issues
65+
/// Code like `self.cmp(other).into()` will be flagged as incorrect, despite `.into()` wrapping
66+
/// it in `Some`.
67+
///
6368
/// ### Limitations
6469
/// Will not lint if `Self` and `Rhs` do not have the same type.
6570
///
@@ -202,9 +207,8 @@ impl LateLintPass<'_> for IncorrectImpls {
202207
[cmp_expr],
203208
) = expr.kind
204209
&& is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
205-
&& let ExprKind::MethodCall(cmp_path, _, [other_expr], ..) = cmp_expr.kind
206-
&& cmp_path.ident.name == sym::cmp
207-
&& let Res::Local(..) = path_res(cx, other_expr)
210+
// Fix #11178, allow `Self::cmp(self, ..)` too
211+
&& self_cmp_call(cx, cmp_expr, impl_item.owner_id.def_id)
208212
{} else {
209213
// If `Self` and `Rhs` are not the same type, bail. This makes creating a valid
210214
// suggestion tons more complex.
@@ -242,3 +246,21 @@ impl LateLintPass<'_> for IncorrectImpls {
242246
}
243247
}
244248
}
249+
250+
/// Returns whether this is `self.cmp(..)`, `Self::cmp(self, ..)` or `Ord::cmp(self, ..)`.
251+
fn self_cmp_call<'tcx>(cx: &LateContext<'tcx>, cmp_expr: &'tcx Expr<'tcx>, def_id: LocalDefId) -> bool {
252+
match cmp_expr.kind {
253+
ExprKind::Call(path, [_self, _other]) => path_res(cx, path)
254+
.opt_def_id()
255+
.is_some_and(|def_id| match_def_path(cx, def_id, &ORD_CMP)),
256+
ExprKind::MethodCall(_, _, [_other], ..) => {
257+
// It's a bit annoying but `typeck_results` only gives us the CURRENT body, which we have none, not
258+
// of any `LocalDefId` we want, so we must call the query itself to avoid an immediate ICE
259+
cx.tcx
260+
.typeck(def_id)
261+
.type_dependent_def_id(cmp_expr.hir_id)
262+
.is_some_and(|def_id| match_def_path(cx, def_id, &ORD_CMP))
263+
},
264+
_ => false,
265+
}
266+
}

clippy_utils/src/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,4 @@ pub const OPTION_UNWRAP: [&str; 4] = ["core", "option", "Option", "unwrap"];
161161
pub const OPTION_EXPECT: [&str; 4] = ["core", "option", "Option", "expect"];
162162
pub const FORMATTER: [&str; 3] = ["core", "fmt", "Formatter"];
163163
pub const DEBUG_STRUCT: [&str; 4] = ["core", "fmt", "builders", "DebugStruct"];
164+
pub const ORD_CMP: [&str; 4] = ["core", "cmp", "Ord", "cmp"];

tests/ui/incorrect_partial_ord_impl_on_ord_type.fixed

+32
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,35 @@ impl PartialOrd<u32> for F {
112112
todo!();
113113
}
114114
}
115+
116+
// #11178, do not lint
117+
118+
#[derive(Eq, PartialEq)]
119+
struct G(u32);
120+
121+
impl Ord for G {
122+
fn cmp(&self, other: &Self) -> Ordering {
123+
todo!();
124+
}
125+
}
126+
127+
impl PartialOrd for G {
128+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
129+
Some(Self::cmp(self, other))
130+
}
131+
}
132+
133+
#[derive(Eq, PartialEq)]
134+
struct H(u32);
135+
136+
impl Ord for H {
137+
fn cmp(&self, other: &Self) -> Ordering {
138+
todo!();
139+
}
140+
}
141+
142+
impl PartialOrd for H {
143+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
144+
Some(Ord::cmp(self, other))
145+
}
146+
}

tests/ui/incorrect_partial_ord_impl_on_ord_type.rs

+32
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,35 @@ impl PartialOrd<u32> for F {
116116
todo!();
117117
}
118118
}
119+
120+
// #11178, do not lint
121+
122+
#[derive(Eq, PartialEq)]
123+
struct G(u32);
124+
125+
impl Ord for G {
126+
fn cmp(&self, other: &Self) -> Ordering {
127+
todo!();
128+
}
129+
}
130+
131+
impl PartialOrd for G {
132+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
133+
Some(Self::cmp(self, other))
134+
}
135+
}
136+
137+
#[derive(Eq, PartialEq)]
138+
struct H(u32);
139+
140+
impl Ord for H {
141+
fn cmp(&self, other: &Self) -> Ordering {
142+
todo!();
143+
}
144+
}
145+
146+
impl PartialOrd for H {
147+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
148+
Some(Ord::cmp(self, other))
149+
}
150+
}

0 commit comments

Comments
 (0)