Skip to content

Commit d346ec9

Browse files
committed
Add async/const fn tests for needless-late-init
+nits
1 parent 3957244 commit d346ec9

6 files changed

+102
-62
lines changed

clippy_lints/src/needless_late_init.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use rustc_span::Span;
1111

1212
declare_clippy_lint! {
1313
/// ### What it does
14-
/// Checks for late initializations that can be replaced by a let statement
14+
/// Checks for late initializations that can be replaced by a `let` statement
1515
/// with an initializer.
1616
///
1717
/// ### Why is this bad?
18-
/// Assigning in the let statement is less repetitive.
18+
/// Assigning in the `let` statement is less repetitive.
1919
///
2020
/// ### Example
2121
/// ```rust
@@ -55,7 +55,7 @@ declare_clippy_lint! {
5555
#[clippy::version = "1.58.0"]
5656
pub NEEDLESS_LATE_INIT,
5757
style,
58-
"late initializations that can be replaced by a let statement with an initializer"
58+
"late initializations that can be replaced by a `let` statement with an initializer"
5959
}
6060
declare_lint_pass!(NeedlessLateInit => [NEEDLESS_LATE_INIT]);
6161

@@ -275,7 +275,7 @@ fn check<'tcx>(
275275
if usage.needs_semi {
276276
diag.span_suggestion(
277277
usage.stmt.span.shrink_to_hi(),
278-
"add a semicolon after the if expression",
278+
"add a semicolon after the `if` expression",
279279
";".to_string(),
280280
applicability,
281281
);
@@ -301,12 +301,16 @@ fn check<'tcx>(
301301
applicability,
302302
);
303303

304-
diag.multipart_suggestion("remove the assignments from the match arms", suggestions, applicability);
304+
diag.multipart_suggestion(
305+
"remove the assignments from the `match` arms",
306+
suggestions,
307+
applicability,
308+
);
305309

306310
if usage.needs_semi {
307311
diag.span_suggestion(
308312
usage.stmt.span.shrink_to_hi(),
309-
"add a semicolon after the match expression",
313+
"add a semicolon after the `match` expression",
310314
";".to_string(),
311315
applicability,
312316
);

tests/ui/let_if_seq.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
unused_assignments,
44
clippy::similar_names,
55
clippy::blacklisted_name,
6-
clippy::branches_sharing_code
6+
clippy::branches_sharing_code,
7+
clippy::needless_late_init
78
)]
89
#![warn(clippy::useless_let_if_seq)]
910

tests/ui/let_if_seq.stderr

+5-46
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
1-
error: unneeded late initalization
2-
--> $DIR/let_if_seq.rs:48:5
3-
|
4-
LL | let foo;
5-
| ^^^^^^^^
6-
|
7-
= note: `-D clippy::needless-late-init` implied by `-D warnings`
8-
help: declare `foo` here
9-
|
10-
LL | let foo = if f() {
11-
| +++++++++
12-
help: remove the assignments from the branches
13-
|
14-
LL | 0
15-
|
16-
help: add a semicolon after the if expression
17-
|
18-
LL | };
19-
| +
20-
211
error: `if _ { .. } else { .. }` is an expression
22-
--> $DIR/let_if_seq.rs:65:5
2+
--> $DIR/let_if_seq.rs:66:5
233
|
244
LL | / let mut foo = 0;
255
LL | | if f() {
@@ -31,7 +11,7 @@ LL | | }
3111
= note: you might not need `mut` at all
3212

3313
error: `if _ { .. } else { .. }` is an expression
34-
--> $DIR/let_if_seq.rs:70:5
14+
--> $DIR/let_if_seq.rs:71:5
3515
|
3616
LL | / let mut bar = 0;
3717
LL | | if f() {
@@ -45,7 +25,7 @@ LL | | }
4525
= note: you might not need `mut` at all
4626

4727
error: `if _ { .. } else { .. }` is an expression
48-
--> $DIR/let_if_seq.rs:78:5
28+
--> $DIR/let_if_seq.rs:79:5
4929
|
5030
LL | / let quz;
5131
LL | | if f() {
@@ -56,7 +36,7 @@ LL | | }
5636
| |_____^ help: it is more idiomatic to write: `let quz = if f() { 42 } else { 0 };`
5737

5838
error: `if _ { .. } else { .. }` is an expression
59-
--> $DIR/let_if_seq.rs:107:5
39+
--> $DIR/let_if_seq.rs:108:5
6040
|
6141
LL | / let mut baz = 0;
6242
LL | | if f() {
@@ -66,26 +46,5 @@ LL | | }
6646
|
6747
= note: you might not need `mut` at all
6848

