Use type snippet instead of init expr for proc macro check
This commit is contained in:
parent
0cc1454db5
commit
143ff2dfc3
@ -1,4 +1,5 @@
|
||||
use clippy_utils::{diagnostics::span_lint_and_help, is_from_proc_macro};
|
||||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use clippy_utils::source::snippet;
|
||||
use rustc_hir::{Local, TyKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
@ -32,7 +33,12 @@ fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>
|
||||
if let TyKind::Infer = &ty.kind; // that type is '_'
|
||||
if local.span.ctxt() == ty.span.ctxt();
|
||||
then {
|
||||
if let Some(init) = local.init && is_from_proc_macro(cx, init) {
|
||||
let underscore_span = ty.span.with_lo(local.pat.span.hi());
|
||||
let snippet = snippet(cx, underscore_span, ": _");
|
||||
|
||||
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
|
||||
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
|
||||
if !snippet.trim().starts_with(':') && !snippet.trim().ends_with('_') {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -41,7 +47,7 @@ fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>
|
||||
LET_WITH_TYPE_UNDERSCORE,
|
||||
local.span,
|
||||
"variable declared with type underscore",
|
||||
Some(ty.span.with_lo(local.pat.span.hi())),
|
||||
Some(underscore_span),
|
||||
"remove the explicit type `_` declaration"
|
||||
)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
//@aux-build: proc_macros.rs
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::let_with_type_underscore)]
|
||||
#![allow(clippy::let_unit_value)]
|
||||
#![allow(clippy::let_unit_value, clippy::needless_late_init)]
|
||||
|
||||
extern crate proc_macros;
|
||||
|
||||
@ -9,20 +9,34 @@ fn func() -> &'static str {
|
||||
""
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
fn main() {
|
||||
// Will lint
|
||||
let x: _ = 1;
|
||||
let _: _ = 2;
|
||||
let x: _ = func();
|
||||
let x: _;
|
||||
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;
|
||||
|
||||
// do not lint from procedural macros
|
||||
// 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 = ();
|
||||
};
|
||||
}
|
||||
|
@ -1,39 +1,63 @@
|
||||
error: variable declared with type underscore
|
||||
--> $DIR/let_with_type_underscore.rs:14:5
|
||||
--> $DIR/let_with_type_underscore.rs:15:5
|
||||
|
|
||||
LL | let x: _ = 1;
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: remove the explicit type `_` declaration
|
||||
--> $DIR/let_with_type_underscore.rs:14:10
|
||||
--> $DIR/let_with_type_underscore.rs:15:10
|
||||
|
|
||||
LL | let x: _ = 1;
|
||||
| ^^^
|
||||
= note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
|
||||
|
||||
error: variable declared with type underscore
|
||||
--> $DIR/let_with_type_underscore.rs:15:5
|
||||
--> $DIR/let_with_type_underscore.rs:16:5
|
||||
|
|
||||
LL | let _: _ = 2;
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
help: remove the explicit type `_` declaration
|
||||
--> $DIR/let_with_type_underscore.rs:15:10
|
||||
--> $DIR/let_with_type_underscore.rs:16:10
|
||||
|
|
||||
LL | let _: _ = 2;
|
||||
| ^^^
|
||||
|
||||
error: variable declared with type underscore
|
||||
--> $DIR/let_with_type_underscore.rs:16:5
|
||||
--> $DIR/let_with_type_underscore.rs:17:5
|
||||
|
|
||||
LL | let x: _ = func();
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: remove the explicit type `_` declaration
|
||||
--> $DIR/let_with_type_underscore.rs:16:10
|
||||
--> $DIR/let_with_type_underscore.rs:17:10
|
||||
|
|
||||
LL | let x: _ = func();
|
||||
| ^^^
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error: variable declared with type underscore
|
||||
--> $DIR/let_with_type_underscore.rs:18:5
|
||||
|
|
||||
LL | let x: _;
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
help: remove the explicit type `_` declaration
|
||||
--> $DIR/let_with_type_underscore.rs:18:10
|
||||
|
|
||||
LL | let x: _;
|
||||
| ^^^
|
||||
|
||||
error: variable declared with type underscore
|
||||
--> $DIR/let_with_type_underscore.rs:25:5
|
||||
|
|
||||
LL | let x : _ = 1;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
help: remove the explicit type `_` declaration
|
||||
--> $DIR/let_with_type_underscore.rs:25:10
|
||||
|
|
||||
LL | let x : _ = 1;
|
||||
| ^^^^
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user