Skip to content

Commit a10fe90

Browse files
committed
Auto merge of rust-lang#8779 - binggh:easier-needless-late-init, r=llogiq
Easier readability for `needless_late_init` message Closes rust-lang#8530 Updated the lint to use a `MultiSpan`, showing where the `let` statement was first used and where the initialisation statement was done, as in the format described, for easier readability. Was wondering why, when pushing the span label for the initialisation statement, that sometimes the prior statement above the initialisation statement gets pulled into the output as well - any insight is appreciated! --- changelog: [`needless_late_init`]: Now shows the `let` statement where it was first initialized
2 parents 38b7a04 + 7017eb1 commit a10fe90

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

clippy_lints/src/needless_late_init.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::path_to_local;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::ty::needs_ordered_drop;
55
use clippy_utils::visitors::{expr_visitor, expr_visitor_no_bodies, is_local_used};
6-
use rustc_errors::Applicability;
6+
use rustc_errors::{Applicability, MultiSpan};
77
use rustc_hir::intravisit::Visitor;
88
use rustc_hir::{
99
BindingAnnotation, Block, Expr, ExprKind, HirId, Local, LocalSource, MatchSource, Node, Pat, PatKind, Stmt,
@@ -267,11 +267,14 @@ fn check<'tcx>(
267267
match usage.expr.kind {
268268
ExprKind::Assign(..) => {
269269
let assign = LocalAssign::new(cx, usage.expr, binding_id)?;
270+
let mut msg_span = MultiSpan::from_spans(vec![local_stmt.span, assign.span]);
271+
msg_span.push_span_label(local_stmt.span, "created here");
272+
msg_span.push_span_label(assign.span, "initialised here");
270273

271274
span_lint_and_then(
272275
cx,
273276
NEEDLESS_LATE_INIT,
274-
local_stmt.span,
277+
msg_span,
275278
"unneeded late initialization",
276279
|diag| {
277280
diag.tool_only_span_suggestion(
@@ -365,7 +368,6 @@ fn check<'tcx>(
365368
impl<'tcx> LateLintPass<'tcx> for NeedlessLateInit {
366369
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
367370
let mut parents = cx.tcx.hir().parent_iter(local.hir_id);
368-
369371
if_chain! {
370372
if let Local {
371373
init: None,

tests/ui/needless_late_init.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ error: unneeded late initialization
123123
--> $DIR/needless_late_init.rs:60:5
124124
|
125125
LL | let x;
126-
| ^^^^^^
126+
| ^^^^^^ created here
127+
LL | let y = SignificantDrop;
128+
LL | x = 1;
129+
| ^^^^^ initialised here
127130
|
128131
help: declare `x` here
129132
|
@@ -134,7 +137,10 @@ error: unneeded late initialization
134137
--> $DIR/needless_late_init.rs:64:5
135138
|
136139
LL | let x;
137-
| ^^^^^^
140+
| ^^^^^^ created here
141+
LL | let y = 1;
142+
LL | x = SignificantDrop;
143+
| ^^^^^^^^^^^^^^^^^^^ initialised here
138144
|
139145
help: declare `x` here
140146
|
@@ -145,7 +151,10 @@ error: unneeded late initialization
145151
--> $DIR/needless_late_init.rs:68:5
146152
|
147153
LL | let x;
148-
| ^^^^^^
154+
| ^^^^^^ created here
155+
...
156+
LL | x = SignificantDrop;
157+
| ^^^^^^^^^^^^^^^^^^^ initialised here
149158
|
150159
help: declare `x` here
151160
|

tests/ui/needless_late_init_fixable.stderr

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: unneeded late initialization
22
--> $DIR/needless_late_init_fixable.rs:6:5
33
|
44
LL | let a;
5-
| ^^^^^^
5+
| ^^^^^^ created here
6+
LL | a = "zero";
7+
| ^^^^^^^^^^ initialised here
68
|
79
= note: `-D clippy::needless-late-init` implied by `-D warnings`
810
help: declare `a` here
@@ -14,7 +16,10 @@ error: unneeded late initialization
1416
--> $DIR/needless_late_init_fixable.rs:9:5
1517
|
1618
LL | let b;
17-
| ^^^^^^
19+
| ^^^^^^ created here
20+
LL | let c;
21+
LL | b = 1;
22+
| ^^^^^ initialised here
1823
|
1924
help: declare `b` here
2025
|
@@ -25,7 +30,10 @@ error: unneeded late initialization
2530
--> $DIR/needless_late_init_fixable.rs:10:5
2631
|
2732
LL | let c;
28-
| ^^^^^^
33+
| ^^^^^^ created here
34+
LL | b = 1;
35+
LL | c = 2;
36+
| ^^^^^ initialised here
2937
|
3038
help: declare `c` here
3139
|
@@ -36,7 +44,9 @@ error: unneeded late initialization
3644
--> $DIR/needless_late_init_fixable.rs:14:5
3745
|
3846
LL | let d: usize;
39-
| ^^^^^^^^^^^^^
47+
| ^^^^^^^^^^^^^ created here
48+
LL | d = 1;
49+
| ^^^^^ initialised here
4050
|
4151
help: declare `d` here
4252
|
@@ -47,7 +57,9 @@ error: unneeded late initialization
4757
--> $DIR/needless_late_init_fixable.rs:17:5
4858
|
4959
LL | let e;
50-
| ^^^^^^
60+
| ^^^^^^ created here
61+
LL | e = format!("{}", d);
62+
| ^^^^^^^^^^^^^^^^^^^^ initialised here
5163
|
5264
help: declare `e` here
5365
|

0 commit comments

Comments
 (0)