Skip to content

Commit

Permalink
Auto merge of #8779 - binggh:easier-needless-late-init, r=llogiq
Browse files Browse the repository at this point in the history
Easier readability for `needless_late_init` message

Closes #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
  • Loading branch information
bors committed May 3, 2022
2 parents 38b7a04 + 7017eb1 commit a10fe90
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
8 changes: 5 additions & 3 deletions clippy_lints/src/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::path_to_local;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::needs_ordered_drop;
use clippy_utils::visitors::{expr_visitor, expr_visitor_no_bodies, is_local_used};
use rustc_errors::Applicability;
use rustc_errors::{Applicability, MultiSpan};
use rustc_hir::intravisit::Visitor;
use rustc_hir::{
BindingAnnotation, Block, Expr, ExprKind, HirId, Local, LocalSource, MatchSource, Node, Pat, PatKind, Stmt,
Expand Down Expand Up @@ -267,11 +267,14 @@ fn check<'tcx>(
match usage.expr.kind {
ExprKind::Assign(..) => {
let assign = LocalAssign::new(cx, usage.expr, binding_id)?;
let mut msg_span = MultiSpan::from_spans(vec![local_stmt.span, assign.span]);
msg_span.push_span_label(local_stmt.span, "created here");
msg_span.push_span_label(assign.span, "initialised here");

span_lint_and_then(
cx,
NEEDLESS_LATE_INIT,
local_stmt.span,
msg_span,
"unneeded late initialization",
|diag| {
diag.tool_only_span_suggestion(
Expand Down Expand Up @@ -365,7 +368,6 @@ fn check<'tcx>(
impl<'tcx> LateLintPass<'tcx> for NeedlessLateInit {
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
let mut parents = cx.tcx.hir().parent_iter(local.hir_id);

if_chain! {
if let Local {
init: None,
Expand Down
15 changes: 12 additions & 3 deletions tests/ui/needless_late_init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ error: unneeded late initialization
--> $DIR/needless_late_init.rs:60:5
|
LL | let x;
| ^^^^^^
| ^^^^^^ created here
LL | let y = SignificantDrop;
LL | x = 1;
| ^^^^^ initialised here
|
help: declare `x` here
|
Expand All @@ -134,7 +137,10 @@ error: unneeded late initialization
--> $DIR/needless_late_init.rs:64:5
|
LL | let x;
| ^^^^^^
| ^^^^^^ created here
LL | let y = 1;
LL | x = SignificantDrop;
| ^^^^^^^^^^^^^^^^^^^ initialised here
|
help: declare `x` here
|
Expand All @@ -145,7 +151,10 @@ error: unneeded late initialization
--> $DIR/needless_late_init.rs:68:5
|
LL | let x;
| ^^^^^^
| ^^^^^^ created here
...
LL | x = SignificantDrop;
| ^^^^^^^^^^^^^^^^^^^ initialised here
|
help: declare `x` here
|
Expand Down
22 changes: 17 additions & 5 deletions tests/ui/needless_late_init_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: unneeded late initialization
--> $DIR/needless_late_init_fixable.rs:6:5
|
LL | let a;
| ^^^^^^
| ^^^^^^ created here
LL | a = "zero";
| ^^^^^^^^^^ initialised here
|
= note: `-D clippy::needless-late-init` implied by `-D warnings`
help: declare `a` here
Expand All @@ -14,7 +16,10 @@ error: unneeded late initialization
--> $DIR/needless_late_init_fixable.rs:9:5
|
LL | let b;
| ^^^^^^
| ^^^^^^ created here
LL | let c;
LL | b = 1;
| ^^^^^ initialised here
|
help: declare `b` here
|
Expand All @@ -25,7 +30,10 @@ error: unneeded late initialization
--> $DIR/needless_late_init_fixable.rs:10:5
|
LL | let c;
| ^^^^^^
| ^^^^^^ created here
LL | b = 1;
LL | c = 2;
| ^^^^^ initialised here
|
help: declare `c` here
|
Expand All @@ -36,7 +44,9 @@ error: unneeded late initialization
--> $DIR/needless_late_init_fixable.rs:14:5
|
LL | let d: usize;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ created here
LL | d = 1;
| ^^^^^ initialised here
|
help: declare `d` here
|
Expand All @@ -47,7 +57,9 @@ error: unneeded late initialization
--> $DIR/needless_late_init_fixable.rs:17:5
|
LL | let e;
| ^^^^^^
| ^^^^^^ created here
LL | e = format!("{}", d);
| ^^^^^^^^^^^^^^^^^^^^ initialised here
|
help: declare `e` here
|
Expand Down

0 comments on commit a10fe90

Please sign in to comment.