Skip to content

Commit e42586d

Browse files
fix: let_with_type_underscore don't eat closing paren in let (i): _ = 0; (#15386)
Apparently, one can surround a pattern with an arbitrary number of parens, and the resulting `Pat` won't include the span of those parens. And the previous approach extended the to-be-deleted span all the way to the end of `Pat`'s span, so it included the closing paren. I think the new approach is more robust anyway, since all we care to remove (besides the `_`) is the `:`, so we search just for that. ~This does leave one extra whitespace in one of the test cases, but I'd say let rustfmt handle that.~ fixed in the third commit changelog: [`let_with_type_underscore`]: don't eat closing paren in `let (i): _ = 0;` fixes #15377
2 parents cde155d + 0f98da7 commit e42586d

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

clippy_lints/src/let_with_type_underscore.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_from_proc_macro;
3+
use clippy_utils::source::{IntoSpan, SpanRangeExt};
34
use rustc_errors::Applicability;
45
use rustc_hir::{LetStmt, TyKind};
56
use rustc_lint::{LateContext, LateLintPass};
@@ -30,17 +31,23 @@ impl<'tcx> LateLintPass<'tcx> for UnderscoreTyped {
3031
if let Some(ty) = local.ty // Ensure that it has a type defined
3132
&& let TyKind::Infer(()) = &ty.kind // that type is '_'
3233
&& local.span.eq_ctxt(ty.span)
33-
&& !local.span.in_external_macro(cx.tcx.sess.source_map())
34+
&& let sm = cx.tcx.sess.source_map()
35+
&& !local.span.in_external_macro(sm)
3436
&& !is_from_proc_macro(cx, ty)
3537
{
38+
let span_to_remove = sm
39+
.span_extend_to_prev_char_before(ty.span, ':', true)
40+
.with_leading_whitespace(cx)
41+
.into_span();
42+
3643
span_lint_and_then(
3744
cx,
3845
LET_WITH_TYPE_UNDERSCORE,
3946
local.span,
4047
"variable declared with type underscore",
4148
|diag| {
4249
diag.span_suggestion_verbose(
43-
ty.span.with_lo(local.pat.span.hi()),
50+
span_to_remove,
4451
"remove the explicit type `_` declaration",
4552
"",
4653
Applicability::MachineApplicable,

tests/ui/let_with_type_underscore.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ fn main() {
4545
x = ();
4646
};
4747
}
48+
49+
fn issue15377() {
50+
let (a) = 0;
51+
//~^ let_with_type_underscore
52+
let ((a)) = 0;
53+
//~^ let_with_type_underscore
54+
let ((a,)) = (0,);
55+
//~^ let_with_type_underscore
56+
#[rustfmt::skip]
57+
let ( (a ) ) = 0;
58+
//~^ let_with_type_underscore
59+
}

tests/ui/let_with_type_underscore.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ fn main() {
4545
x = ();
4646
};
4747
}
48+
49+
fn issue15377() {
50+
let (a): _ = 0;
51+
//~^ let_with_type_underscore
52+
let ((a)): _ = 0;
53+
//~^ let_with_type_underscore
54+
let ((a,)): _ = (0,);
55+
//~^ let_with_type_underscore
56+
#[rustfmt::skip]
57+
let ( (a ) ): _ = 0;
58+
//~^ let_with_type_underscore
59+
}

tests/ui/let_with_type_underscore.stderr

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,53 @@ LL - let x : _ = 1;
6060
LL + let x = 1;
6161
|
6262

63-
error: aborting due to 5 previous errors
63+
error: variable declared with type underscore
64+
--> tests/ui/let_with_type_underscore.rs:50:5
65+
|
66+
LL | let (a): _ = 0;
67+
| ^^^^^^^^^^^^^^^
68+
|
69+
help: remove the explicit type `_` declaration
70+
|
71+
LL - let (a): _ = 0;
72+
LL + let (a) = 0;
73+
|
74+
75+
error: variable declared with type underscore
76+
--> tests/ui/let_with_type_underscore.rs:52:5
77+
|
78+
LL | let ((a)): _ = 0;
79+
| ^^^^^^^^^^^^^^^^^
80+
|
81+
help: remove the explicit type `_` declaration
82+
|
83+
LL - let ((a)): _ = 0;
84+
LL + let ((a)) = 0;
85+
|
86+
87+
error: variable declared with type underscore
88+
--> tests/ui/let_with_type_underscore.rs:54:5
89+
|
90+
LL | let ((a,)): _ = (0,);
91+
| ^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
help: remove the explicit type `_` declaration
94+
|
95+
LL - let ((a,)): _ = (0,);
96+
LL + let ((a,)) = (0,);
97+
|
98+
99+
error: variable declared with type underscore
100+
--> tests/ui/let_with_type_underscore.rs:57:5
101+
|
102+
LL | let ( (a ) ): _ = 0;
103+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
104+
|
105+
help: remove the explicit type `_` declaration
106+
|
107+
LL - let ( (a ) ): _ = 0;
108+
LL + let ( (a ) ) = 0;
109+
|
110+
111+
error: aborting due to 9 previous errors
64112

0 commit comments

Comments
 (0)