about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-05-03 20:42:35 +0000
committerbors <bors@rust-lang.org>2022-05-03 20:42:35 +0000
commita10fe902d2c7afe3d42e9b00627a6983488521d3 (patch)
tree8cb315c0df07395438dd0b7ef76a20f93fdde317
parent38b7a041cc8b346927b74edd8c7ddccee47a4c93 (diff)
parent7017eb102d71526205b2feded779ff1f26d11da1 (diff)
downloadrust-a10fe902d2c7afe3d42e9b00627a6983488521d3.tar.gz
rust-a10fe902d2c7afe3d42e9b00627a6983488521d3.zip
Auto merge of #8779 - binggh:easier-needless-late-init, r=llogiq
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
-rw-r--r--clippy_lints/src/needless_late_init.rs8
-rw-r--r--tests/ui/needless_late_init.stderr15
-rw-r--r--tests/ui/needless_late_init_fixable.stderr22
3 files changed, 34 insertions, 11 deletions
diff --git a/clippy_lints/src/needless_late_init.rs b/clippy_lints/src/needless_late_init.rs
index a863a7990ca..b70871b38be 100644
--- a/clippy_lints/src/needless_late_init.rs
+++ b/clippy_lints/src/needless_late_init.rs
@@ -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,
@@ -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(
@@ -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,
diff --git a/tests/ui/needless_late_init.stderr b/tests/ui/needless_late_init.stderr
index 015c173561a..d33a117b288 100644
--- a/tests/ui/needless_late_init.stderr
+++ b/tests/ui/needless_late_init.stderr
@@ -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
    |
@@ -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
    |
@@ -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
    |
diff --git a/tests/ui/needless_late_init_fixable.stderr b/tests/ui/needless_late_init_fixable.stderr
index c97cd93fd2f..8c664309e3e 100644
--- a/tests/ui/needless_late_init_fixable.stderr
+++ b/tests/ui/needless_late_init_fixable.stderr
@@ -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
@@ -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
    |
@@ -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
    |
@@ -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
    |
@@ -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
    |