Skip to content

Commit 89c37e3

Browse files
committed
use other instead of self
1 parent 9ff890d commit 89c37e3

14 files changed

+202
-284
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4861,6 +4861,7 @@ Released 2018-09-13
48614861
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
48624862
[`inconsistent_struct_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_struct_constructor
48634863
[`incorrect_clone_impl_on_copy_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type
4864+
[`incorrect_partial_ord_impl_on_ord_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_partial_ord_impl_on_ord_type
48644865
[`index_refutable_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#index_refutable_slice
48654866
[`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
48664867
[`ineffective_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_bit_mask
@@ -5046,7 +5047,6 @@ Released 2018-09-13
50465047
[`needless_option_as_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref
50475048
[`needless_option_take`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_take
50485049
[`needless_parens_on_range_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_parens_on_range_literals
5049-
[`needless_partial_ord_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_partial_ord_impl
50505050
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
50515051
[`needless_pub_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pub_self
50525052
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark

clippy_lints/src/declared_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
207207
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
208208
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
209209
crate::incorrect_impls::INCORRECT_CLONE_IMPL_ON_COPY_TYPE_INFO,
210+
crate::incorrect_impls::INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE_INFO,
210211
crate::index_refutable_slice::INDEX_REFUTABLE_SLICE_INFO,
211212
crate::indexing_slicing::INDEXING_SLICING_INFO,
212213
crate::indexing_slicing::OUT_OF_BOUNDS_INDEXING_INFO,
@@ -466,7 +467,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
466467
crate::needless_else::NEEDLESS_ELSE_INFO,
467468
crate::needless_for_each::NEEDLESS_FOR_EACH_INFO,
468469
crate::needless_if::NEEDLESS_IF_INFO,
469-
crate::needless_impls::NEEDLESS_PARTIAL_ORD_IMPL_INFO,
470470
crate::needless_late_init::NEEDLESS_LATE_INIT_INFO,
471471
crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO,
472472
crate::needless_pass_by_value::NEEDLESS_PASS_BY_VALUE_INFO,

clippy_lints/src/incorrect_impls.rs

+124-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, get_parent_node, last_path_segment, ty::implements_trait};
1+
use clippy_utils::{
2+
diagnostics::{span_lint_and_sugg, span_lint_and_then},
3+
get_parent_node, is_res_lang_ctor, last_path_segment, path_res,
4+
ty::implements_trait,
5+
};
26
use rustc_errors::Applicability;
3-
use rustc_hir::{ExprKind, ImplItem, ImplItemKind, ItemKind, Node, UnOp};
7+
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, PatKind, UnOp};
48
use rustc_hir_analysis::hir_ty_to_ty;
59
use rustc_lint::{LateContext, LateLintPass};
610
use rustc_middle::ty::EarlyBinder;
711
use rustc_session::{declare_lint_pass, declare_tool_lint};
812
use rustc_span::{sym, symbol};
13+
use std::borrow::Cow;
914

1015
declare_clippy_lint! {
1116
/// ### What it does
@@ -46,10 +51,59 @@ declare_clippy_lint! {
4651
correctness,
4752
"manual implementation of `Clone` on a `Copy` type"
4853
}
49-
declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE]);
54+
declare_clippy_lint! {
55+
/// ### What it does
56+
/// Checks for manual implementations of both `PartialOrd` and `Ord` when only `Ord` is
57+
/// necessary.
58+
///
59+
/// ### Why is this bad?
60+
/// If both `PartialOrd` and `Ord` are implemented, they must agree. This is commonly done by
61+
/// wrapping the result of `cmp` in `Some` for `partial_cmp`. Not doing this may silently
62+
/// introduce an error upon refactoring.
63+
///
64+
/// ### Example
65+
/// ```rust,ignore
66+
/// #[derive(Eq, PartialEq)]
67+
/// struct A(u32);
68+
///
69+
/// impl Ord for A {
70+
/// fn cmp(&self, other: &Self) -> Ordering {
71+
/// todo!();
72+
/// }
73+
/// }
74+
///
75+
/// impl PartialOrd for A {
76+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
77+
/// todo!();
78+
/// }
79+
/// }
80+
/// ```
81+
/// Use instead:
82+
/// ```rust,ignore
83+
/// #[derive(Eq, PartialEq)]
84+
/// struct A(u32);
85+
///
86+
/// impl Ord for A {
87+
/// fn cmp(&self, other: &Self) -> Ordering {
88+
/// todo!();
89+
/// }
90+
/// }
91+
///
92+
/// impl PartialOrd for A {
93+
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
94+
/// Some(self.cmp(other))
95+
/// }
96+
/// }
97+
/// ```
98+
#[clippy::version = "1.72.0"]
99+
pub INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
100+
correctness,
101+
"manual implementation of `PartialOrd` when `Ord` is already implemented"
102+
}
103+
declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE, INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE]);
50104

