Skip to content

Commit 143ff2d

Browse files
committed
Use type snippet instead of init expr for proc macro check
1 parent 0cc1454 commit 143ff2d

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

clippy_lints/src/let_with_type_underscore.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clippy_utils::{diagnostics::span_lint_and_help, is_from_proc_macro};
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::source::snippet;
23
use rustc_hir::{Local, TyKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_middle::lint::in_external_macro;
@@ -32,7 +33,12 @@ impl LateLintPass<'_> for UnderscoreTyped {
3233
if let TyKind::Infer = &ty.kind; // that type is '_'
3334
if local.span.ctxt() == ty.span.ctxt();
3435
then {
35-
if let Some(init) = local.init && is_from_proc_macro(cx, init) {
36+
let underscore_span = ty.span.with_lo(local.pat.span.hi());
37+
let snippet = snippet(cx, underscore_span, ": _");
38+
39+
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
40+
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
41+
if !snippet.trim().starts_with(':') && !snippet.trim().ends_with('_') {
3642
return;
3743
}
3844

@@ -41,7 +47,7 @@ impl LateLintPass<'_> for UnderscoreTyped {
4147
LET_WITH_TYPE_UNDERSCORE,
4248
local.span,
4349
"variable declared with type underscore",
44-
Some(ty.span.with_lo(local.pat.span.hi())),
50+
Some(underscore_span),
4551
"remove the explicit type `_` declaration"
4652
)
4753
}

tests/ui/let_with_type_underscore.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
//@aux-build: proc_macros.rs
22
#![allow(unused)]
33
#![warn(clippy::let_with_type_underscore)]
4-
#![allow(clippy::let_unit_value)]
4+
#![allow(clippy::let_unit_value, clippy::needless_late_init)]
55

66
extern crate proc_macros;
77

88
fn func() -> &'static str {
99
""
1010
}
1111

12+
#[rustfmt::skip]
1213
fn main() {
1314
// Will lint
1415
let x: _ = 1;
1516
let _: _ = 2;
1617
let x: _ = func();
18+
let x: _;
19+
x = ();
1720

1821
let x = 1; // Will not lint, Rust infers this to an integer before Clippy
1922
let x = func();
2023
let x: Vec<_> = Vec::<u32>::new();
2124
let x: [_; 1] = [1];
25+
let x : _ = 1;
2226

23-
// do not lint from procedural macros
27+
// Do not lint from procedural macros
2428
proc_macros::with_span! {
2529
span
2630
let x: _ = ();
31+
// Late initialization
32+
let x: _;
33+
x = ();
34+
// Ensure weird formatting will not break it (hopefully)
35+
let x : _ = 1;
36+
let x
37+
: _ = 1;
38+
let x :
39+
_;
40+
x = ();
2741
};
2842
}
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
11
error: variable declared with type underscore
2-
--> $DIR/let_with_type_underscore.rs:14:5
2+
--> $DIR/let_with_type_underscore.rs:15:5
33
|
44
LL | let x: _ = 1;
55
| ^^^^^^^^^^^^^
66
|
77
help: remove the explicit type `_` declaration
8-
--> $DIR/let_with_type_underscore.rs:14:10
8+
--> $DIR/let_with_type_underscore.rs:15:10
99
|
1010
LL | let x: _ = 1;
1111
| ^^^
1212
= note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
1313

1414
error: variable declared with type underscore
15-
--> $DIR/let_with_type_underscore.rs:15:5
15+
--> $DIR/let_with_type_underscore.rs:16:5
1616
|
1717
LL | let _: _ = 2;
1818
| ^^^^^^^^^^^^^
1919
|
2020
help: remove the explicit type `_` declaration
21-
--> $DIR/let_with_type_underscore.rs:15:10
21+
--> $DIR/let_with_type_underscore.rs:16:10
2222
|
2323
LL | let _: _ = 2;
2424
| ^^^
2525

2626
error: variable declared with type underscore
27-
--> $DIR/let_with_type_underscore.rs:16:5
27+
--> $DIR/let_with_type_underscore.rs:17:5
2828
|
2929
LL | let x: _ = func();
3030
| ^^^^^^^^^^^^^^^^^^
3131
|
3232
help: remove the explicit type `_` declaration
33-
--> $DIR/let_with_type_underscore.rs:16:10
33+
--> $DIR/let_with_type_underscore.rs:17:10
3434
|
3535
LL | let x: _ = func();
3636
| ^^^
3737

38-
error: aborting due to 3 previous errors
38+
error: variable declared with type underscore
39+
--> $DIR/let_with_type_underscore.rs:18:5
40+
|
41+
LL | let x: _;
42+
| ^^^^^^^^^
43+
|
44+
help: remove the explicit type `_` declaration
45+
--> $DIR/let_with_type_underscore.rs:18:10
46+
|
47+
LL | let x: _;
48+
| ^^^
49+
50+
error: variable declared with type underscore
51+
--> $DIR/let_with_type_underscore.rs:25:5
52+
|
53+
LL | let x : _ = 1;
54+
| ^^^^^^^^^^^^^^
55+
|
56+
help: remove the explicit type `_` declaration
57+
--> $DIR/let_with_type_underscore.rs:25:10
58+
|
59+
LL | let x : _ = 1;
60+
| ^^^^
61+
62+
error: aborting due to 5 previous errors
3963

0 commit comments

Comments
 (0)