Add async/const fn tests for needless-late-init

+nits
This commit is contained in:
Alex Macleod 2021-11-26 14:16:48 +00:00
parent 3957244120
commit d346ec94fe
6 changed files with 102 additions and 62 deletions

View file

@ -11,11 +11,11 @@ use rustc_span::Span;
declare_clippy_lint! {
/// ### What it does
/// Checks for late initializations that can be replaced by a let statement
/// Checks for late initializations that can be replaced by a `let` statement
/// with an initializer.
///
/// ### Why is this bad?
/// Assigning in the let statement is less repetitive.
/// Assigning in the `let` statement is less repetitive.
///
/// ### Example
/// ```rust
@ -55,7 +55,7 @@ declare_clippy_lint! {
#[clippy::version = "1.58.0"]
pub NEEDLESS_LATE_INIT,
style,
"late initializations that can be replaced by a let statement with an initializer"
"late initializations that can be replaced by a `let` statement with an initializer"
}
declare_lint_pass!(NeedlessLateInit => [NEEDLESS_LATE_INIT]);
@ -275,7 +275,7 @@ fn check<'tcx>(
if usage.needs_semi {
diag.span_suggestion(
usage.stmt.span.shrink_to_hi(),
"add a semicolon after the if expression",
"add a semicolon after the `if` expression",
";".to_string(),
applicability,
);
@ -301,12 +301,16 @@ fn check<'tcx>(
applicability,
);
diag.multipart_suggestion("remove the assignments from the match arms", suggestions, applicability);
diag.multipart_suggestion(
"remove the assignments from the `match` arms",
suggestions,
applicability,
);
if usage.needs_semi {
diag.span_suggestion(
usage.stmt.span.shrink_to_hi(),
"add a semicolon after the match expression",
"add a semicolon after the `match` expression",
";".to_string(),
applicability,
);

View file

@ -3,7 +3,8 @@
unused_assignments,
clippy::similar_names,
clippy::blacklisted_name,
clippy::branches_sharing_code
clippy::branches_sharing_code,
clippy::needless_late_init
)]
#![warn(clippy::useless_let_if_seq)]

View file

@ -1,25 +1,5 @@
error: unneeded late initalization
--> $DIR/let_if_seq.rs:48:5
|
LL | let foo;
| ^^^^^^^^
|
= note: `-D clippy::needless-late-init` implied by `-D warnings`
help: declare `foo` here
|
LL | let foo = if f() {
| +++++++++
help: remove the assignments from the branches
|
LL | 0
|
help: add a semicolon after the if expression
|
LL | };
| +
error: `if _ { .. } else { .. }` is an expression
--> $DIR/let_if_seq.rs:65:5
--> $DIR/let_if_seq.rs:66:5
|
LL | / let mut foo = 0;
LL | | if f() {
@ -31,7 +11,7 @@ LL | | }
= note: you might not need `mut` at all
error: `if _ { .. } else { .. }` is an expression
--> $DIR/let_if_seq.rs:70:5
--> $DIR/let_if_seq.rs:71:5
|
LL | / let mut bar = 0;
LL | | if f() {
@ -45,7 +25,7 @@ LL | | }
= note: you might not need `mut` at all
error: `if _ { .. } else { .. }` is an expression
--> $DIR/let_if_seq.rs:78:5
--> $DIR/let_if_seq.rs:79:5
|
LL | / let quz;
LL | | if f() {
@ -56,7 +36,7 @@ LL | | }
| |_____^ help: it is more idiomatic to write: `let quz = if f() { 42 } else { 0 };`
error: `if _ { .. } else { .. }` is an expression
--> $DIR/let_if_seq.rs:107:5
--> $DIR/let_if_seq.rs:108:5
|
LL | / let mut baz = 0;
LL | | if f() {
@ -66,26 +46,5 @@ LL | | }
|
= note: you might not need `mut` at all
error: unneeded late initalization
--> $DIR/let_if_seq.rs:78:5
|
LL | let quz;
| ^^^^^^^^
|
help: declare `quz` here
|
LL | let quz = if f() {
| +++++++++
help: remove the assignments from the branches
|
LL ~ 42
LL | } else {
LL ~ 0
|
help: add a semicolon after the if expression
|
LL | };
| +
error: aborting due to 6 previous errors
error: aborting due to 4 previous errors

View file

@ -42,6 +42,40 @@ fn main() {
println!("{}", a);
}
async fn in_async() -> &'static str {
async fn f() -> &'static str {
"one"
}
let a;
let n = 1;
match n {
1 => a = f().await,
_ => {
a = "two";
},
}
a
}
const fn in_const() -> &'static str {
const fn f() -> &'static str {
"one"
}
let a;
let n = 1;
match n {
1 => a = f(),
_ => {
a = "two";
},
}
a
}
fn does_not_lint() {
let z;
if false {

View file

@ -9,13 +9,13 @@ help: declare `a` here
|
LL | let a = match n {
| +++++++
help: remove the assignments from the match arms
help: remove the assignments from the `match` arms
|
LL ~ 1 => "one",
LL | _ => {
LL ~ "two"
|
help: add a semicolon after the match expression
help: add a semicolon after the `match` expression
|
LL | };
| +
@ -36,7 +36,7 @@ LL ~ "four"
LL | } else {
LL ~ "five"
|
help: add a semicolon after the if expression
help: add a semicolon after the `if` expression
|
LL | };
| +
@ -57,7 +57,7 @@ LL ~ n
LL | } else {
LL ~ -50
|
help: add a semicolon after the if expression
help: add a semicolon after the `if` expression
|
LL | };
| +
@ -78,7 +78,7 @@ LL ~ temp
LL | } else {
LL ~ 15
|
help: add a semicolon after the if expression
help: add a semicolon after the `if` expression
|
LL | };
| +
@ -99,10 +99,52 @@ LL ~ format!("{} {}", a, b)
LL | } else {
LL ~ format!("{}", c)
|
help: add a semicolon after the if expression
help: add a semicolon after the `if` expression
|
LL | };
| +
error: aborting due to 5 previous errors
error: unneeded late initalization
--> $DIR/needless_late_init.rs:50:5
|
LL | let a;
| ^^^^^^
|
help: declare `a` here
|
LL | let a = match n {
| +++++++
help: remove the assignments from the `match` arms
|
LL ~ 1 => f().await,
LL | _ => {
LL ~ "two"
|
help: add a semicolon after the `match` expression
|
LL | };
| +
error: unneeded late initalization
--> $DIR/needless_late_init.rs:67:5
|
LL | let a;
| ^^^^^^
|
help: declare `a` here
|
LL | let a = match n {
| +++++++
help: remove the assignments from the `match` arms
|
LL ~ 1 => f(),
LL | _ => {
LL ~ "two"
|
help: add a semicolon after the `match` expression
|
LL | };
| +
error: aborting due to 7 previous errors

View file

@ -64,7 +64,7 @@ help: declare `f` here
|
LL | let f = match 1 {
| +++++++
help: remove the assignments from the match arms
help: remove the assignments from the `match` arms
|
LL | 1 => "three",
| ~~~~~~~
@ -83,7 +83,7 @@ help: remove the assignments from the branches
|
LL | 5
|
help: add a semicolon after the if expression
help: add a semicolon after the `if` expression
|
LL | };
| +