Skip to content

Commit 3f9201f

Browse files
committed
ok now it does
1 parent 77d37af commit 3f9201f

8 files changed

+64
-48
lines changed

clippy_lints/src/manual_partial_ord_and_ord_impl.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir_analysis::hir_ty_to_ty;
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::def_id::DefId;
10+
use rustc_span::sym;
1011
use std::cell::OnceCell;
1112

1213
declare_clippy_lint! {
@@ -19,7 +20,7 @@ declare_clippy_lint! {
1920
/// `Ord::cmp` in `Some`. Not doing this may silently introduce an error upon refactoring.
2021
///
2122
/// ### Example
22-
/// ```rust
23+
/// ```rust,ignore
2324
/// #[derive(Eq, PartialEq)]
2425
/// struct A(u32);
2526
///
@@ -36,7 +37,7 @@ declare_clippy_lint! {
3637
/// }
3738
/// ```
3839
/// Use instead:
39-
/// ```rust
40+
/// ```rust,ignore
4041
/// #[derive(Eq, PartialEq)]
4142
/// struct A(u32);
4243
///
@@ -55,7 +56,7 @@ declare_clippy_lint! {
5556
#[clippy::version = "1.71.0"]
5657
pub MANUAL_PARTIAL_ORD_AND_ORD_IMPL,
5758
correctness,
58-
"default lint description"
59+
"manual implementation of `PartialOrd` when `Ord` is already implemented"
5960
}
6061
impl_lint_pass!(ManualPartialOrdAndOrdImpl => [MANUAL_PARTIAL_ORD_AND_ORD_IMPL]);
6162

@@ -69,18 +70,22 @@ impl LateLintPass<'_> for ManualPartialOrdAndOrdImpl {
6970
if_chain! {
7071
if let ItemKind::Impl(imp) = &item.kind;
7172
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id);
72-
if cx.tcx.is_diagnostic_item(sym!(PartialOrd), impl_trait_ref.skip_binder().def_id);
73+
let partial_ord_def_id = impl_trait_ref.skip_binder().def_id;
74+
if cx.tcx.is_diagnostic_item(sym::PartialOrd, partial_ord_def_id);
75+
if !cx.tcx.is_automatically_derived(item.owner_id.to_def_id());
7376
then {
7477
lint_impl_body(self, cx, imp, item);
7578
}
7679
}
7780
}
7881
}
7982

