Skip to content

Commit 7f30b20

Browse files
committed
fulfill expectations in check_partial_eq_without_eq
changelog: fulfill expectations in [derive_partial_eq_without_eq]
1 parent 76856ff commit 7f30b20

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

clippy_lints/src/derive.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then, span_lint_hir_and_then};
22
use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy};
33
use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths};
44
use rustc_errors::Applicability;
@@ -456,20 +456,27 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
456456
&& !has_non_exhaustive_attr(cx.tcx, *adt)
457457
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id)
458458
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
459+
&& let Some(local_def_id) = adt.did().as_local()
459460
// If all of our fields implement `Eq`, we can implement `Eq` too
460461
&& adt
461462
.all_fields()
462463
.map(|f| f.ty(cx.tcx, args))
463464
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]))
464465
{
465-
span_lint_and_sugg(
466+
span_lint_hir_and_then(
466467
cx,
467468
DERIVE_PARTIAL_EQ_WITHOUT_EQ,
469+
cx.tcx.local_def_id_to_hir_id(local_def_id),
468470
span.ctxt().outer_expn_data().call_site,
469471
"you are deriving `PartialEq` and can implement `Eq`",
470-
"consider deriving `Eq` as well",
471-
"PartialEq, Eq".to_string(),
472-
Applicability::MachineApplicable,
472+
|diag| {
473+
diag.span_suggestion(
474+
span.ctxt().outer_expn_data().call_site,
475+
"consider deriving `Eq` as well",
476+
"PartialEq, Eq",
477+
Applicability::MachineApplicable,
478+
);
479+
},
473480
);
474481
}
475482
}

tests/ui/derive_partial_eq_without_eq.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(lint_reasons)]
12
#![allow(unused)]
23
#![warn(clippy::derive_partial_eq_without_eq)]
34

@@ -14,6 +15,22 @@ pub struct MissingEq {
1415
bar: String,
1516
}
1617

18+
// Check that we honor the `allow` attribute
19+
#[allow(clippy::derive_partial_eq_without_eq)]
20+
#[derive(Debug, PartialEq)]
21+
pub struct AllowedMissingEq {
22+
foo: u32,
23+
bar: String,
24+
}
25+
26+
// Check that we honor the `expect` attribute
27+
#[expect(clippy::derive_partial_eq_without_eq)]
28+
#[derive(Debug, PartialEq)]
29+
pub struct ExpectedMissingEq {
30+
foo: u32,
31+
bar: String,
32+
}
33+
1734
// Eq is derived
1835
#[derive(PartialEq, Eq)]
1936
pub struct NotMissingEq {

tests/ui/derive_partial_eq_without_eq.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(lint_reasons)]
12
#![allow(unused)]
23
#![warn(clippy::derive_partial_eq_without_eq)]
34

@@ -14,6 +15,22 @@ pub struct MissingEq {
1415
bar: String,
1516
}
1617

