Skip to content

Commit 50cff11

Browse files
Make let_with_type_underscore help message into a suggestion (#14749)
changelog: none
2 parents 34cd948 + 8c04522 commit 50cff11

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

clippy_lints/src/let_with_type_underscore.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_from_proc_macro;
3+
use rustc_errors::Applicability;
34
use rustc_hir::{LetStmt, TyKind};
45
use rustc_lint::{LateContext, LateLintPass};
56
use rustc_session::declare_lint_pass;
@@ -32,13 +33,19 @@ impl<'tcx> LateLintPass<'tcx> for UnderscoreTyped {
3233
&& !local.span.in_external_macro(cx.tcx.sess.source_map())
3334
&& !is_from_proc_macro(cx, ty)
3435
{
35-
span_lint_and_help(
36+
span_lint_and_then(
3637
cx,
3738
LET_WITH_TYPE_UNDERSCORE,
3839
local.span,
3940
"variable declared with type underscore",
40-
Some(ty.span.with_lo(local.pat.span.hi())),
41-
"remove the explicit type `_` declaration",
41+
|diag| {
42+
diag.span_suggestion_verbose(
43+
ty.span.with_lo(local.pat.span.hi()),
44+
"remove the explicit type `_` declaration",
45+
"",
46+
Applicability::MachineApplicable,
47+
);
48+
},
4249
);
4350
}
4451
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//@aux-build: proc_macros.rs
2+
#![allow(unused)]
3+
#![warn(clippy::let_with_type_underscore)]
4+
#![allow(clippy::let_unit_value, clippy::needless_late_init)]
5+
6+
extern crate proc_macros;
7+
8+
fn func() -> &'static str {
9+
""
10+
}
11+
12+
#[rustfmt::skip]
13+
fn main() {
14+
// Will lint
15+
let x = 1;
16+
//~^ let_with_type_underscore
17+
let _ = 2;
18+
//~^ let_with_type_underscore
19+
let x = func();
20+
//~^ let_with_type_underscore
21+
let x;
22+
//~^ let_with_type_underscore
23+
x = ();
24+
25+
let x = 1; // Will not lint, Rust infers this to an integer before Clippy
26+
let x = func();
27+
let x: Vec<_> = Vec::<u32>::new();
28+
let x: [_; 1] = [1];
29+
let x = 1;
30+
//~^ let_with_type_underscore
31+
32+
// Do not lint from procedural macros
33+
proc_macros::with_span! {
34+
span
35+
let x: _ = ();
36+
// Late initialization
37+
let x: _;
38+
x = ();
39+
// Ensure weird formatting will not break it (hopefully)
40+
let x : _ = 1;
41+
let x
42+
: _ = 1;
43+
let x :
44+
_;
45+
x = ();
46+
};
47+
}

tests/ui/let_with_type_underscore.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error: variable declared with type underscore
44
LL | let x: _ = 1;
55
| ^^^^^^^^^^^^^
66
|
7-
help: remove the explicit type `_` declaration
8-
--> tests/ui/let_with_type_underscore.rs:15:10
9-
|
10-
LL | let x: _ = 1;
11-
| ^^^
127
= note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
138
= help: to override `-D warnings` add `#[allow(clippy::let_with_type_underscore)]`
9+
help: remove the explicit type `_` declaration
10+
|
11+
LL - let x: _ = 1;
12+
LL + let x = 1;
13+
|
1414

1515
error: variable declared with type underscore
1616
--> tests/ui/let_with_type_underscore.rs:17:5
@@ -19,10 +19,10 @@ LL | let _: _ = 2;
1919
| ^^^^^^^^^^^^^
2020
|
2121
help: remove the explicit type `_` declaration
22-
--> tests/ui/let_with_type_underscore.rs:17:10
2322
|
24-
LL | let _: _ = 2;
25-
| ^^^
23+
LL - let _: _ = 2;
24+
LL + let _ = 2;
25+
|
2626

2727
error: variable declared with type underscore
2828
--> tests/ui/let_with_type_underscore.rs:19:5
@@ -31,10 +31,10 @@ LL | let x: _ = func();
3131
| ^^^^^^^^^^^^^^^^^^
3232
|
3333
help: remove the explicit type `_` declaration
34-
--> tests/ui/let_with_type_underscore.rs:19:10
3534
|
36-
LL | let x: _ = func();
37-
| ^^^
35+
LL - let x: _ = func();
36+
LL + let x = func();
37+
|
3838

3939
error: variable declared with type underscore
4040
--> tests/ui/let_with_type_underscore.rs:21:5
@@ -43,10 +43,10 @@ LL | let x: _;
4343
| ^^^^^^^^^
4444
|
4545
help: remove the explicit type `_` declaration
46-
--> tests/ui/let_with_type_underscore.rs:21:10
4746
|
48-
LL | let x: _;
49-
| ^^^
47+
LL - let x: _;
48+
LL + let x;
49+
|
5050

5151
error: variable declared with type underscore
5252
--> tests/ui/let_with_type_underscore.rs:29:5
@@ -55,10 +55,10 @@ LL | let x : _ = 1;
5555
| ^^^^^^^^^^^^^^
5656
|
5757
help: remove the explicit type `_` declaration
58-
--> tests/ui/let_with_type_underscore.rs:29:10
5958
|
60-
LL | let x : _ = 1;
61-
| ^^^^
59+
LL - let x : _ = 1;
60+
LL + let x = 1;
61+
|
6262

6363
error: aborting due to 5 previous errors
6464

0 commit comments

Comments
 (0)