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
15 changes: 11 additions & 4 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_help;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_from_proc_macro;
use rustc_errors::Applicability;
use rustc_hir::{LetStmt, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
Expand Down Expand Up @@ -32,13 +33,19 @@ impl<'tcx> LateLintPass<'tcx> for UnderscoreTyped {
&& !local.span.in_external_macro(cx.tcx.sess.source_map())
&& !is_from_proc_macro(cx, ty)
{
span_lint_and_help(
span_lint_and_then(
cx,
LET_WITH_TYPE_UNDERSCORE,
local.span,
"variable declared with type underscore",
Some(ty.span.with_lo(local.pat.span.hi())),
"remove the explicit type `_` declaration",
|diag| {
diag.span_suggestion_verbose(
ty.span.with_lo(local.pat.span.hi()),
"remove the explicit type `_` declaration",
"",
Applicability::MachineApplicable,
);
},
);
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/ui/let_with_type_underscore.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//@aux-build: proc_macros.rs
#![allow(unused)]
#![warn(clippy::let_with_type_underscore)]
#![allow(clippy::let_unit_value, clippy::needless_late_init)]

extern crate proc_macros;

fn func() -> &'static str {
""
}

#[rustfmt::skip]
fn main() {
// Will lint
let x = 1;
//~^ let_with_type_underscore
let _ = 2;
//~^ let_with_type_underscore
let x = func();
//~^ let_with_type_underscore
let x;
//~^ let_with_type_underscore
x = ();

let x = 1; // Will not lint, Rust infers this to an integer before Clippy
let x = func();
let x: Vec<_> = Vec::<u32>::new();
let x: [_; 1] = [1];
let x = 1;
//~^ let_with_type_underscore

// Do not lint from procedural macros
proc_macros::with_span! {
span
let x: _ = ();
// Late initialization
let x: _;
x = ();
// Ensure weird formatting will not break it (hopefully)
let x : _ = 1;
let x
: _ = 1;
let x :
_;
x = ();
};
}
34 changes: 17 additions & 17 deletions tests/ui/let_with_type_underscore.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ error: variable declared with type underscore
LL | let x: _ = 1;
| ^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> tests/ui/let_with_type_underscore.rs:15:10
|
LL | let x: _ = 1;
| ^^^
= note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::let_with_type_underscore)]`
help: remove the explicit type `_` declaration
|
LL - let x: _ = 1;
LL + let x = 1;
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:17:5
Expand All @@ -19,10 +19,10 @@ LL | let _: _ = 2;
| ^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> tests/ui/let_with_type_underscore.rs:17:10
|
LL | let _: _ = 2;
| ^^^
LL - let _: _ = 2;
LL + let _ = 2;
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:19:5
Expand All @@ -31,10 +31,10 @@ LL | let x: _ = func();
| ^^^^^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> tests/ui/let_with_type_underscore.rs:19:10
|
LL | let x: _ = func();
| ^^^
LL - let x: _ = func();
LL + let x = func();
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:21:5
Expand All @@ -43,10 +43,10 @@ LL | let x: _;
| ^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> tests/ui/let_with_type_underscore.rs:21:10
|
LL | let x: _;
| ^^^
LL - let x: _;
LL + let x;
|

error: variable declared with type underscore
--> tests/ui/let_with_type_underscore.rs:29:5
Expand All @@ -55,10 +55,10 @@ LL | let x : _ = 1;
| ^^^^^^^^^^^^^^
|
help: remove the explicit type `_` declaration
--> tests/ui/let_with_type_underscore.rs:29:10
|
LL | let x : _ = 1;
| ^^^^
LL - let x : _ = 1;
LL + let x = 1;
|

error: aborting due to 5 previous errors