51105
impl LateLintPass<'_> for IncorrectImpls {
52-
#[expect(clippy::needless_return)]
106+
#[expect(clippy::too_many_lines)]
53107
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
54108
let node = get_parent_node(cx.tcx, impl_item.hir_id());
55109
let Some(Node::Item(item)) = node else {
@@ -72,10 +126,7 @@ impl LateLintPass<'_> for IncorrectImpls {
72126
let ExprKind::Block(block, ..) = body.value.kind else {
73127
return;
74128
};
75-
// Above is duplicated from the `duplicate_manual_partial_ord_impl` branch.
76-
// Remove it while solving conflicts once that PR is merged.
77129

78-
// Actual implementation; remove this comment once aforementioned PR is merged
79130
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl_def_id)
80131
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)
81132
&& implements_trait(
@@ -120,5 +171,71 @@ impl LateLintPass<'_> for IncorrectImpls {
120171
return;
121172
}
122173
}
174+
175+
if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl_def_id)
176+
&& impl_item.ident.name == sym::partial_cmp
177+
&& let Some(ord_def_id) = cx
178+
.tcx
179+
.diagnostic_items(trait_impl.def_id.krate)
180+
.name_to_id
181+
.get(&sym::Ord)
182+
&& implements_trait(
183+
cx,
184+
hir_ty_to_ty(cx.tcx, imp.self_ty),
185+
*ord_def_id,
186+
trait_impl.substs,
187+
)
188+
{
189+
if block.stmts.is_empty()
190+
&& let Some(expr) = block.expr
191+
&& let ExprKind::Call(
192+
Expr {
193+
kind: ExprKind::Path(some_path),
194+
hir_id: some_hir_id,
195+
..
196+
},
197+
[cmp_expr],
198+
) = expr.kind
199+
&& is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
200+
&& let ExprKind::MethodCall(cmp_path, _, [other_expr], ..) = cmp_expr.kind
201+
&& cmp_path.ident.name == sym::cmp
202+
&& let Res::Local(..) = path_res(cx, other_expr)
203+
{} else {
204+
// If lhs and rhs are not the same type, bail. This makes creating a valid
205+
// suggestion tons more complex.
206+
if let Some(lhs) = trait_impl.substs.get(0)
207+
&& let Some(rhs) = trait_impl.substs.get(1)
208+
&& lhs != rhs
209+
{
210+
return;
211+
}
212+
213+
span_lint_and_then(
214+
cx,
215+
INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
216+
item.span,
217+
"incorrect implementation of `partial_cmp` on an `Ord` type",
218+
|diag| {
219+
let (help, app) = if let Some(other) = body.params.get(1)
220+
&& let PatKind::Binding(_, _, other_ident, ..) = other.pat.kind
221+
{
222+
(
223+
Cow::Owned(format!("{{ Some(self.cmp({})) }}", other_ident.name)),
224+
Applicability::Unspecified,
225+
)
226+
} else {
227+
(Cow::Borrowed("{ Some(self.cmp(...)) }"), Applicability::HasPlaceholders)
228+
};
229+
230+
diag.span_suggestion(
231+
block.span,
232+
"change this to",
233+
help,
234+
app,
235+
);
236+
}
237+
);
238+
}
239+
}
123240
}
124241
}

clippy_lints/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ mod needless_continue;
227227
mod needless_else;
228228
mod needless_for_each;
229229
mod needless_if;
230-
mod needless_impls;
231230
mod needless_late_init;
232231
mod needless_parens_on_range_literals;
233232
mod needless_pass_by_value;

clippy_lints/src/needless_impls.rs

-145
This file was deleted.

tests/ui/bool_comparison.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(clippy::needless_if)]
44
#![warn(clippy::bool_comparison)]
5-
#![allow(clippy::needless_partial_ord_impl)]
5+
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
66

77
fn main() {
88
let x = true;

tests/ui/bool_comparison.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(clippy::needless_if)]
44
#![warn(clippy::bool_comparison)]
5-
#![allow(clippy::needless_partial_ord_impl)]
5+
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
66

77
fn main() {
88
let x = true;

tests/ui/derive.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#![allow(clippy::incorrect_clone_impl_on_copy_type, dead_code)]
1+
#![allow(
2+
clippy::incorrect_clone_impl_on_copy_type,
3+
clippy::incorrect_partial_ord_impl_on_ord_type,
4+
dead_code
5+
)]
26
#![warn(clippy::expl_impl_clone_on_copy)]
37

48
#[derive(Copy)]

0 commit comments

Comments
 (0)