Skip to content

fulfill expectations in check_partial_eq_without_eq #12841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy};
use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -456,20 +456,27 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
&& !has_non_exhaustive_attr(cx.tcx, *adt)
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id)
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
&& let Some(local_def_id) = adt.did().as_local()
// If all of our fields implement `Eq`, we can implement `Eq` too
&& adt
.all_fields()
.map(|f| f.ty(cx.tcx, args))
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]))
{
span_lint_and_sugg(
span_lint_hir_and_then(
cx,
DERIVE_PARTIAL_EQ_WITHOUT_EQ,
cx.tcx.local_def_id_to_hir_id(local_def_id),
span.ctxt().outer_expn_data().call_site,
"you are deriving `PartialEq` and can implement `Eq`",
"consider deriving `Eq` as well",
"PartialEq, Eq".to_string(),
Applicability::MachineApplicable,
|diag| {
diag.span_suggestion(
span.ctxt().outer_expn_data().call_site,
"consider deriving `Eq` as well",
"PartialEq, Eq",
Applicability::MachineApplicable,
);
},
);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/derive_partial_eq_without_eq.fixed
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(lint_reasons)]
#![allow(unused)]
#![warn(clippy::derive_partial_eq_without_eq)]

Expand All @@ -14,6 +15,22 @@ pub struct MissingEq {
bar: String,
}

// Check that we honor the `allow` attribute
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, PartialEq)]
pub struct AllowedMissingEq {
foo: u32,
bar: String,
}

// Check that we honor the `expect` attribute
#[expect(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, PartialEq)]
pub struct ExpectedMissingEq {
foo: u32,
bar: String,
}

// Eq is derived
#[derive(PartialEq, Eq)]
pub struct NotMissingEq {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/derive_partial_eq_without_eq.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(lint_reasons)]
#![allow(unused)]
#![warn(clippy::derive_partial_eq_without_eq)]

Expand All @@ -14,6 +15,22 @@ pub struct MissingEq {
bar: String,
}

// Check that we honor the `allow` attribute
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, PartialEq)]
pub struct AllowedMissingEq {
foo: u32,
bar: String,
}

// Check that we honor the `expect` attribute
#[expect(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, PartialEq)]
pub struct ExpectedMissingEq {
foo: u32,
bar: String,
}

// Eq is derived
#[derive(PartialEq, Eq)]
pub struct NotMissingEq {
Expand Down
26 changes: 13 additions & 13 deletions tests/ui/derive_partial_eq_without_eq.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: you are deriving `PartialEq` and can implement `Eq`
--> tests/ui/derive_partial_eq_without_eq.rs:11:17
--> tests/ui/derive_partial_eq_without_eq.rs:12:17
|
LL | #[derive(Debug, PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
Expand All @@ -8,73 +8,73 @@ LL | #[derive(Debug, PartialEq)]
= help: to override `-D warnings` add `#[allow(clippy::derive_partial_eq_without_eq)]`

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

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

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

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

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

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

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

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

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

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

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

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