Skip to content

Commit f729147

Browse files
committed
Auto merge of #10865 - Centri3:let_with_type_underscore_tracing, r=Jarcho
[`let_with_type_underscore`]: Don't emit on locals from procedural macros closes #10498 changelog: [`let_with_type_underscore`]: Don't emit on locals from procedural macros
2 parents c8a0565 + ba1fb74 commit f729147

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

clippy_lints/src/let_with_type_underscore.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
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;
@@ -25,14 +26,21 @@ declare_clippy_lint! {
2526
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);
2627

2728
impl LateLintPass<'_> for UnderscoreTyped {
28-
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
29+
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
2930
if_chain! {
3031
if !in_external_macro(cx.tcx.sess, local.span);
3132
if let Some(ty) = local.ty; // Ensure that it has a type defined
3233
if let TyKind::Infer = &ty.kind; // that type is '_'
3334
if local.span.ctxt() == ty.span.ctxt();
3435
then {
35-
span_lint_and_help(cx,
36+
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
37+
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
38+
if snippet(cx, ty.span, "_").trim() != "_" {
39+
return;
40+
}
41+
42+
span_lint_and_help(
43+
cx,
3644
LET_WITH_TYPE_UNDERSCORE,
3745
local.span,
3846
"variable declared with type underscore",

tests/ui/let_with_type_underscore.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1+
//@aux-build: proc_macros.rs
12
#![allow(unused)]
23
#![warn(clippy::let_with_type_underscore)]
3-
#![allow(clippy::let_unit_value)]
4+
#![allow(clippy::let_unit_value, clippy::needless_late_init)]
5+
6+
extern crate proc_macros;
47

58
fn func() -> &'static str {
69
""
710
}
811

12+
#[rustfmt::skip]
913
fn main() {
1014
// Will lint
1115
let x: _ = 1;
1216
let _: _ = 2;
1317
let x: _ = func();
18+
let x: _;
19+
x = ();
1420

1521
let x = 1; // Will not lint, Rust infers this to an integer before Clippy
1622
let x = func();
1723
let x: Vec<_> = Vec::<u32>::new();
1824
let x: [_; 1] = [1];
25+
let x : _ = 1;
26+
27+
// Do not lint from procedural macros
28+
proc_macros::with_span! {
29+
span
30+
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 = ();
41+
};
1942
}
+31-7
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:11: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:11: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:12: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:12: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:13: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:13: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)