mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Auto merge of #12702 - Luv-Ray:non_canonical_partial_ord_impl, r=Manishearth
[`non_canonical_partial_ord_impl`]: Fix emitting warnings which conflict with `needless_return` fixes #12683 --- changelog: fix [`non_canonical_partial_ord_impl`] emitting warnings which conflict with `needless_return`
This commit is contained in:
commit
9162bbf5ac
3 changed files with 71 additions and 11 deletions
|
@ -182,17 +182,17 @@ impl LateLintPass<'_> for NonCanonicalImpls {
|
|||
|
||||
if block.stmts.is_empty()
|
||||
&& let Some(expr) = block.expr
|
||||
&& let ExprKind::Call(
|
||||
Expr {
|
||||
kind: ExprKind::Path(some_path),
|
||||
hir_id: some_hir_id,
|
||||
..
|
||||
},
|
||||
[cmp_expr],
|
||||
) = expr.kind
|
||||
&& is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
|
||||
// Fix #11178, allow `Self::cmp(self, ..)` too
|
||||
&& self_cmp_call(cx, cmp_expr, impl_item.owner_id.def_id, &mut needs_fully_qualified)
|
||||
&& expr_is_cmp(cx, &expr.kind, impl_item, &mut needs_fully_qualified)
|
||||
{
|
||||
}
|
||||
// Fix #12683, allow [`needless_return`] here
|
||||
else if block.expr.is_none()
|
||||
&& let Some(stmt) = block.stmts.first()
|
||||
&& let rustc_hir::StmtKind::Semi(Expr {
|
||||
kind: ExprKind::Ret(Some(Expr { kind: ret_kind, .. })),
|
||||
..
|
||||
}) = stmt.kind
|
||||
&& expr_is_cmp(cx, ret_kind, impl_item, &mut needs_fully_qualified)
|
||||
{
|
||||
} else {
|
||||
// If `Self` and `Rhs` are not the same type, bail. This makes creating a valid
|
||||
|
@ -245,6 +245,30 @@ impl LateLintPass<'_> for NonCanonicalImpls {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return true if `expr_kind` is a `cmp` call.
|
||||
fn expr_is_cmp<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
expr_kind: &'tcx ExprKind<'tcx>,
|
||||
impl_item: &ImplItem<'_>,
|
||||
needs_fully_qualified: &mut bool,
|
||||
) -> bool {
|
||||
if let ExprKind::Call(
|
||||
Expr {
|
||||
kind: ExprKind::Path(some_path),
|
||||
hir_id: some_hir_id,
|
||||
..
|
||||
},
|
||||
[cmp_expr],
|
||||
) = expr_kind
|
||||
{
|
||||
is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
|
||||
// Fix #11178, allow `Self::cmp(self, ..)` too
|
||||
&& self_cmp_call(cx, cmp_expr, impl_item.owner_id.def_id, needs_fully_qualified)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether this is any of `self.cmp(..)`, `Self::cmp(self, ..)` or `Ord::cmp(self, ..)`.
|
||||
fn self_cmp_call<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
|
|
|
@ -142,3 +142,21 @@ impl PartialOrd for H {
|
|||
Some(Ord::cmp(self, other))
|
||||
}
|
||||
}
|
||||
|
||||
// #12683, do not lint
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct I(u32);
|
||||
|
||||
impl Ord for I {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for I {
|
||||
#[allow(clippy::needless_return)]
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
return Some(self.cmp(other));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -146,3 +146,21 @@ impl PartialOrd for H {
|
|||
Some(Ord::cmp(self, other))
|
||||
}
|
||||
}
|
||||
|
||||
// #12683, do not lint
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct I(u32);
|
||||
|
||||
impl Ord for I {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for I {
|
||||
#[allow(clippy::needless_return)]
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
return Some(self.cmp(other));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue