fulfill expectations in check_partial_eq_without_eq

changelog: fulfill expectations in [derive_partial_eq_without_eq]
This commit is contained in:
Jakob Schwarz 2024-05-22 09:01:37 +02:00
parent 76856ffb57
commit 7f30b20b28
No known key found for this signature in database
GPG Key ID: 5E5CCB0709EF8B92
4 changed files with 59 additions and 18 deletions

View File

@ -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::ty::{implements_trait, implements_trait_with_env, is_copy};
use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths}; use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths};
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -456,21 +456,28 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
&& !has_non_exhaustive_attr(cx.tcx, *adt) && !has_non_exhaustive_attr(cx.tcx, *adt)
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id) && !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 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 // If all of our fields implement `Eq`, we can implement `Eq` too
&& adt && adt
.all_fields() .all_fields()
.map(|f| f.ty(cx.tcx, args)) .map(|f| f.ty(cx.tcx, args))
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[])) .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, cx,
DERIVE_PARTIAL_EQ_WITHOUT_EQ, DERIVE_PARTIAL_EQ_WITHOUT_EQ,
cx.tcx.local_def_id_to_hir_id(local_def_id),
span.ctxt().outer_expn_data().call_site, span.ctxt().outer_expn_data().call_site,
"you are deriving `PartialEq` and can implement `Eq`", "you are deriving `PartialEq` and can implement `Eq`",
|diag| {
diag.span_suggestion(
span.ctxt().outer_expn_data().call_site,
"consider deriving `Eq` as well", "consider deriving `Eq` as well",
"PartialEq, Eq".to_string(), "PartialEq, Eq",
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
},
);
} }
} }

View File

@ -1,3 +1,4 @@
#![feature(lint_reasons)]
#![allow(unused)] #![allow(unused)]
#![warn(clippy::derive_partial_eq_without_eq)] #![warn(clippy::derive_partial_eq_without_eq)]
@ -14,6 +15,22 @@ pub struct MissingEq {
bar: String, 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 // Eq is derived
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct NotMissingEq { pub struct NotMissingEq {

View File

@ -1,3 +1,4 @@
#![feature(lint_reasons)]
#![allow(unused)] #![allow(unused)]
#![warn(clippy::derive_partial_eq_without_eq)] #![warn(clippy::derive_partial_eq_without_eq)]
@ -14,6 +15,22 @@ pub struct MissingEq {
bar: String, 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 // Eq is derived
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct NotMissingEq { pub struct NotMissingEq {

View File

@ -1,5 +1,5 @@
error: you are deriving `PartialEq` and can implement `Eq` 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)] LL | #[derive(Debug, PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
@ -8,73 +8,73 @@ LL | #[derive(Debug, PartialEq)]
= help: to override `-D warnings` add `#[allow(clippy::derive_partial_eq_without_eq)]` = help: to override `-D warnings` add `#[allow(clippy::derive_partial_eq_without_eq)]`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(Debug, PartialEq, Clone)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`
error: you are deriving `PartialEq` and can implement `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)] LL | #[derive(PartialEq)]
| ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq` | ^^^^^^^^^ help: consider deriving `Eq` as well: `PartialEq, Eq`