Skip to content

Commit 9162bbf

Browse files
committed
Auto merge of rust-lang#12702 - Luv-Ray:non_canonical_partial_ord_impl, r=Manishearth
[`non_canonical_partial_ord_impl`]: Fix emitting warnings which conflict with `needless_return` fixes rust-lang#12683 --- changelog: fix [`non_canonical_partial_ord_impl`] emitting warnings which conflict with `needless_return`
2 parents b2a7371 + 107e44b commit 9162bbf

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

clippy_lints/src/non_canonical_impls.rs

+35-11
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,17 @@ impl LateLintPass<'_> for NonCanonicalImpls {
182182

183183
if block.stmts.is_empty()
184184
&& let Some(expr) = block.expr
185-
&& let ExprKind::Call(
186-
Expr {
187-
kind: ExprKind::Path(some_path),
188-
hir_id: some_hir_id,
189-
..
190-
},
191-
[cmp_expr],
192-
) = expr.kind
193-
&& is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
194-
// Fix #11178, allow `Self::cmp(self, ..)` too
195-
&& self_cmp_call(cx, cmp_expr, impl_item.owner_id.def_id, &mut needs_fully_qualified)
185+
&& expr_is_cmp(cx, &expr.kind, impl_item, &mut needs_fully_qualified)
186+
{
187+
}
188+
// Fix #12683, allow [`needless_return`] here
189+
else if block.expr.is_none()
190+
&& let Some(stmt) = block.stmts.first()
191+
&& let rustc_hir::StmtKind::Semi(Expr {
192+
kind: ExprKind::Ret(Some(Expr { kind: ret_kind, .. })),
193+
..
194+
}) = stmt.kind
195+
&& expr_is_cmp(cx, ret_kind, impl_item, &mut needs_fully_qualified)
196196
{
197197
} else {
198198
// If `Self` and `Rhs` are not the same type, bail. This makes creating a valid
@@ -245,6 +245,30 @@ impl LateLintPass<'_> for NonCanonicalImpls {
245245
}
246246
}
247247

248+
/// Return true if `expr_kind` is a `cmp` call.
249+
fn expr_is_cmp<'tcx>(
250+
cx: &LateContext<'tcx>,
251+
expr_kind: &'tcx ExprKind<'tcx>,
252+
impl_item: &ImplItem<'_>,
253+
needs_fully_qualified: &mut bool,
254+
) -> bool {
255+
if let ExprKind::Call(
256+
Expr {
257+
kind: ExprKind::Path(some_path),
258+
hir_id: some_hir_id,
259+
..
260+
},
261+
[cmp_expr],
262+
) = expr_kind
263+
{
264+
is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
265+
// Fix #11178, allow `Self::cmp(self, ..)` too
266+
&& self_cmp_call(cx, cmp_expr, impl_item.owner_id.def_id, needs_fully_qualified)
267+
} else {
268+
false
269+
}
270+
}
271+
248272
/// Returns whether this is any of `self.cmp(..)`, `Self::cmp(self, ..)` or `Ord::cmp(self, ..)`.
249273
fn self_cmp_call<'tcx>(
250274
cx: &LateContext<'tcx>,

tests/ui/non_canonical_partial_ord_impl.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,21 @@ impl PartialOrd for H {
142142
Some(Ord::cmp(self, other))
143143
}
144144
}
145+
146+
// #12683, do not lint
147+
148+
#[derive(Eq, PartialEq)]
149+
struct I(u32);
150+
151+
impl Ord for I {
152+
fn cmp(&self, other: &Self) -> Ordering {
153+
todo!();
154+
}
155+
}
156+
157+
impl PartialOrd for I {
158+
#[allow(clippy::needless_return)]
159+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
160+
return Some(self.cmp(other));
161+
}
162+
}

tests/ui/non_canonical_partial_ord_impl.rs

+18
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,21 @@ impl PartialOrd for H {
146146
Some(Ord::cmp(self, other))
147147
}
148148
}
149+
150+
// #12683, do not lint
151+
152+
#[derive(Eq, PartialEq)]
153+
struct I(u32);
154+
155+
impl Ord for I {
156+
fn cmp(&self, other: &Self) -> Ordering {
157+
todo!();
158+
}
159+
}
160+
161+
impl PartialOrd for I {
162+
#[allow(clippy::needless_return)]
163+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
164+
return Some(self.cmp(other));
165+
}
166+
}

0 commit comments

Comments
 (0)