Skip to content

Commit

Permalink
Add async/const fn tests for needless-late-init
Browse files Browse the repository at this point in the history
+nits
  • Loading branch information
Alexendoo committed Nov 26, 2021
1 parent 3957244 commit d346ec9
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 62 deletions.
16 changes: 10 additions & 6 deletions clippy_lints/src/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/let_if_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down
51 changes: 5 additions & 46 deletions tests/ui/let_if_seq.stderr
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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

34 changes: 34 additions & 0 deletions tests/ui/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
56 changes: 49 additions & 7 deletions tests/ui/needless_late_init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 | };
| +
Expand All @@ -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 | };
| +
Expand All @@ -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 | };
| +
Expand All @@ -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 | };
| +
Expand All @@ -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

4 changes: 2 additions & 2 deletions tests/ui/needless_late_init_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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",
| ~~~~~~~
Expand All @@ -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 | };
| +
Expand Down

0 comments on commit d346ec9

Please sign in to comment.