Skip to content

Commit 62c41e2

Browse files
committed
fix suggestion for assignments wrapped in parentheses under needless_late_init
1 parent 8cef0b6 commit 62c41e2

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

clippy_lints/src/needless_late_init.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::path_to_local;
3-
use clippy_utils::source::{SourceText, SpanRangeExt};
3+
use clippy_utils::source::{SourceText, SpanRangeExt, snippet};
44
use clippy_utils::ty::needs_ordered_drop;
55
use clippy_utils::visitors::{for_each_expr, for_each_expr_without_closures, is_local_used};
66
use core::ops::ControlFlow;
@@ -100,7 +100,6 @@ fn stmt_needs_ordered_drop(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
100100
#[derive(Debug)]
101101
struct LocalAssign {
102102
lhs_id: HirId,
103-
lhs_span: Span,
104103
rhs_span: Span,
105104
span: Span,
106105
}
@@ -118,7 +117,6 @@ impl LocalAssign {
118117

119118
Some(Self {
120119
lhs_id: path_to_local(lhs)?,
121-
lhs_span: lhs.span,
122120
rhs_span: rhs.span.source_callsite(),
123121
span,
124122
})
@@ -281,7 +279,10 @@ fn check<'tcx>(
281279
format!("move the declaration `{binding_name}` here"),
282280
vec![
283281
(local_stmt.span, String::new()),
284-
(assign.lhs_span, let_snippet.to_owned()),
282+
(
283+
assign.span,
284+
let_snippet.to_owned() + " = " + &snippet(cx, assign.rhs_span, ".."),
285+
),
285286
],
286287
Applicability::MachineApplicable,
287288
);

tests/ui/needless_late_init.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,9 @@ fn issue13776() {
297297
let x;
298298
issue13776_mac!(x, 10); // should not lint
299299
}
300+
301+
fn issue9895() {
302+
303+
//~^ needless_late_init
304+
let r = 5;
305+
}

tests/ui/needless_late_init.rs

+6
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,9 @@ fn issue13776() {
297297
let x;
298298
issue13776_mac!(x, 10); // should not lint
299299
}
300+
301+
fn issue9895() {
302+
let r;
303+
//~^ needless_late_init
304+
(r = 5);
305+
}

tests/ui/needless_late_init.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,21 @@ LL | },
275275
LL ~ };
276276
|
277277

278-
error: aborting due to 16 previous errors
278+
error: unneeded late initialization
279+
--> tests/ui/needless_late_init.rs:302:5
280+
|
281+
LL | let r;
282+
| ^^^^^^ created here
283+
LL |
284+
LL | (r = 5);
285+
| ^^^^^^^ initialised here
286+
|
287+
help: move the declaration `r` here
288+
|
289+
LL ~
290+
LL |
291+
LL ~ let r = 5;
292+
|
293+
294+
error: aborting due to 17 previous errors
279295

0 commit comments

Comments
 (0)