Skip to content

Commit c618387

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

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

Diff for: clippy_lints/src/needless_late_init.rs

+7-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,7 @@ fn stmt_needs_ordered_drop(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
100100
#[derive(Debug)]
101101
struct LocalAssign {
102102
lhs_id: HirId,
103-
lhs_span: Span,
103+
_lhs_span: Span,
104104
rhs_span: Span,
105105
span: Span,
106106
}
@@ -118,7 +118,7 @@ impl LocalAssign {
118118

119119
Some(Self {
120120
lhs_id: path_to_local(lhs)?,
121-
lhs_span: lhs.span,
121+
_lhs_span: lhs.span,
122122
rhs_span: rhs.span.source_callsite(),
123123
span,
124124
})
@@ -281,7 +281,10 @@ fn check<'tcx>(
281281
format!("move the declaration `{binding_name}` here"),
282282
vec![
283283
(local_stmt.span, String::new()),
284-
(assign.lhs_span, let_snippet.to_owned()),
284+
(
285+
assign.span,
286+
let_snippet.to_owned() + " = " + &snippet(cx, assign.rhs_span, ".."),
287+
),
285288
],
286289
Applicability::MachineApplicable,
287290
);

Diff for: 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+
}

Diff for: 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+
}

Diff for: 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)