Skip to content

Commit 671f4c9

Browse files
committed
Supress default_box_assignments lint for late-init locals
1 parent 7137dc5 commit 671f4c9

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

clippy_lints/src/default_box_assignments.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::is_default_equivalent_call;
32
use clippy_utils::sugg::Sugg;
43
use clippy_utils::ty::implements_trait;
4+
use clippy_utils::{is_default_equivalent_call, local_is_initialized, path_to_local};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -38,6 +38,14 @@ 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+
42+
// No diagnostic for late-initialized locals
43+
if let Some(local) = path_to_local(lhs)
44+
&& !local_is_initialized(cx, local)
45+
{
46+
return;
47+
}
48+
4149
if is_box_of_default(cx, lhs_ty) && is_default_call(cx, rhs) && !rhs.span.from_expansion() {
4250
span_lint_and_then(
4351
cx,

tests/ui/default_box_assignments.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ fn main() {
2323
// No lint for assigning to Box<T> where T: !Default
2424
let mut b = Box::<str>::from("hi".to_string());
2525
b = Default::default();
26+
27+
// No lint for late initializations
28+
#[allow(clippy::needless_late_init)]
29+
let bb: Box<u32>;
30+
bb = Default::default();
2631
}

tests/ui/default_box_assignments.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ fn main() {
2323
// No lint for assigning to Box<T> where T: !Default
2424
let mut b = Box::<str>::from("hi".to_string());
2525
b = Default::default();
26+
27+
// No lint for late initializations
28+
#[allow(clippy::needless_late_init)]
29+
let bb: Box<u32>;
30+
bb = Default::default();
2631
}

0 commit comments

Comments
 (0)