Make let_with_type_underscore help message into a suggestion
This commit is contained in:
parent
cf6bebb343
commit
8c045221b5
3 changed files with 75 additions and 21 deletions
|
|
@ -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;
|
||||
|
|
@ -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,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
47
tests/ui/let_with_type_underscore.fixed
Normal file
47
tests/ui/let_with_type_underscore.fixed
Normal file
|
|
@ -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 = ();
|
||||
};
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue