Skip to content

Commit 7137dc5

Browse files
Simplify is_box_of_default and is_default_call
Co-authored-by: Samuel Tardieu <[email protected]>
1 parent d27feba commit 7137dc5

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

clippy_lints/src/default_box_assignments.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::ty::implements_trait;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::{self, GenericArgKind, Ty};
8+
use rustc_middle::ty::{self, Ty};
99
use rustc_session::declare_lint_pass;
1010
use rustc_span::sym;
1111

@@ -38,7 +38,7 @@ impl LateLintPass<'_> for DefaultBoxAssignments {
3838
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
3939
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind {
4040
let lhs_ty = cx.typeck_results().expr_ty(lhs);
41-
if is_box_of_default(lhs_ty, cx) && is_default_call(rhs, cx) && !rhs.span.from_expansion() {
41+
if is_box_of_default(cx, lhs_ty) && is_default_call(cx, rhs) && !rhs.span.from_expansion() {
4242
span_lint_and_then(
4343
cx,
4444
DEFAULT_BOX_ASSIGNMENTS,
@@ -64,28 +64,17 @@ impl LateLintPass<'_> for DefaultBoxAssignments {
6464
}
6565
}
6666

67-
fn is_box_of_default<'a>(ty: Ty<'a>, cx: &LateContext<'a>) -> bool {
67+
fn is_box_of_default<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
6868
if let ty::Adt(def, args) = ty.kind()
69-
&& cx.tcx.lang_items().get(LangItem::OwnedBox) == Some(def.did())
70-
&& let Some(inner) = args.iter().find_map(|arg| match arg.kind() {
71-
GenericArgKind::Type(ty) => Some(ty),
72-
_ => None,
73-
})
69+
&& cx.tcx.is_lang_item(def.did(), LangItem::OwnedBox)
70+
&& let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
7471
{
75-
cx.tcx
76-
.get_diagnostic_item(sym::Default)
77-
.is_some_and(|id| implements_trait(cx, inner, id, &[]))
72+
implements_trait(cx, args.type_at(0), default_trait_id, &[])
7873
} else {
7974
false
8075
}
8176
}
8277

83-
fn is_default_call(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
84-
if let ExprKind::Call(func, _args) = expr.kind
85-
&& is_default_equivalent_call(cx, func, Some(expr))
86-
{
87-
true
88-
} else {
89-
false
90-
}
78+
fn is_default_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
79+
matches!(expr.kind, ExprKind::Call(func, _args) if is_default_equivalent_call(cx, func, Some(expr)))
9180
}

0 commit comments

Comments
 (0)