mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-08 09:38:26 +03:00
@@ -3,9 +3,8 @@
|
||||
use clippy_utils::ty::implements_trait;
|
||||
use clippy_utils::{is_default_equivalent_call, local_is_initialized, path_def_id, path_to_local};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, ExprKind, LangItem, QPath};
|
||||
use rustc_hir::{Expr, ExprKind, QPath};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{self, Ty};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::sym;
|
||||
|
||||
@@ -40,20 +39,11 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
|
||||
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind
|
||||
&& !lhs.span.from_expansion()
|
||||
&& !rhs.span.from_expansion()
|
||||
{
|
||||
let lhs_ty = cx.typeck_results().expr_ty(lhs);
|
||||
|
||||
&& let lhs_ty = cx.typeck_results().expr_ty(lhs)
|
||||
// No diagnostic for late-initialized locals
|
||||
if let Some(local) = path_to_local(lhs)
|
||||
&& !local_is_initialized(cx, local)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(inner_ty) = get_box_inner_type(cx, lhs_ty) else {
|
||||
return;
|
||||
};
|
||||
|
||||
&& path_to_local(lhs).is_none_or(|local| local_is_initialized(cx, local))
|
||||
&& let Some(inner_ty) = lhs_ty.boxed_ty()
|
||||
{
|
||||
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
|
||||
&& implements_trait(cx, inner_ty, default_trait_id, &[])
|
||||
&& is_default_call(cx, rhs)
|
||||
@@ -103,16 +93,6 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_box_inner_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
||||
if let ty::Adt(def, args) = ty.kind()
|
||||
&& cx.tcx.is_lang_item(def.did(), LangItem::OwnedBox)
|
||||
{
|
||||
Some(args.type_at(0))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_default_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||
matches!(expr.kind, ExprKind::Call(func, _args) if is_default_equivalent_call(cx, func, Some(expr)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user