Make let_with_type_underscore help message into a suggestion

This commit is contained in:
Alex Macleod 2025-05-07 15:16:32 +00:00
parent cf6bebb343
commit 8c045221b5
3 changed files with 75 additions and 21 deletions

View 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 = ();
};
}

View file

@ -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