1
Fork 0

fix suggestion for assignments have enclosing parentheses under needless_late_init (#14169)

fixes #9895

changelog: [`needless_late_init`]: correct suggestion when assignments
have enclosing parentheses
This commit is contained in:
Timo 2025-03-19 22:51:38 +00:00 committed by GitHub
commit 221ae5f176
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 5 deletions

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::path_to_local;
use clippy_utils::source::{SourceText, SpanRangeExt};
use clippy_utils::source::{SourceText, SpanRangeExt, snippet};
use clippy_utils::ty::needs_ordered_drop;
use clippy_utils::visitors::{for_each_expr, for_each_expr_without_closures, is_local_used};
use core::ops::ControlFlow;
@ -100,7 +100,6 @@ fn stmt_needs_ordered_drop(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
#[derive(Debug)]
struct LocalAssign {
lhs_id: HirId,
lhs_span: Span,
rhs_span: Span,
span: Span,
}
@ -118,7 +117,6 @@ impl LocalAssign {
Some(Self {
lhs_id: path_to_local(lhs)?,
lhs_span: lhs.span,
rhs_span: rhs.span.source_callsite(),
span,
})
@ -281,7 +279,10 @@ fn check<'tcx>(
format!("move the declaration `{binding_name}` here"),
vec![
(local_stmt.span, String::new()),
(assign.lhs_span, let_snippet.to_owned()),
(
assign.span,
let_snippet.to_owned() + " = " + &snippet(cx, assign.rhs_span, ".."),
),
],
Applicability::MachineApplicable,
);

View file

@ -297,3 +297,9 @@ fn issue13776() {
let x;
issue13776_mac!(x, 10); // should not lint
}
fn issue9895() {
//~^ needless_late_init
let r = 5;
}

View file

@ -297,3 +297,9 @@ fn issue13776() {
let x;
issue13776_mac!(x, 10); // should not lint
}
fn issue9895() {
let r;
//~^ needless_late_init
(r = 5);
}

View file

@ -275,5 +275,21 @@ LL | },
LL ~ };
|
error: aborting due to 16 previous errors
error: unneeded late initialization
--> tests/ui/needless_late_init.rs:302:5
|
LL | let r;
| ^^^^^^ created here
LL |
LL | (r = 5);
| ^^^^^^^ initialised here
|
help: move the declaration `r` here
|
LL ~
LL |
LL ~ let r = 5;
|
error: aborting due to 17 previous errors