Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 30, 2023
1 parent 89c37e3 commit f2cccc2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
29 changes: 12 additions & 17 deletions clippy_lints/src/incorrect_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use clippy_utils::{
ty::implements_trait,
};
use rustc_errors::Applicability;
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, PatKind, UnOp};
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, UnOp};
use rustc_hir_analysis::hir_ty_to_ty;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::EarlyBinder;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, symbol};
use rustc_span::{sym, symbol::kw};
use std::borrow::Cow;

declare_clippy_lint! {
Expand Down Expand Up @@ -105,8 +105,7 @@ declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE, INCORRE
impl LateLintPass<'_> for IncorrectImpls {
#[expect(clippy::too_many_lines)]
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
let node = get_parent_node(cx.tcx, impl_item.hir_id());
let Some(Node::Item(item)) = node else {
let Some(Node::Item(item)) = get_parent_node(cx.tcx, impl_item.hir_id()) else {
return;
};
let ItemKind::Impl(imp) = item.kind else {
Expand All @@ -115,7 +114,6 @@ impl LateLintPass<'_> for IncorrectImpls {
let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else {
return;
};
let trait_impl_def_id = trait_impl.def_id;
if cx.tcx.is_automatically_derived(item.owner_id.to_def_id()) {
return;
}
Expand All @@ -127,7 +125,7 @@ impl LateLintPass<'_> for IncorrectImpls {
return;
};

if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl_def_id)
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl.def_id)
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)
&& implements_trait(
cx,
Expand All @@ -139,9 +137,9 @@ impl LateLintPass<'_> for IncorrectImpls {
if impl_item.ident.name == sym::clone {
if block.stmts.is_empty()
&& let Some(expr) = block.expr
&& let ExprKind::Unary(UnOp::Deref, inner) = expr.kind
&& let ExprKind::Path(qpath) = inner.kind
&& last_path_segment(&qpath).ident.name == symbol::kw::SelfLower
&& let ExprKind::Unary(UnOp::Deref, deref) = expr.kind
&& let ExprKind::Path(qpath) = deref.kind
&& last_path_segment(&qpath).ident.name == kw::SelfLower
{} else {
span_lint_and_sugg(
cx,
Expand All @@ -163,7 +161,7 @@ impl LateLintPass<'_> for IncorrectImpls {
INCORRECT_CLONE_IMPL_ON_COPY_TYPE,
impl_item.span,
"incorrect implementation of `clone_from` on a `Copy` type",
"remove this",
"remove it",
String::new(),
Applicability::MaybeIncorrect,
);
Expand All @@ -172,7 +170,7 @@ impl LateLintPass<'_> for IncorrectImpls {
}
}

if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl_def_id)
if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl.def_id)
&& impl_item.ident.name == sym::partial_cmp
&& let Some(ord_def_id) = cx
.tcx
Expand Down Expand Up @@ -203,10 +201,7 @@ impl LateLintPass<'_> for IncorrectImpls {
{} else {
// If lhs and rhs are not the same type, bail. This makes creating a valid
// suggestion tons more complex.
if let Some(lhs) = trait_impl.substs.get(0)
&& let Some(rhs) = trait_impl.substs.get(1)
&& lhs != rhs
{
if let [lhs, rhs, ..] = trait_impl.substs.as_slice() && lhs != rhs {
return;
}

Expand All @@ -216,8 +211,8 @@ impl LateLintPass<'_> for IncorrectImpls {
item.span,
"incorrect implementation of `partial_cmp` on an `Ord` type",
|diag| {
let (help, app) = if let Some(other) = body.params.get(1)
&& let PatKind::Binding(_, _, other_ident, ..) = other.pat.kind
let (help, app) = if let [_, other] = body.params
&& let Some(other_ident) = other.pat.simple_ident()
{
(
Cow::Owned(format!("{{ Some(self.cmp({})) }}", other_ident.name)),
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/incorrect_clone_impl_on_copy_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | / fn clone_from(&mut self, source: &Self) {
LL | | source.clone();
LL | | *self = source.clone();
LL | | }
| |_____^ help: remove this
| |_____^ help: remove it

error: incorrect implementation of `clone` on a `Copy` type
--> $DIR/incorrect_clone_impl_on_copy_type.rs:81:29
Expand All @@ -34,7 +34,7 @@ LL | / fn clone_from(&mut self, source: &Self) {
LL | | source.clone();
LL | | *self = source.clone();
LL | | }
| |_____^ help: remove this
| |_____^ help: remove it

error: aborting due to 4 previous errors

2 changes: 1 addition & 1 deletion tests/ui/incorrect_partial_ord_impl_on_ord_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PartialOrd for B {
}
}

// lint, but we can't give a suggestion since &Self is not named
// lint, but we can't give a correct suggestion since &Self is not named

#[derive(Eq, PartialEq)]
struct C(u32);
Expand Down

0 comments on commit f2cccc2

Please sign in to comment.