Skip to content

Commit c5c1a77

Browse files
committed
use other instead of self
1 parent 041d07e commit c5c1a77

12 files changed

+81
-128
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4833,6 +4833,7 @@ Released 2018-09-13
48334833
[`imprecise_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#imprecise_flops
48344834
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
48354835
[`inconsistent_struct_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_struct_constructor
4836+
[`incorrect_partial_ord_impl_on_ord_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_partial_ord_impl_on_ord_type
48364837
[`index_refutable_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#index_refutable_slice
48374838
[`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
48384839
[`ineffective_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_bit_mask
@@ -5017,7 +5018,6 @@ Released 2018-09-13
50175018
[`needless_option_as_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref
50185019
[`needless_option_take`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_take
50195020
[`needless_parens_on_range_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_parens_on_range_literals
5020-
[`needless_partial_ord_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_partial_ord_impl
50215021
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
50225022
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
50235023
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop

clippy_lints/src/declared_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
206206
crate::implicit_saturating_add::IMPLICIT_SATURATING_ADD_INFO,
207207
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
208208
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
209+
crate::incorrect_impls::INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE_INFO,
209210
crate::index_refutable_slice::INDEX_REFUTABLE_SLICE_INFO,
210211
crate::indexing_slicing::INDEXING_SLICING_INFO,
211212
crate::indexing_slicing::OUT_OF_BOUNDS_INDEXING_INFO,
@@ -463,7 +464,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
463464
crate::needless_else::NEEDLESS_ELSE_INFO,
464465
crate::needless_for_each::NEEDLESS_FOR_EACH_INFO,
465466
crate::needless_if::NEEDLESS_IF_INFO,
466-
crate::needless_impls::NEEDLESS_PARTIAL_ORD_IMPL_INFO,
467467
crate::needless_late_init::NEEDLESS_LATE_INIT_INFO,
468468
crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO,
469469
crate::needless_pass_by_value::NEEDLESS_PASS_BY_VALUE_INFO,

clippy_lints/src/needless_impls.rs renamed to clippy_lints/src/incorrect_impls.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ declare_clippy_lint! {
5555
/// }
5656
/// ```
5757
#[clippy::version = "1.72.0"]
58-
pub NEEDLESS_PARTIAL_ORD_IMPL,
58+
pub INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
5959
correctness,
6060
"manual implementation of `PartialOrd` when `Ord` is already implemented"
6161
}
62-
declare_lint_pass!(NeedlessImpls => [NEEDLESS_PARTIAL_ORD_IMPL]);
62+
declare_lint_pass!(IncorrectImpls => [INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE]);
6363

64-
impl LateLintPass<'_> for NeedlessImpls {
64+
impl LateLintPass<'_> for IncorrectImpls {
6565
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
6666
let node = get_parent_node(cx.tcx, impl_item.hir_id());
6767
let Some(Node::Item(item)) = node else {
@@ -114,13 +114,22 @@ impl LateLintPass<'_> for NeedlessImpls {
114114
&& cmp_path.ident.name == sym::cmp
115115
&& let Res::Local(..) = path_res(cx, other_expr)
116116
{} else {
117+
// If lhs and rhs are not the same type, bail. This makes creating a valid
118+
// suggestion tons more complex.
119+
if let Some(lhs) = trait_impl.substs.get(0)
120+
&& let Some(rhs) = trait_impl.substs.get(1)
121+
&& lhs != rhs
122+
{
123+
return;
124+
}
125+
117126
span_lint_and_then(
118127
cx,
119-
NEEDLESS_PARTIAL_ORD_IMPL,
128+
INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
120129
item.span,
121-
"manual implementation of `PartialOrd` when `Ord` is already implemented",
130+
"incorrect implementation of `partial_cmp` on an `Ord` type",
122131
|diag| {
123-
let (help, app) = if let Some(other) = body.params.get(0)
132+
let (help, app) = if let Some(other) = body.params.get(1)
124133
&& let PatKind::Binding(_, _, other_ident, ..) = other.pat.kind
125134
{
126135
(

clippy_lints/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ mod implicit_return;
150150
mod implicit_saturating_add;
151151
mod implicit_saturating_sub;
152152
mod inconsistent_struct_constructor;
153+
mod incorrect_impls;
153154
mod index_refutable_slice;
154155
mod indexing_slicing;
155156
mod infinite_iter;
@@ -226,7 +227,6 @@ mod needless_continue;
226227
mod needless_else;
227228
mod needless_for_each;
228229
mod needless_if;
229-
mod needless_impls;
230230
mod needless_late_init;
231231
mod needless_parens_on_range_literals;
232232
mod needless_pass_by_value;
@@ -1048,6 +1048,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10481048
let stack_size_threshold = conf.stack_size_threshold;
10491049
store.register_late_pass(move |_| Box::new(large_stack_frames::LargeStackFrames::new(stack_size_threshold)));
10501050
store.register_late_pass(|_| Box::new(single_range_in_vec_init::SingleRangeInVecInit));
1051+
store.register_late_pass(|_| Box::new(incorrect_impls::IncorrectImpls));
10511052
// add lints here, do not remove this comment, it's used in `new_lint`
10521053
}
10531054

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(clippy::needless_partial_ord_impl, dead_code)]
1+
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type, dead_code)]
22
#![warn(clippy::expl_impl_clone_on_copy)]
33

44
#[derive(Copy)]

tests/ui/derive_ord_xor_partial_ord.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![warn(clippy::derive_ord_xor_partial_ord)]
22
#![allow(clippy::unnecessary_wraps)]
3-
#![allow(clippy::needless_partial_ord_impl)]
3+
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
44

55
use std::cmp::Ordering;
66

tests/ui/needless_partial_ord_impl.rs renamed to tests/ui/incorrect_partial_ord_impl_on_ord_type.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@run-rustfix
21
#![allow(unused)]
32
#![no_main]
43

@@ -51,7 +50,7 @@ impl Ord for C {
5150

5251
impl PartialOrd for C {
5352
fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
54-
todo!();
53+
todo!(); // don't run rustfix, or else this will cause it to fail to compile
5554
}
5655
}
5756

@@ -87,3 +86,32 @@ impl<A: Ord + PartialOrd> PartialOrd for Uwu<A> {
8786
todo!();
8887
}
8988
}
89+
90+
// do not lint since `Rhs` is not `Self`
91+
92+
#[derive(Eq, PartialEq)]
93+
struct F(u32);
94+
95+
impl Ord for F {
96+
fn cmp(&self, other: &Self) -> Ordering {
97+
todo!();
98+
}
99+
}
100+
101+
impl PartialOrd for F {
102+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
103+
Some(self.cmp(other))
104+
}
105+
}
106+
107+
impl PartialEq<u32> for F {
108+
fn eq(&self, other: &u32) -> bool {
109+
todo!();
110+
}
111+
}
112+
113+
impl PartialOrd<u32> for F {
114+
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
115+
todo!();
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: incorrect implementation of `partial_cmp` on an `Ord` type
2+
--> $DIR/incorrect_partial_ord_impl_on_ord_type.rs:17:1
3+
|
4+
LL | / impl PartialOrd for A {
5+
LL | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
6+
| | _____________________________________________________________-
7+
LL | || todo!();
8+
LL | || }
9+
| ||_____- help: change this to: `{ Some(self.cmp(other)) }`
10+
LL | | }
11+
| |__^
12+
|
13+
= note: `#[deny(clippy::incorrect_partial_ord_impl_on_ord_type)]` on by default
14+
15+
error: incorrect implementation of `partial_cmp` on an `Ord` type
16+
--> $DIR/incorrect_partial_ord_impl_on_ord_type.rs:51:1
17+
|
18+
LL | / impl PartialOrd for C {
19+
LL | | fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
20+
| | _________________________________________________________-
21+
LL | || todo!(); // don't run rustfix, or else this will cause it to fail to compile
22+
LL | || }
23+
| ||_____- help: change this to: `{ Some(self.cmp(...)) }`
24+
LL | | }
25+
| |__^
26+
27+
error: aborting due to 2 previous errors
28+

tests/ui/needless_partial_ord_impl.fixed

-85
This file was deleted.

tests/ui/needless_partial_ord_impl.stderr

-28
This file was deleted.

0 commit comments

Comments
 (0)