Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier readability for needless_late_init message #8779

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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