Skip to content

Commit f2cccc2

Browse files
committed
refactor
1 parent 89c37e3 commit f2cccc2

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

clippy_lints/src/incorrect_impls.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use clippy_utils::{
44
ty::implements_trait,
55
};
66
use rustc_errors::Applicability;
7-
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, PatKind, UnOp};
7+
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, UnOp};
88
use rustc_hir_analysis::hir_ty_to_ty;
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::EarlyBinder;
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
12-
use rustc_span::{sym, symbol};
12+
use rustc_span::{sym, symbol::kw};
1313
use std::borrow::Cow;
1414

1515
declare_clippy_lint! {
@@ -105,8 +105,7 @@ declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE, INCORRE
105105
impl LateLintPass<'_> for IncorrectImpls {
106106
#[expect(clippy::too_many_lines)]
107107
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
108-
let node = get_parent_node(cx.tcx, impl_item.hir_id());
109-
let Some(Node::Item(item)) = node else {
108+
let Some(Node::Item(item)) = get_parent_node(cx.tcx, impl_item.hir_id()) else {
110109
return;
111110
};
112111
let ItemKind::Impl(imp) = item.kind else {
@@ -115,7 +114,6 @@ impl LateLintPass<'_> for IncorrectImpls {
115114
let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else {
116115
return;
117116
};
118-
let trait_impl_def_id = trait_impl.def_id;
119117
if cx.tcx.is_automatically_derived(item.owner_id.to_def_id()) {
120118
return;
121119
}
@@ -127,7 +125,7 @@ impl LateLintPass<'_> for IncorrectImpls {
127125
return;
128126
};
129127

130-
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl_def_id)
128+
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl.def_id)
131129
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)
132130
&& implements_trait(
133131
cx,
@@ -139,9 +137,9 @@ impl LateLintPass<'_> for IncorrectImpls {
139137
if impl_item.ident.name == sym::clone {
140138
if block.stmts.is_empty()
141139
&& let Some(expr) = block.expr
142-
&& let ExprKind::Unary(UnOp::Deref, inner) = expr.kind
143-
&& let ExprKind::Path(qpath) = inner.kind
144-
&& last_path_segment(&qpath).ident.name == symbol::kw::SelfLower
140+
&& let ExprKind::Unary(UnOp::Deref, deref) = expr.kind
141+
&& let ExprKind::Path(qpath) = deref.kind
142+
&& last_path_segment(&qpath).ident.name == kw::SelfLower
145143
{} else {
146144
span_lint_and_sugg(
147145
cx,
@@ -163,7 +161,7 @@ impl LateLintPass<'_> for IncorrectImpls {
163161
INCORRECT_CLONE_IMPL_ON_COPY_TYPE,
164162
impl_item.span,
165163
"incorrect implementation of `clone_from` on a `Copy` type",
166-
"remove this",
164+
"remove it",
167165
String::new(),
168166
Applicability::MaybeIncorrect,
169167
);
@@ -172,7 +170,7 @@ impl LateLintPass<'_> for IncorrectImpls {
172170
}
173171
}
174172

175-
if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl_def_id)
173+
if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl.def_id)
176174
&& impl_item.ident.name == sym::partial_cmp
177175
&& let Some(ord_def_id) = cx
178176
.tcx
@@ -203,10 +201,7 @@ impl LateLintPass<'_> for IncorrectImpls {
203201
{} else {
204202
// If lhs and rhs are not the same type, bail. This makes creating a valid
205203
// suggestion tons more complex.
206-
if let Some(lhs) = trait_impl.substs.get(0)
207-
&& let Some(rhs) = trait_impl.substs.get(1)
208-
&& lhs != rhs
209-
{
204+
if let [lhs, rhs, ..] = trait_impl.substs.as_slice() && lhs != rhs {
210205
return;
211206
}
212207

@@ -216,8 +211,8 @@ impl LateLintPass<'_> for IncorrectImpls {
216211
item.span,
217212
"incorrect implementation of `partial_cmp` on an `Ord` type",
218213
|diag| {
219-
let (help, app) = if let Some(other) = body.params.get(1)
220-
&& let PatKind::Binding(_, _, other_ident, ..) = other.pat.kind
214+
let (help, app) = if let [_, other] = body.params
215+
&& let Some(other_ident) = other.pat.simple_ident()
221216
{
222217
(
223218
Cow::Owned(format!("{{ Some(self.cmp({})) }}", other_ident.name)),

tests/ui/incorrect_clone_impl_on_copy_type.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | / fn clone_from(&mut self, source: &Self) {
1616
LL | | source.clone();
1717
LL | | *self = source.clone();
1818
LL | | }
19-
| |_____^ help: remove this
19+
| |_____^ help: remove it
2020

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

3939
error: aborting due to 4 previous errors
4040

tests/ui/incorrect_partial_ord_impl_on_ord_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl PartialOrd for B {
3737
}
3838
}
3939

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

4242
#[derive(Eq, PartialEq)]
4343
struct C(u32);

0 commit comments

Comments
 (0)