Skip to content

Commit e1607e9

Browse files
committed
Auto merge of #8912 - Alexendoo:needless-late-init-ice, r=giraffate
needless_late_init: fix ICE when all branches return the never type Fixes #8911 When the assignment is done in a match guard or the if condition and all of the branches return the never type `assignment_suggestions` would return an empty `Vec` which caused the ICE. It now returns `None` in that scenario Also moves some tests to the top of the file changelog: ICE Fixes: [`needless_late_init`] #8911
2 parents 588e198 + 722f7d2 commit e1607e9

File tree

4 files changed

+148
-117
lines changed

4 files changed

+148
-117
lines changed

clippy_lints/src/needless_late_init.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,15 @@ fn assignment_suggestions<'tcx>(
194194
}))
195195
.collect::<Option<Vec<(Span, String)>>>()?;
196196

197-
let applicability = if suggestions.len() > 1 {
197+
match suggestions.len() {
198+
// All of `exprs` are never types
199+
// https://github.com/rust-lang/rust-clippy/issues/8911
200+
0 => None,
201+
1 => Some((Applicability::MachineApplicable, suggestions)),
198202
// multiple suggestions don't work with rustfix in multipart_suggest
199203
// https://github.com/rust-lang/rustfix/issues/141
200-
Applicability::Unspecified
201-
} else {
202-
Applicability::MachineApplicable
203-
};
204-
Some((applicability, suggestions))
204+
_ => Some((Applicability::Unspecified, suggestions)),
205+
}
205206
}
206207

207208
struct Usage<'tcx> {

tests/ui/needless_late_init.fixed

+31-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(
44
unused,
55
clippy::assign_op_pattern,
6+
clippy::blocks_in_if_conditions,
67
clippy::let_and_return,
78
clippy::let_unit_value,
89
clippy::nonminimal_bool
@@ -18,6 +19,22 @@ impl std::ops::Drop for SignificantDrop {
1819
}
1920
}
2021

22+
fn simple() {
23+
24+
let a = "zero";
25+
26+
27+
28+
let b = 1;
29+
let c = 2;
30+
31+
32+
let d: usize = 1;
33+
34+
35+
let e = format!("{}", d);
36+
}
37+
2138
fn main() {
2239

2340
let n = 1;
@@ -237,22 +254,20 @@ fn does_not_lint() {
237254
x = SignificantDrop;
238255
}
239256

240-
mod fixable {
241-
#![allow(dead_code)]
242-
243-
fn main() {
244-
245-
let a = "zero";
246-
247-
248-
249-
let b = 1;
250-
let c = 2;
251-
252-
253-
let d: usize = 1;
257+
#[rustfmt::skip]
258+
fn issue8911() -> u32 {
259+
let x;
260+
match 1 {
261+
_ if { x = 1; false } => return 1,
262+
_ => return 2,
263+
}
254264

255-
256-
let e = format!("{}", d);
265+
let x;
266+
if { x = 1; true } {
267+
return 1;
268+
} else {
269+
return 2;
257270
}
271+
272+
3
258273
}

tests/ui/needless_late_init.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(
44
unused,
55
clippy::assign_op_pattern,
6+
clippy::blocks_in_if_conditions,
67
clippy::let_and_return,
78
clippy::let_unit_value,
89
clippy::nonminimal_bool
@@ -18,6 +19,22 @@ impl std::ops::Drop for SignificantDrop {
1819
}
1920
}
2021

22+
fn simple() {
23+
let a;
24+
a = "zero";
25+
26+
let b;
27+
let c;
28+
b = 1;
29+
c = 2;
30+
31+
let d: usize;
32+
d = 1;
33+
34+
let e;
35+
e = format!("{}", d);
36+
}
37+
2138
fn main() {
2239
let a;
2340
let n = 1;
@@ -237,22 +254,20 @@ fn does_not_lint() {
237254
x = SignificantDrop;
238255
}
239256

240-
mod fixable {
241-
#![allow(dead_code)]
242-
243-
fn main() {
244-
let a;
245-
a = "zero";
246-
247-
let b;
248-
let c;
249-
b = 1;
250-
c = 2;
251-
252-
let d: usize;
253-
d = 1;
257+
#[rustfmt::skip]
258+
fn issue8911() -> u32 {
259+
let x;
260+
match 1 {
261+
_ if { x = 1; false } => return 1,
262+
_ => return 2,
263+
}
254264

255-
let e;
256-
e = format!("{}", d);
265+
let x;
266+
if { x = 1; true } {
267+
return 1;
268+
} else {
269+
return 2;
257270
}
271+
272+
3
258273
}

0 commit comments

Comments
 (0)