Skip to content

Commit ec6f1bd

Browse files
committed
Auto merge of rust-lang#11358 - Alexendoo:incorrect-to-manual-impls, r=Jarcho
Rename incorrect_impls to non_canonical_impls, move them to warn by default The wording/category of these feel too strong to me, I would expect most of the time it's linting the implementations aren't going to be *incorrect*, just unnecessary changelog: rename `incorrect_clone_impl_on_copy_type` to [`non_canonical_clone_impl`] changelog: rename `incorrect_partial_ord_impl_on_ord_type` to [`non_canonical_partial_ord_impl`] changelog: Move [`non_canonical_clone_impl`], [`non_canonical_partial_ord_impl`] to suspicious
2 parents 27165ac + b99921a commit ec6f1bd

24 files changed

+153
-139
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5206,6 +5206,8 @@ Released 2018-09-13
52065206
[`no_effect_underscore_binding`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_effect_underscore_binding
52075207
[`no_mangle_with_rust_abi`]: https://rust-lang.github.io/rust-clippy/master/index.html#no_mangle_with_rust_abi
52085208
[`non_ascii_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_ascii_literal
5209+
[`non_canonical_clone_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_canonical_clone_impl
5210+
[`non_canonical_partial_ord_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_canonical_partial_ord_impl
52095211
[`non_minimal_cfg`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_minimal_cfg
52105212
[`non_octal_unix_permissions`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_octal_unix_permissions
52115213
[`non_send_fields_in_send_ty`]: https://rust-lang.github.io/rust-clippy/master/index.html#non_send_fields_in_send_ty

clippy_lints/src/declared_lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
211211
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
212212
crate::implied_bounds_in_impls::IMPLIED_BOUNDS_IN_IMPLS_INFO,
213213
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
214-
crate::incorrect_impls::INCORRECT_CLONE_IMPL_ON_COPY_TYPE_INFO,
215-
crate::incorrect_impls::INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE_INFO,
216214
crate::index_refutable_slice::INDEX_REFUTABLE_SLICE_INFO,
217215
crate::indexing_slicing::INDEXING_SLICING_INFO,
218216
crate::indexing_slicing::OUT_OF_BOUNDS_INDEXING_INFO,
@@ -498,6 +496,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
498496
crate::no_effect::NO_EFFECT_UNDERSCORE_BINDING_INFO,
499497
crate::no_effect::UNNECESSARY_OPERATION_INFO,
500498
crate::no_mangle_with_rust_abi::NO_MANGLE_WITH_RUST_ABI_INFO,
499+
crate::non_canonical_impls::NON_CANONICAL_CLONE_IMPL_INFO,
500+
crate::non_canonical_impls::NON_CANONICAL_PARTIAL_ORD_IMPL_INFO,
501501
crate::non_copy_const::BORROW_INTERIOR_MUTABLE_CONST_INFO,
502502
crate::non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST_INFO,
503503
crate::non_expressive_names::JUST_UNDERSCORES_AND_DIGITS_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ mod implicit_saturating_add;
154154
mod implicit_saturating_sub;
155155
mod implied_bounds_in_impls;
156156
mod inconsistent_struct_constructor;
157-
mod incorrect_impls;
158157
mod index_refutable_slice;
159158
mod indexing_slicing;
160159
mod infinite_iter;
@@ -244,6 +243,7 @@ mod neg_multiply;
244243
mod new_without_default;
245244
mod no_effect;
246245
mod no_mangle_with_rust_abi;
246+
mod non_canonical_impls;
247247
mod non_copy_const;
248248
mod non_expressive_names;
249249
mod non_octal_unix_permissions;
@@ -1070,7 +1070,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10701070
avoid_breaking_exported_api,
10711071
))
10721072
});
1073-
store.register_late_pass(|_| Box::new(incorrect_impls::IncorrectImpls));
1073+
store.register_late_pass(|_| Box::new(non_canonical_impls::NonCanonicalImpls));
10741074
store.register_late_pass(move |_| {
10751075
Box::new(single_call_fn::SingleCallFn {
10761076
avoid_breaking_exported_api,

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ use rustc_span::symbol::kw;
1414

1515
declare_clippy_lint! {
1616
/// ### What it does
17-
/// Checks for manual implementations of `Clone` when `Copy` is already implemented.
17+
/// Checks for non-canonical implementations of `Clone` when `Copy` is already implemented.
1818
///
1919
/// ### Why is this bad?
20-
/// If both `Clone` and `Copy` are implemented, they must agree. This is done by dereferencing
21-
/// `self` in `Clone`'s implementation. Anything else is incorrect.
20+
/// If both `Clone` and `Copy` are implemented, they must agree. This can done by dereferencing
21+
/// `self` in `Clone`'s implementation, which will avoid any possibility of the implementations
22+
/// becoming out of sync.
2223
///
2324
/// ### Example
2425
/// ```rust,ignore
@@ -47,26 +48,22 @@ declare_clippy_lint! {
4748
/// impl Copy for A {}
4849
/// ```
4950
#[clippy::version = "1.72.0"]
50-
pub INCORRECT_CLONE_IMPL_ON_COPY_TYPE,
51-
correctness,
52-
"manual implementation of `Clone` on a `Copy` type"
51+
pub NON_CANONICAL_CLONE_IMPL,
52+
suspicious,
53+
"non-canonical implementation of `Clone` on a `Copy` type"
5354
}
5455
declare_clippy_lint! {
5556
/// ### What it does
56-
/// Checks for manual implementations of both `PartialOrd` and `Ord` when only `Ord` is
57-
/// necessary.
57+
/// Checks for non-canonical implementations of `PartialOrd` when `Ord` is already implemented.
5858
///
5959
/// ### Why is this bad?
6060
/// If both `PartialOrd` and `Ord` are implemented, they must agree. This is commonly done by
6161
/// wrapping the result of `cmp` in `Some` for `partial_cmp`. Not doing this may silently
6262
/// introduce an error upon refactoring.
6363
///
6464
/// ### Known issues
65-
/// Code that calls the `.into()` method instead will be flagged as incorrect, despite `.into()`
66-
/// wrapping it in `Some`.
67-
///
68-
/// ### Limitations
69-
/// Will not lint if `Self` and `Rhs` do not have the same type.
65+
/// Code that calls the `.into()` method instead will be flagged, despite `.into()` wrapping it
66+
/// in `Some`.
7067
///
7168
/// ### Example
7269
/// ```rust
@@ -108,13 +105,13 @@ declare_clippy_lint! {
108105
/// }
109106
/// ```
110107
#[clippy::version = "1.72.0"]
111-
pub INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
112-
correctness,
113-
"manual implementation of `PartialOrd` when `Ord` is already implemented"
108+
pub NON_CANONICAL_PARTIAL_ORD_IMPL,
109+
suspicious,
110+
"non-canonical implementation of `PartialOrd` on an `Ord` type"
114111
}
115-
declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE, INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE]);
112+
declare_lint_pass!(NonCanonicalImpls => [NON_CANONICAL_CLONE_IMPL, NON_CANONICAL_PARTIAL_ORD_IMPL]);
116113

117-
impl LateLintPass<'_> for IncorrectImpls {
114+
impl LateLintPass<'_> for NonCanonicalImpls {
118115
#[expect(clippy::too_many_lines)]
119116
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
120117
let Some(Node::Item(item)) = get_parent_node(cx.tcx, impl_item.hir_id()) else {
@@ -155,9 +152,9 @@ impl LateLintPass<'_> for IncorrectImpls {
155152
{} else {
156153
span_lint_and_sugg(
157154
cx,
158-
INCORRECT_CLONE_IMPL_ON_COPY_TYPE,
155+
NON_CANONICAL_CLONE_IMPL,
159156
block.span,
160-
"incorrect implementation of `clone` on a `Copy` type",
157+
"non-canonical implementation of `clone` on a `Copy` type",
161158
"change this to",
162159
"{ *self }".to_owned(),
163160
Applicability::MaybeIncorrect,
@@ -170,9 +167,9 @@ impl LateLintPass<'_> for IncorrectImpls {
170167
if impl_item.ident.name == sym::clone_from {
171168
span_lint_and_sugg(
172169
cx,
173-
INCORRECT_CLONE_IMPL_ON_COPY_TYPE,
170+
NON_CANONICAL_CLONE_IMPL,
174171
impl_item.span,
175-
"incorrect implementation of `clone_from` on a `Copy` type",
172+
"unnecessary implementation of `clone_from` on a `Copy` type",
176173
"remove it",
177174
String::new(),
178175
Applicability::MaybeIncorrect,
@@ -218,9 +215,9 @@ impl LateLintPass<'_> for IncorrectImpls {
218215

219216
span_lint_and_then(
220217
cx,
221-
INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
218+
NON_CANONICAL_PARTIAL_ORD_IMPL,
222219
item.span,
223-
"incorrect implementation of `partial_cmp` on an `Ord` type",
220+
"non-canonical implementation of `partial_cmp` on an `Ord` type",
224221
|diag| {
225222
let [_, other] = body.params else {
226223
return;

clippy_lints/src/renamed_lints.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
1515
("clippy::eval_order_dependence", "clippy::mixed_read_write_in_expression"),
1616
("clippy::identity_conversion", "clippy::useless_conversion"),
1717
("clippy::if_let_some_result", "clippy::match_result_ok"),
18+
("clippy::incorrect_clone_impl_on_copy_type", "clippy::non_canonical_clone_impl"),
19+
("clippy::incorrect_partial_ord_impl_on_ord_type", "clippy::non_canonical_partial_ord_impl"),
1820
("clippy::integer_arithmetic", "clippy::arithmetic_side_effects"),
1921
("clippy::logic_bug", "clippy::overly_complex_bool_expr"),
2022
("clippy::new_without_default_derive", "clippy::new_without_default"),
@@ -38,12 +40,12 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
3840
("clippy::drop_bounds", "drop_bounds"),
3941
("clippy::drop_copy", "dropping_copy_types"),
4042
("clippy::drop_ref", "dropping_references"),
43+
("clippy::fn_null_check", "useless_ptr_null_checks"),
4144
("clippy::for_loop_over_option", "for_loops_over_fallibles"),
4245
("clippy::for_loop_over_result", "for_loops_over_fallibles"),
4346
("clippy::for_loops_over_fallibles", "for_loops_over_fallibles"),
4447
("clippy::forget_copy", "forgetting_copy_types"),
4548
("clippy::forget_ref", "forgetting_references"),
46-
("clippy::fn_null_check", "useless_ptr_null_checks"),
4749
("clippy::into_iter_on_array", "array_into_iter"),
4850
("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"),
4951
("clippy::invalid_ref", "invalid_value"),

tests/ui/bool_comparison.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::needless_if)]
22
#![warn(clippy::bool_comparison)]
3-
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
3+
#![allow(clippy::non_canonical_partial_ord_impl)]
44

55
fn main() {
66
let x = true;

tests/ui/bool_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::needless_if)]
22
#![warn(clippy::bool_comparison)]
3-
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
3+
#![allow(clippy::non_canonical_partial_ord_impl)]
44

55
fn main() {
66
let x = true;

tests/ui/clone_on_copy_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(clippy::incorrect_clone_impl_on_copy_type)]
1+
#![allow(clippy::non_canonical_clone_impl)]
22

33
use std::fmt;
44
use std::marker::PhantomData;

tests/ui/derive.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#![allow(
2-
clippy::incorrect_clone_impl_on_copy_type,
3-
clippy::incorrect_partial_ord_impl_on_ord_type,
4-
dead_code
5-
)]
1+
#![allow(clippy::non_canonical_clone_impl, clippy::non_canonical_partial_ord_impl, dead_code)]
62
#![warn(clippy::expl_impl_clone_on_copy)]
73

84
#[derive(Copy)]

tests/ui/derive.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you are implementing `Clone` explicitly on a `Copy` type
2-
--> $DIR/derive.rs:11:1
2+
--> $DIR/derive.rs:7:1
33
|
44
LL | / impl Clone for Qux {
55
LL | |
@@ -10,7 +10,7 @@ LL | | }
1010
| |_^
1111
|
1212
note: consider deriving `Clone` or removing `Copy`
13-
--> $DIR/derive.rs:11:1
13+
--> $DIR/derive.rs:7:1
1414
|
1515
LL | / impl Clone for Qux {
1616
LL | |
@@ -23,7 +23,7 @@ LL | | }
2323
= help: to override `-D warnings` add `#[allow(clippy::expl_impl_clone_on_copy)]`
2424

2525
error: you are implementing `Clone` explicitly on a `Copy` type
26-
--> $DIR/derive.rs:36:1
26+
--> $DIR/derive.rs:32:1
2727
|
2828
LL | / impl<'a> Clone for Lt<'a> {
2929
LL | |
@@ -34,7 +34,7 @@ LL | | }
3434
| |_^
3535
|
3636
note: consider deriving `Clone` or removing `Copy`
37-
--> $DIR/derive.rs:36:1
37+
--> $DIR/derive.rs:32:1
3838
|
3939
LL | / impl<'a> Clone for Lt<'a> {
4040
LL | |
@@ -45,7 +45,7 @@ LL | | }
4545
| |_^
4646

4747
error: you are implementing `Clone` explicitly on a `Copy` type
48-
--> $DIR/derive.rs:48:1
48+
--> $DIR/derive.rs:44:1
4949
|
5050
LL | / impl Clone for BigArray {
5151
LL | |
@@ -56,7 +56,7 @@ LL | | }
5656
| |_^
5757
|
5858
note: consider deriving `Clone` or removing `Copy`
59-
--> $DIR/derive.rs:48:1
59+
--> $DIR/derive.rs:44:1
6060
|
6161
LL | / impl Clone for BigArray {
6262
LL | |
@@ -67,7 +67,7 @@ LL | | }
6767
| |_^
6868

6969
error: you are implementing `Clone` explicitly on a `Copy` type
70-
--> $DIR/derive.rs:60:1
70+
--> $DIR/derive.rs:56:1
7171
|
7272
LL | / impl Clone for FnPtr {
7373
LL | |
@@ -78,7 +78,7 @@ LL | | }
7878
| |_^
7979
|
8080
note: consider deriving `Clone` or removing `Copy`
81-
--> $DIR/derive.rs:60:1
81+
--> $DIR/derive.rs:56:1
8282
|
8383
LL | / impl Clone for FnPtr {
8484
LL | |
@@ -89,7 +89,7 @@ LL | | }
8989
| |_^
9090

9191
error: you are implementing `Clone` explicitly on a `Copy` type
92-
--> $DIR/derive.rs:81:1
92+
--> $DIR/derive.rs:77:1
9393
|
9494
LL | / impl<T: Clone> Clone for Generic2<T> {
9595
LL | |
@@ -100,7 +100,7 @@ LL | | }
100100
| |_^
101101
|
102102
note: consider deriving `Clone` or removing `Copy`
103-
--> $DIR/derive.rs:81:1
103+
--> $DIR/derive.rs:77:1
104104
|
105105
LL | / impl<T: Clone> Clone for Generic2<T> {
106106
LL | |

tests/ui/derive_ord_xor_partial_ord.rs

Lines changed: 1 addition & 1 deletion
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::incorrect_partial_ord_impl_on_ord_type)]
3+
#![allow(clippy::non_canonical_partial_ord_impl)]
44

55
use std::cmp::Ordering;
66

tests/ui/incorrect_clone_impl_on_copy_type.stderr renamed to tests/ui/non_canonical_clone_impl.stderr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
error: incorrect implementation of `clone` on a `Copy` type
2-
--> $DIR/incorrect_clone_impl_on_copy_type.rs:9:29
1+
error: non-canonical implementation of `clone` on a `Copy` type
2+
--> $DIR/non_canonical_clone_impl.rs:9:29
33
|
44
LL | fn clone(&self) -> Self {
55
| _____________________________^
66
LL | | Self(self.0)
77
LL | | }
88
| |_____^ help: change this to: `{ *self }`
99
|
10-
= note: `#[deny(clippy::incorrect_clone_impl_on_copy_type)]` on by default
10+
= note: `-D clippy::non-canonical-clone-impl` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::non_canonical_clone_impl)]`
1112

12-
error: incorrect implementation of `clone_from` on a `Copy` type
13-
--> $DIR/incorrect_clone_impl_on_copy_type.rs:13:5
13+
error: unnecessary implementation of `clone_from` on a `Copy` type
14+
--> $DIR/non_canonical_clone_impl.rs:13:5
1415
|
1516
LL | / fn clone_from(&mut self, source: &Self) {
1617
LL | | source.clone();
1718
LL | | *self = source.clone();
1819
LL | | }
1920
| |_____^ help: remove it
2021

21-
error: incorrect implementation of `clone` on a `Copy` type
22-
--> $DIR/incorrect_clone_impl_on_copy_type.rs:80:29
22+
error: non-canonical implementation of `clone` on a `Copy` type
23+
--> $DIR/non_canonical_clone_impl.rs:80:29
2324
|
2425
LL | fn clone(&self) -> Self {
2526
| _____________________________^
2627
LL | | Self(self.0)
2728
LL | | }
2829
| |_____^ help: change this to: `{ *self }`
2930

30-
error: incorrect implementation of `clone_from` on a `Copy` type
31-
--> $DIR/incorrect_clone_impl_on_copy_type.rs:84:5
31+
error: unnecessary implementation of `clone_from` on a `Copy` type
32+
--> $DIR/non_canonical_clone_impl.rs:84:5
3233
|
3334
LL | / fn clone_from(&mut self, source: &Self) {
3435
LL | | source.clone();

tests/ui/incorrect_partial_ord_impl_on_ord_type.stderr renamed to tests/ui/non_canonical_partial_ord_impl.stderr

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
error: incorrect implementation of `partial_cmp` on an `Ord` type
2-
--> $DIR/incorrect_partial_ord_impl_on_ord_type.rs:16:1
1+
error: non-canonical implementation of `partial_cmp` on an `Ord` type
2+
--> $DIR/non_canonical_partial_ord_impl.rs:16:1
33
|
44
LL | / impl PartialOrd for A {
55
LL | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
@@ -10,10 +10,11 @@ LL | || }
1010
LL | | }
1111
| |__^
1212
|
13-
= note: `#[deny(clippy::incorrect_partial_ord_impl_on_ord_type)]` on by default
13+
= note: `-D clippy::non-canonical-partial-ord-impl` implied by `-D warnings`
14+
= help: to override `-D warnings` add `#[allow(clippy::non_canonical_partial_ord_impl)]`
1415

15-
error: incorrect implementation of `partial_cmp` on an `Ord` type
16-
--> $DIR/incorrect_partial_ord_impl_on_ord_type.rs:50:1
16+
error: non-canonical implementation of `partial_cmp` on an `Ord` type
17+
--> $DIR/non_canonical_partial_ord_impl.rs:50:1
1718
|
1819
LL | / impl PartialOrd for C {
1920
LL | | fn partial_cmp(&self, _: &Self) -> Option<Ordering> {

tests/ui/incorrect_partial_ord_impl_on_ord_type_fully_qual.rs renamed to tests/ui/non_canonical_partial_ord_impl_fully_qual.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ impl cmp::Ord for A {
2121
}
2222

2323
impl PartialOrd for A {
24-
//~^ ERROR: incorrect implementation of `partial_cmp` on an `Ord` type
25-
//~| NOTE: `#[deny(clippy::incorrect_partial_ord_impl_on_ord_type)]` on by default
2624
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
2725
// NOTE: This suggestion is wrong, as `Ord` is not in scope. But this should be fine as it isn't
2826
// automatically applied
@@ -46,7 +44,6 @@ impl cmp::Ord for B {
4644
}
4745

4846
impl PartialOrd for B {
49-
//~^ ERROR: incorrect implementation of `partial_cmp` on an `Ord` type
5047
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
5148
// This calls `B.cmp`, not `Ord::cmp`!
5249
Some(self.cmp(other))

0 commit comments

Comments
 (0)