69-
error: unneeded late initalization
70-
--> $DIR/let_if_seq.rs:78:5
71-
|
72-
LL | let quz;
73-
| ^^^^^^^^
74-
|
75-
help: declare `quz` here
76-
|
77-
LL | let quz = if f() {
78-
| +++++++++
79-
help: remove the assignments from the branches
80-
|
81-
LL ~ 42
82-
LL | } else {
83-
LL ~ 0
84-
|
85-
help: add a semicolon after the if expression
86-
|
87-
LL | };
88-
| +
89-
90-
error: aborting due to 6 previous errors
49+
error: aborting due to 4 previous errors
9150

tests/ui/needless_late_init.rs

+34
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ fn main() {
4242
println!("{}", a);
4343
}
4444

45+
async fn in_async() -> &'static str {
46+
async fn f() -> &'static str {
47+
"one"
48+
}
49+
50+
let a;
51+
let n = 1;
52+
match n {
53+
1 => a = f().await,
54+
_ => {
55+
a = "two";
56+
},
57+
}
58+
59+
a
60+
}
61+
62+
const fn in_const() -> &'static str {
63+
const fn f() -> &'static str {
64+
"one"
65+
}
66+
67+
let a;
68+
let n = 1;
69+
match n {
70+
1 => a = f(),
71+
_ => {
72+
a = "two";
73+
},
74+
}
75+
76+
a
77+
}
78+
4579
fn does_not_lint() {
4680
let z;
4781
if false {

tests/ui/needless_late_init.stderr

+49-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ help: declare `a` here
99
|
1010
LL | let a = match n {
1111
| +++++++
12-
help: remove the assignments from the match arms
12+
help: remove the assignments from the `match` arms
1313
|
1414
LL ~ 1 => "one",
1515
LL | _ => {
1616
LL ~ "two"
1717
|
18-
help: add a semicolon after the match expression
18+
help: add a semicolon after the `match` expression
1919
|
2020
LL | };
2121
| +
@@ -36,7 +36,7 @@ LL ~ "four"
3636
LL | } else {
3737
LL ~ "five"
3838
|
39-
help: add a semicolon after the if expression
39+
help: add a semicolon after the `if` expression
4040
|
4141
LL | };
4242
| +
@@ -57,7 +57,7 @@ LL ~ n
5757
LL | } else {
5858
LL ~ -50
5959
|
60-
help: add a semicolon after the if expression
60+
help: add a semicolon after the `if` expression
6161
|
6262
LL | };
6363
| +
@@ -78,7 +78,7 @@ LL ~ temp
7878
LL | } else {
7979
LL ~ 15
8080
|
81-
help: add a semicolon after the if expression
81+
help: add a semicolon after the `if` expression
8282
|
8383
LL | };
8484
| +
@@ -99,10 +99,52 @@ LL ~ format!("{} {}", a, b)
9999
LL | } else {
100100
LL ~ format!("{}", c)
101101
|
102-
help: add a semicolon after the if expression
102+
help: add a semicolon after the `if` expression
103103
|
104104
LL | };
105105
| +
106106

107-
error: aborting due to 5 previous errors
107+
error: unneeded late initalization
108+
--> $DIR/needless_late_init.rs:50:5
109+
|
110+
LL | let a;
111+
| ^^^^^^
112+
|
113+
help: declare `a` here
114+
|
115+
LL | let a = match n {
116+
| +++++++
117+
help: remove the assignments from the `match` arms
118+
|
119+
LL ~ 1 => f().await,
120+
LL | _ => {
121+
LL ~ "two"
122+
|
123+
help: add a semicolon after the `match` expression
124+
|
125+
LL | };
126+
| +
127+
128+
error: unneeded late initalization
129+
--> $DIR/needless_late_init.rs:67:5
130+
|
131+
LL | let a;
132+
| ^^^^^^
133+
|
134+
help: declare `a` here
135+
|
136+
LL | let a = match n {
137+
| +++++++
138+
help: remove the assignments from the `match` arms
139+
|
140+
LL ~ 1 => f(),
141+
LL | _ => {
142+
LL ~ "two"
143+
|
144+
help: add a semicolon after the `match` expression
145+
|
146+
LL | };
147+
| +
148+
149+
error: aborting due to 7 previous errors
108150

tests/ui/needless_late_init_fixable.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ help: declare `f` here
6464
|
6565
LL | let f = match 1 {
6666
| +++++++
67-
help: remove the assignments from the match arms
67+
help: remove the assignments from the `match` arms
6868
|
6969
LL | 1 => "three",
7070
| ~~~~~~~
@@ -83,7 +83,7 @@ help: remove the assignments from the branches
8383
|
8484
LL | 5
8585
|
86-
help: add a semicolon after the if expression
86+
help: add a semicolon after the `if` expression
8787
|
8888
LL | };
8989
| +

0 commit comments

Comments
 (0)