Skip to content

Commit 30c73fe

Browse files
replace_box: clean-up a bit (#15834)
changelog: none
2 parents f110f34 + 01d2adc commit 30c73fe

File tree

1 file changed

+5
-25
lines changed

1 file changed

+5
-25
lines changed

clippy_lints/src/replace_box.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ use clippy_utils::sugg::Sugg;
33
use clippy_utils::ty::implements_trait;
44
use clippy_utils::{is_default_equivalent_call, local_is_initialized, path_def_id, path_to_local};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, ExprKind, LangItem, QPath};
6+
use rustc_hir::{Expr, ExprKind, QPath};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::{self, Ty};
98
use rustc_session::declare_lint_pass;
109
use rustc_span::sym;
1110

@@ -40,20 +39,11 @@ impl LateLintPass<'_> for ReplaceBox {
4039
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind
4140
&& !lhs.span.from_expansion()
4241
&& !rhs.span.from_expansion()
43-
{
44-
let lhs_ty = cx.typeck_results().expr_ty(lhs);
45-
42+
&& let lhs_ty = cx.typeck_results().expr_ty(lhs)
4643
// No diagnostic for late-initialized locals
47-
if let Some(local) = path_to_local(lhs)
48-
&& !local_is_initialized(cx, local)
49-
{
50-
return;
51-
}
52-
53-
let Some(inner_ty) = get_box_inner_type(cx, lhs_ty) else {
54-
return;
55-
};
56-
44+
&& path_to_local(lhs).is_none_or(|local| local_is_initialized(cx, local))
45+
&& let Some(inner_ty) = lhs_ty.boxed_ty()
46+
{
5747
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
5848
&& implements_trait(cx, inner_ty, default_trait_id, &[])
5949
&& is_default_call(cx, rhs)
@@ -103,16 +93,6 @@ impl LateLintPass<'_> for ReplaceBox {
10393
}
10494
}
10595

106-
fn get_box_inner_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
107-
if let ty::Adt(def, args) = ty.kind()
108-
&& cx.tcx.is_lang_item(def.did(), LangItem::OwnedBox)
109-
{
110-
Some(args.type_at(0))
111-
} else {
112-
None
113-
}
114-
}
115-
11696
fn is_default_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
11797
matches!(expr.kind, ExprKind::Call(func, _args) if is_default_equivalent_call(cx, func, Some(expr)))
11898
}

0 commit comments

Comments
 (0)