Skip to content

Commit d227bf4

Browse files
authored
Allow expect on impl for derive_ord_xor_partial_ord (#16303)
Closes #16298 This lint flags on the type defination, while also giving a warning on the span of the impl block. It is counter-intuitive that you need to put the `expect` on top of the type defination instead of the impl block to suppress the warning, and this PR addresses that. changelog: [`derive_ord_xor_partial_ord`] allow `expect` on `impl` block
2 parents e3c975f + 064b95e commit d227bf4

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

clippy_lints/src/derive/derive_ord_xor_partial_ord.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2+
use clippy_utils::fulfill_or_allowed;
23
use rustc_hir::{self as hir, HirId};
34
use rustc_lint::LateContext;
45
use rustc_middle::ty::Ty;
5-
use rustc_span::{Span, sym};
6+
use rustc_span::sym;
67

78
use super::DERIVE_ORD_XOR_PARTIAL_ORD;
89

910
/// Implementation of the `DERIVE_ORD_XOR_PARTIAL_ORD` lint.
1011
pub(super) fn check<'tcx>(
1112
cx: &LateContext<'tcx>,
12-
span: Span,
13+
item: &hir::Item<'_>,
1314
trait_ref: &hir::TraitRef<'_>,
1415
ty: Ty<'tcx>,
1516
adt_hir_id: HirId,
@@ -19,6 +20,8 @@ pub(super) fn check<'tcx>(
1920
&& let Some(partial_ord_trait_def_id) = cx.tcx.lang_items().partial_ord_trait()
2021
&& let Some(def_id) = &trait_ref.trait_def_id()
2122
&& *def_id == ord_trait_def_id
23+
&& let item_hir_id = cx.tcx.local_def_id_to_hir_id(item.owner_id)
24+
&& !fulfill_or_allowed(cx, DERIVE_ORD_XOR_PARTIAL_ORD, [adt_hir_id])
2225
{
2326
// Look for the PartialOrd implementations for `ty`
2427
cx.tcx.for_each_relevant_impl(partial_ord_trait_def_id, ty, |impl_id| {
@@ -39,7 +42,7 @@ pub(super) fn check<'tcx>(
3942
"you are deriving `Ord` but have implemented `PartialOrd` explicitly"
4043
};
4144

42-
span_lint_hir_and_then(cx, DERIVE_ORD_XOR_PARTIAL_ORD, adt_hir_id, span, mess, |diag| {
45+
span_lint_hir_and_then(cx, DERIVE_ORD_XOR_PARTIAL_ORD, item_hir_id, item.span, mess, |diag| {
4346
if let Some(local_def_id) = impl_id.as_local() {
4447
let hir_id = cx.tcx.local_def_id_to_hir_id(local_def_id);
4548
diag.span_note(cx.tcx.hir_span(hir_id), "`PartialOrd` implemented here");

clippy_lints/src/derive/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'tcx> LateLintPass<'tcx> for Derive {
208208
let is_automatically_derived = cx.tcx.is_automatically_derived(item.owner_id.to_def_id());
209209

210210
derived_hash_with_manual_eq::check(cx, item.span, trait_ref, ty, adt_hir_id, is_automatically_derived);
211-
derive_ord_xor_partial_ord::check(cx, item.span, trait_ref, ty, adt_hir_id, is_automatically_derived);
211+
derive_ord_xor_partial_ord::check(cx, item, trait_ref, ty, adt_hir_id, is_automatically_derived);
212212

213213
if is_automatically_derived {
214214
unsafe_derive_deserialize::check(cx, item, trait_ref, ty, adt_hir_id);

tests/ui/derive_ord_xor_partial_ord.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,17 @@ mod issue15708 {
9191
}
9292
}
9393
}
94+
95+
mod issue16298 {
96+
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
97+
struct Normalized<S>(S);
98+
99+
impl<S: Eq> Eq for Normalized<S> {}
100+
101+
#[expect(clippy::derive_ord_xor_partial_ord)]
102+
impl<S: Eq + PartialOrd> Ord for Normalized<S> {
103+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
104+
self.partial_cmp(other).unwrap()
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)