18+
// Check that we honor the `allow` attribute
19+
#[allow(clippy::derive_partial_eq_without_eq)]
20+
#[derive(Debug, PartialEq)]
21+
pub struct AllowedMissingEq {
22+
foo: u32,
23+
bar: String,
24+
}
25+
26+
// Check that we honor the `expect` attribute
27+
#[expect(clippy::derive_partial_eq_without_eq)]
28+
#[derive(Debug, PartialEq)]
29+
pub struct ExpectedMissingEq {
30+
foo: u32,
31+
bar: String,
32+
}
33+
1734
// Eq is derived
1835
#[derive(PartialEq, Eq)]
1936
pub struct NotMissingEq {

tests/ui/derive_partial_eq_without_eq.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you are deriving `PartialEq` and can implement `Eq`
2-
--> tests/ui/derive_partial_eq_without_eq.rs:11:17
2+
--> tests/ui/derive_partial_eq_without_eq.rs:12:17
33
|
44
LL | #[derive(Debug, PartialEq)]
55
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
@@ -8,73 +8,73 @@ LL | #[derive(Debug, PartialEq)]
88
= help: to override `-D warnings` add `#[allow(clippy::derive_partial_eq_without_eq)]`
99

1010
error: you are deriving `PartialEq` and can implement `Eq`
11-
--> tests/ui/derive_partial_eq_without_eq.rs:53:10
11+
--> tests/ui/derive_partial_eq_without_eq.rs:70:10
1212
|
1313
LL | #[derive(PartialEq)]
1414
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
1515

1616
error: you are deriving `PartialEq` and can implement `Eq`
17-
--> tests/ui/derive_partial_eq_without_eq.rs:59:10
17+
--> tests/ui/derive_partial_eq_without_eq.rs:76:10
1818
|
1919
LL | #[derive(PartialEq)]
2020
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
2121

2222
error: you are deriving `PartialEq` and can implement `Eq`
23-
--> tests/ui/derive_partial_eq_without_eq.rs:65:10
23+
--> tests/ui/derive_partial_eq_without_eq.rs:82:10
2424
|
2525
LL | #[derive(PartialEq)]
2626
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
2727

2828
error: you are deriving `PartialEq` and can implement `Eq`
29-
--> tests/ui/derive_partial_eq_without_eq.rs:68:10
29+
--> tests/ui/derive_partial_eq_without_eq.rs:85:10
3030
|
3131
LL | #[derive(PartialEq)]
3232
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
3333

3434
error: you are deriving `PartialEq` and can implement `Eq`
35-
--> tests/ui/derive_partial_eq_without_eq.rs:74:10
35+
--> tests/ui/derive_partial_eq_without_eq.rs:91:10
3636
|
3737
LL | #[derive(PartialEq)]
3838
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
3939

4040
error: you are deriving `PartialEq` and can implement `Eq`
41-
--> tests/ui/derive_partial_eq_without_eq.rs:80:10
41+
--> tests/ui/derive_partial_eq_without_eq.rs:97:10
4242
|
4343
LL | #[derive(PartialEq)]
4444
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
4545

4646
error: you are deriving `PartialEq` and can implement `Eq`
47-
--> tests/ui/derive_partial_eq_without_eq.rs:93:17
47+
--> tests/ui/derive_partial_eq_without_eq.rs:110:17
4848
|
4949
LL | #[derive(Debug, PartialEq, Clone)]
5050
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
5151

5252
error: you are deriving `PartialEq` and can implement `Eq`
53-
--> tests/ui/derive_partial_eq_without_eq.rs:96:10
53+
--> tests/ui/derive_partial_eq_without_eq.rs:113:10
5454
|
5555
LL | #[derive(PartialEq)]
5656
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
5757

5858
error: you are deriving `PartialEq` and can implement `Eq`
59-
--> tests/ui/derive_partial_eq_without_eq.rs:103:14
59+
--> tests/ui/derive_partial_eq_without_eq.rs:120:14
6060
|
6161
LL | #[derive(PartialEq)]
6262
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
6363

6464
error: you are deriving `PartialEq` and can implement `Eq`
65-
--> tests/ui/derive_partial_eq_without_eq.rs:106:14
65+
--> tests/ui/derive_partial_eq_without_eq.rs:123:14
6666
|
6767
LL | #[derive(PartialEq)]
6868
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
6969

7070
error: you are deriving `PartialEq` and can implement `Eq`
71-
--> tests/ui/derive_partial_eq_without_eq.rs:166:14
71+
--> tests/ui/derive_partial_eq_without_eq.rs:183:14
7272
|
7373
LL | #[derive(PartialEq)]
7474
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
7575

7676
error: you are deriving `PartialEq` and can implement `Eq`
77-
--> tests/ui/derive_partial_eq_without_eq.rs:174:14
77+
--> tests/ui/derive_partial_eq_without_eq.rs:191:14
7878
|
7979
LL | #[derive(PartialEq)]
8080
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`

0 commit comments

Comments
 (0)