83+
#[allow(clippy::unnecessary_def_path)] // ???
84+
// ^ line 91, that can't be changed without causing compilation to fail
8085
fn lint_impl_body(conf: &mut ManualPartialOrdAndOrdImpl, cx: &LateContext<'_>, imp: &Impl<'_>, item: &Item<'_>) {
8186
for imp_item in imp.items {
8287
if_chain! {
83-
if imp_item.ident.name == sym!(partial_cmp);
88+
if imp_item.ident.name == sym::partial_cmp;
8489
if let ImplItemKind::Fn(_, id) = cx.tcx.hir().impl_item(imp_item.id).kind;
8590
then {
8691
let body = cx.tcx.hir().body(id);
@@ -94,9 +99,9 @@ fn lint_impl_body(conf: &mut ManualPartialOrdAndOrdImpl, cx: &LateContext<'_>, i
9499
if let ExprKind::Call(Expr { kind: ExprKind::Path(path), ..}, [cmp_expr]) = expr.kind;
95100
if let QPath::Resolved(_, some_path) = path;
96101
if let Some(some_seg_one) = some_path.segments.get(0);
97-
if some_seg_one.ident.name == sym!(Some);
102+
if some_seg_one.ident.name == sym::Some;
98103
if let ExprKind::MethodCall(cmp_path, _, [other_expr], ..) = cmp_expr.kind;
99-
if cmp_path.ident.name == sym!(cmp);
104+
if cmp_path.ident.name == sym::cmp;
100105
if let Res::Local(..) = path_res(cx, other_expr);
101106
then {}
102107
else {

tests/ui/bool_comparison.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@run-rustfix
22

33
#![warn(clippy::bool_comparison)]
4+
#![allow(clippy::manual_partial_ord_and_ord_impl)]
45

56
fn main() {
67
let x = true;

tests/ui/bool_comparison.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@run-rustfix
22

33
#![warn(clippy::bool_comparison)]
4+
#![allow(clippy::manual_partial_ord_and_ord_impl)]
45

56
fn main() {
67
let x = true;

tests/ui/bool_comparison.stderr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,133 @@
11
error: equality checks against true are unnecessary
2-
--> $DIR/bool_comparison.rs:7:8
2+
--> $DIR/bool_comparison.rs:8:8
33
|
44
LL | if x == true {
55
| ^^^^^^^^^ help: try simplifying it as shown: `x`
66
|
77
= note: `-D clippy::bool-comparison` implied by `-D warnings`
88

99
error: equality checks against false can be replaced by a negation
10-
--> $DIR/bool_comparison.rs:12:8
10+
--> $DIR/bool_comparison.rs:13:8
1111
|
1212
LL | if x == false {
1313
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
1414

1515
error: equality checks against true are unnecessary
16-
--> $DIR/bool_comparison.rs:17:8
16+
--> $DIR/bool_comparison.rs:18:8
1717
|
1818
LL | if true == x {
1919
| ^^^^^^^^^ help: try simplifying it as shown: `x`
2020

2121
error: equality checks against false can be replaced by a negation
22-
--> $DIR/bool_comparison.rs:22:8
22+
--> $DIR/bool_comparison.rs:23:8
2323
|
2424
LL | if false == x {
2525
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
2626

2727
error: inequality checks against true can be replaced by a negation
28-
--> $DIR/bool_comparison.rs:27:8
28+
--> $DIR/bool_comparison.rs:28:8
2929
|
3030
LL | if x != true {
3131
| ^^^^^^^^^ help: try simplifying it as shown: `!x`
3232

3333
error: inequality checks against false are unnecessary
34-
--> $DIR/bool_comparison.rs:32:8
34+
--> $DIR/bool_comparison.rs:33:8
3535
|
3636
LL | if x != false {
3737
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
3838

3939
error: inequality checks against true can be replaced by a negation
40-
--> $DIR/bool_comparison.rs:37:8
40+
--> $DIR/bool_comparison.rs:38:8
4141
|
4242
LL | if true != x {
4343
| ^^^^^^^^^ help: try simplifying it as shown: `!x`
4444

4545
error: inequality checks against false are unnecessary
46-
--> $DIR/bool_comparison.rs:42:8
46+
--> $DIR/bool_comparison.rs:43:8
4747
|
4848
LL | if false != x {
4949
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
5050

5151
error: less than comparison against true can be replaced by a negation
52-
--> $DIR/bool_comparison.rs:47:8
52+
--> $DIR/bool_comparison.rs:48:8
5353
|
5454
LL | if x < true {
5555
| ^^^^^^^^ help: try simplifying it as shown: `!x`
5656

5757
error: greater than checks against false are unnecessary
58-
--> $DIR/bool_comparison.rs:52:8
58+
--> $DIR/bool_comparison.rs:53:8
5959
|
6060
LL | if false < x {
6161
| ^^^^^^^^^ help: try simplifying it as shown: `x`
6262

6363
error: greater than checks against false are unnecessary
64-
--> $DIR/bool_comparison.rs:57:8
64+
--> $DIR/bool_comparison.rs:58:8
6565
|
6666
LL | if x > false {
6767
| ^^^^^^^^^ help: try simplifying it as shown: `x`
6868

6969
error: less than comparison against true can be replaced by a negation
70-
--> $DIR/bool_comparison.rs:62:8
70+
--> $DIR/bool_comparison.rs:63:8
7171
|
7272
LL | if true > x {
7373
| ^^^^^^^^ help: try simplifying it as shown: `!x`
7474

7575
error: order comparisons between booleans can be simplified
76-
--> $DIR/bool_comparison.rs:68:8
76+
--> $DIR/bool_comparison.rs:69:8
7777
|
7878
LL | if x < y {
7979
| ^^^^^ help: try simplifying it as shown: `!x & y`
8080

8181
error: order comparisons between booleans can be simplified
82-
--> $DIR/bool_comparison.rs:73:8
82+
--> $DIR/bool_comparison.rs:74:8
8383
|
8484
LL | if x > y {
8585
| ^^^^^ help: try simplifying it as shown: `x & !y`
8686

8787
error: this comparison might be written more concisely
88-
--> $DIR/bool_comparison.rs:121:8
88+
--> $DIR/bool_comparison.rs:122:8
8989
|
9090
LL | if a == !b {};
9191
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9292

9393
error: this comparison might be written more concisely
94-
--> $DIR/bool_comparison.rs:122:8
94+
--> $DIR/bool_comparison.rs:123:8
9595
|
9696
LL | if !a == b {};
9797
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9898

9999
error: this comparison might be written more concisely
100-
--> $DIR/bool_comparison.rs:126:8
100+
--> $DIR/bool_comparison.rs:127:8
101101
|
102102
LL | if b == !a {};
103103
| ^^^^^^^ help: try simplifying it as shown: `b != a`
104104

105105
error: this comparison might be written more concisely
106-
--> $DIR/bool_comparison.rs:127:8
106+
--> $DIR/bool_comparison.rs:128:8
107107
|
108108
LL | if !b == a {};
109109
| ^^^^^^^ help: try simplifying it as shown: `b != a`
110110

111111
error: equality checks against false can be replaced by a negation
112-
--> $DIR/bool_comparison.rs:151:8
112+
--> $DIR/bool_comparison.rs:152:8
113113
|
114114
LL | if false == m!(func) {}
115115
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
116116

117117
error: equality checks against false can be replaced by a negation
118-
--> $DIR/bool_comparison.rs:152:8
118+
--> $DIR/bool_comparison.rs:153:8
119119
|
120120
LL | if m!(func) == false {}
121121
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
122122

123123
error: equality checks against true are unnecessary
124-
--> $DIR/bool_comparison.rs:153:8
124+
--> $DIR/bool_comparison.rs:154:8
125125
|
126126
LL | if true == m!(func) {}
127127
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
128128

129129
error: equality checks against true are unnecessary
130-
--> $DIR/bool_comparison.rs:154:8
130+
--> $DIR/bool_comparison.rs:155:8
131131
|
132132
LL | if m!(func) == true {}
133133
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`

tests/ui/derive_ord_xor_partial_ord.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::derive_ord_xor_partial_ord)]
22
#![allow(clippy::unnecessary_wraps)]
3+
#![allow(clippy::manual_partial_ord_and_ord_impl)]
34

45
use std::cmp::Ordering;
56

tests/ui/derive_ord_xor_partial_ord.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
error: you are deriving `Ord` but have implemented `PartialOrd` explicitly
2-
--> $DIR/derive_ord_xor_partial_ord.rs:21:10
2+
--> $DIR/derive_ord_xor_partial_ord.rs:22:10
33
|
44
LL | #[derive(Ord, PartialEq, Eq)]
55
| ^^^
66
|
77
note: `PartialOrd` implemented here
8-
--> $DIR/derive_ord_xor_partial_ord.rs:24:1
8+
--> $DIR/derive_ord_xor_partial_ord.rs:25:1
99
|
1010
LL | impl PartialOrd for DeriveOrd {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: `-D clippy::derive-ord-xor-partial-ord` implied by `-D warnings`
1313
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

1515
error: you are deriving `Ord` but have implemented `PartialOrd` explicitly
16-
--> $DIR/derive_ord_xor_partial_ord.rs:30:10
16+
--> $DIR/derive_ord_xor_partial_ord.rs:31:10
1717
|
1818
LL | #[derive(Ord, PartialEq, Eq)]
1919
| ^^^
2020
|
2121
note: `PartialOrd` implemented here
22-
--> $DIR/derive_ord_xor_partial_ord.rs:33:1
22+
--> $DIR/derive_ord_xor_partial_ord.rs:34:1
2323
|
2424
LL | impl PartialOrd<DeriveOrdWithExplicitTypeVariable> for DeriveOrdWithExplicitTypeVariable {
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
2727

2828
error: you are implementing `Ord` explicitly but have derived `PartialOrd`
29-
--> $DIR/derive_ord_xor_partial_ord.rs:42:1
29+
--> $DIR/derive_ord_xor_partial_ord.rs:43:1
3030
|
3131
LL | / impl std::cmp::Ord for DerivePartialOrd {
3232
LL | | fn cmp(&self, other: &Self) -> Ordering {
@@ -36,14 +36,14 @@ LL | | }
3636
| |_^
3737
|
3838
note: `PartialOrd` implemented here
39-
--> $DIR/derive_ord_xor_partial_ord.rs:39:10
39+
--> $DIR/derive_ord_xor_partial_ord.rs:40:10
4040
|
4141
LL | #[derive(PartialOrd, PartialEq, Eq)]
4242
| ^^^^^^^^^^
4343
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
4444

4545
error: you are implementing `Ord` explicitly but have derived `PartialOrd`
46-
--> $DIR/derive_ord_xor_partial_ord.rs:62:5
46+
--> $DIR/derive_ord_xor_partial_ord.rs:63:5
4747
|
4848
LL | / impl Ord for DerivePartialOrdInUseOrd {
4949
LL | | fn cmp(&self, other: &Self) -> Ordering {
@@ -53,7 +53,7 @@ LL | | }
5353
| |_____^
5454
|
5555
note: `PartialOrd` implemented here
56-
--> $DIR/derive_ord_xor_partial_ord.rs:59:14
56+
--> $DIR/derive_ord_xor_partial_ord.rs:60:14
5757
|
5858
LL | #[derive(PartialOrd, PartialEq, Eq)]
5959
| ^^^^^^^^^^

tests/ui/manual_partial_ord_and_ord_impl.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(unused)]
2-
#![warn(clippy::manual_partial_ord_impl)]
2+
#![warn(clippy::manual_partial_ord_and_ord_impl)]
33
#![no_main]
44

55
use std::cmp::Ordering;
@@ -54,3 +54,19 @@ impl PartialOrd for C {
5454
todo!();
5555
}
5656
}
57+
58+
// do not lint derived
59+
60+
#[derive(Eq, Ord, PartialEq, PartialOrd)]
61+
struct D(u32);
62+
63+
// do not lint if ord is not manually implemented
64+
65+
#[derive(Eq, PartialEq)]
66+
struct E(u32);
67+
68+
impl PartialOrd for E {
69+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
70+
todo!();
71+
}
72+
}

tests/ui/manual_partial_ord_and_ord_impl.stderr

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: unknown lint: `clippy::manual_partial_ord_impl`
2-
--> $DIR/manual_partial_ord_and_ord_impl.rs:2:9
3-
|
4-
LL | #![warn(clippy::manual_partial_ord_impl)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `clippy::manual_partial_ord_and_ord_impl`
6-
|
7-
= note: `-D unknown-lints` implied by `-D warnings`
8-
91
error: manual implementation of `PartialOrd` when `Ord` is already implemented
102
--> $DIR/manual_partial_ord_and_ord_impl.rs:18:1
113
|
@@ -18,7 +10,7 @@ LL | || }
1810
LL | | }
1911
| |__^
2012
|
21-
= note: `#[deny(clippy::manual_partial_ord_and_ord_impl)]` on by default
13+
= note: `-D clippy::manual-partial-ord-and-ord-impl` implied by `-D warnings`
2214

2315
error: manual implementation of `PartialOrd` when `Ord` is already implemented
2416
--> $DIR/manual_partial_ord_and_ord_impl.rs:52:1
@@ -32,5 +24,5 @@ LL | | }
3224
|
3325
= help: return the value of `self.cmp` wrapped in `Some` instead
3426

35-
error: aborting due to 3 previous errors
27+
error: aborting due to 2 previous errors
3628

0 commit comments

Comments
 (0)