Skip to content

Commit ce9ae6f

Browse files
committed
fix suggestion for assignments wrapped in parentheses under needless_late_init
1 parent 4a94ad6 commit ce9ae6f

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

clippy_lints/src/needless_late_init.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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};
4+
use clippy_utils::sugg::has_enclosing_paren;
45
use clippy_utils::ty::needs_ordered_drop;
56
use clippy_utils::visitors::{for_each_expr, for_each_expr_without_closures, is_local_used};
67
use core::ops::ControlFlow;
@@ -271,6 +272,15 @@ fn check<'tcx>(
271272
msg_span.push_span_label(local_stmt.span, "created here");
272273
msg_span.push_span_label(assign.span, "initialised here");
273274

275+
let span_to_replace = if has_enclosing_paren(snippet(cx, usage.expr.span, "..")) {
276+
assign
277+
.lhs_span
278+
.with_lo(usage.expr.span.lo())
279+
.with_hi(usage.expr.span.hi())
280+
} else {
281+
assign.span
282+
};
283+
274284
span_lint_and_then(
275285
cx,
276286
NEEDLESS_LATE_INIT,
@@ -281,7 +291,10 @@ fn check<'tcx>(
281291
format!("move the declaration `{binding_name}` here"),
282292
vec![
283293
(local_stmt.span, String::new()),
284-
(assign.lhs_span, let_snippet.to_owned()),
294+
(
295+
span_to_replace,
296+
let_snippet.to_owned() + " = " + &snippet(cx, assign.rhs_span, ".."),
297+
),
285298
],
286299
Applicability::MachineApplicable,
287300
);

tests/ui/needless_late_init.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,8 @@ fn issue13776() {
281281
let x;
282282
issue13776_mac!(x, 10); // should not lint
283283
}
284+
285+
fn issue9895() {
286+
287+
let r = 5;
288+
}

tests/ui/needless_late_init.rs

+5
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,8 @@ fn issue13776() {
281281
let x;
282282
issue13776_mac!(x, 10); // should not lint
283283
}
284+
285+
fn issue9895() {
286+
let r;
287+
(r = 5);
288+
}

tests/ui/needless_late_init.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,19 @@ LL | },
256256
LL ~ };
257257
|
258258

259-
error: aborting due to 16 previous errors
259+
error: unneeded late initialization
260+
--> tests/ui/needless_late_init.rs:286:5
261+
|
262+
LL | let r;
263+
| ^^^^^^ created here
264+
LL | (r = 5);
265+
| ^^^^^^^ initialised here
266+
|
267+
help: move the declaration `r` here
268+
|
269+
LL ~
270+
LL ~ let r = 5;
271+
|
272+
273+
error: aborting due to 17 previous errors
260274

0 commit comments

Comments
 (0)