Skip to content
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
11 changes: 9 additions & 2 deletions clippy_lints/src/let_with_type_underscore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_from_proc_macro;
use clippy_utils::source::{IntoSpan, SpanRangeExt};
use rustc_errors::Applicability;
use rustc_hir::{LetStmt, TyKind};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -30,17 +31,23 @@ impl<'tcx> LateLintPass<'tcx> for UnderscoreTyped {
if let Some(ty) = local.ty // Ensure that it has a type defined
&& let TyKind::Infer(()) = &ty.kind // that type is '_'
&& local.span.eq_ctxt(ty.span)
&& !local.span.in_external_macro(cx.tcx.sess.source_map())
&& let sm = cx.tcx.sess.source_map()
&& !local.span.in_external_macro(sm)
&& !is_from_proc_macro(cx, ty)
{
let span_to_remove = sm
.span_extend_to_prev_char_before(ty.span, ':', true)
.with_leading_whitespace(cx)
.into_span();

span_lint_and_then(
cx,
LET_WITH_TYPE_UNDERSCORE,
local.span,
"variable declared with type underscore",
|diag| {
diag.span_suggestion_verbose(
ty.span.with_lo(local.pat.span.hi()),
span_to_remove,
"remove the explicit type `_` declaration",
"",
Applicability::MachineApplicable,
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/let_with_type_underscore.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ fn main() {
x = ();
};
}

fn issue15377() {
let (a) = 0;
//~^ let_with_type_underscore
let ((a)) = 0;
//~^ let_with_type_underscore
let ((a,)) = (0,);
//~^ let_with_type_underscore
#[rustfmt::skip]
let ( (a ) ) = 0;
//~^ let_with_type_underscore
}
12 changes: 12 additions & 0 deletions tests/ui/let_with_type_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ fn main() {
x = ();
};
}

fn issue15377() {
let (a): _ = 0;
//~^ let_with_type_underscore
let ((a)): _ = 0;
//~^ let_with_type_underscore
let ((a,)): _ = (0,);
//~^ let_with_type_underscore
#[rustfmt::skip]
let ( (a ) ): _ = 0;
//~^ let_with_type_underscore
}
50 changes: 49 additions & 1 deletion tests/ui/let_with_type_underscore.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,53 @@ LL - let x : _ = 1;
LL + let x = 1;
|

error: aborting due to 5 previous errors
error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:50:5
|
LL | let (a): _ = 0;
| ^^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
|
LL - let (a): _ = 0;
LL + let (a) = 0;
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:52:5
|
LL | let ((a)): _ = 0;
| ^^^^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
|
LL - let ((a)): _ = 0;
LL + let ((a)) = 0;
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:54:5
|
LL | let ((a,)): _ = (0,);
| ^^^^^^^^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
|
LL - let ((a,)): _ = (0,);
LL + let ((a,)) = (0,);
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:57:5
|
LL | let ( (a ) ): _ = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
|
LL - let ( (a ) ): _ = 0;
LL + let ( (a ) ) = 0;
|

error: aborting due to 9 previous errors