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
This commit is contained in:
bors 2022-05-31 00:46:52 +00:00
commit e1607e9d31
4 changed files with 150 additions and 119 deletions

View File

@ -194,14 +194,15 @@ fn assignment_suggestions<'tcx>(
})) }))
.collect::<Option<Vec<(Span, String)>>>()?; .collect::<Option<Vec<(Span, String)>>>()?;
let applicability = if suggestions.len() > 1 { match suggestions.len() {
// All of `exprs` are never types
// https://github.com/rust-lang/rust-clippy/issues/8911
0 => None,
1 => Some((Applicability::MachineApplicable, suggestions)),
// multiple suggestions don't work with rustfix in multipart_suggest // multiple suggestions don't work with rustfix in multipart_suggest
// https://github.com/rust-lang/rustfix/issues/141 // https://github.com/rust-lang/rustfix/issues/141
Applicability::Unspecified _ => Some((Applicability::Unspecified, suggestions)),
} else { }
Applicability::MachineApplicable
};
Some((applicability, suggestions))
} }
struct Usage<'tcx> { struct Usage<'tcx> {

View File

@ -3,6 +3,7 @@
#![allow( #![allow(
unused, unused,
clippy::assign_op_pattern, clippy::assign_op_pattern,
clippy::blocks_in_if_conditions,
clippy::let_and_return, clippy::let_and_return,
clippy::let_unit_value, clippy::let_unit_value,
clippy::nonminimal_bool clippy::nonminimal_bool
@ -18,6 +19,22 @@ impl std::ops::Drop for SignificantDrop {
} }
} }
fn simple() {
let a = "zero";
let b = 1;
let c = 2;
let d: usize = 1;
let e = format!("{}", d);
}
fn main() { fn main() {
let n = 1; let n = 1;
@ -237,22 +254,20 @@ fn does_not_lint() {
x = SignificantDrop; x = SignificantDrop;
} }
mod fixable { #[rustfmt::skip]
#![allow(dead_code)] fn issue8911() -> u32 {
let x;
fn main() { match 1 {
_ if { x = 1; false } => return 1,
let a = "zero"; _ => return 2,
let b = 1;
let c = 2;
let d: usize = 1;
let e = format!("{}", d);
} }
let x;
if { x = 1; true } {
return 1;
} else {
return 2;
}
3
} }

View File

@ -3,6 +3,7 @@
#![allow( #![allow(
unused, unused,
clippy::assign_op_pattern, clippy::assign_op_pattern,
clippy::blocks_in_if_conditions,
clippy::let_and_return, clippy::let_and_return,
clippy::let_unit_value, clippy::let_unit_value,
clippy::nonminimal_bool clippy::nonminimal_bool
@ -18,6 +19,22 @@ impl std::ops::Drop for SignificantDrop {
} }
} }
fn simple() {
let a;
a = "zero";
let b;
let c;
b = 1;
c = 2;
let d: usize;
d = 1;
let e;
e = format!("{}", d);
}
fn main() { fn main() {
let a; let a;
let n = 1; let n = 1;
@ -237,22 +254,20 @@ fn does_not_lint() {
x = SignificantDrop; x = SignificantDrop;
} }
mod fixable { #[rustfmt::skip]
#![allow(dead_code)] fn issue8911() -> u32 {
let x;
fn main() { match 1 {
let a; _ if { x = 1; false } => return 1,
a = "zero"; _ => return 2,
let b;
let c;
b = 1;
c = 2;
let d: usize;
d = 1;
let e;
e = format!("{}", d);
} }
let x;
if { x = 1; true } {
return 1;
} else {
return 2;
}
3
} }

View File

@ -1,10 +1,77 @@
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:22:5 --> $DIR/needless_late_init.rs:23:5
|
LL | let a;
| ^^^^^^ created here
LL | a = "zero";
| ^^^^^^^^^^ initialised here
|
= note: `-D clippy::needless-late-init` implied by `-D warnings`
help: declare `a` here
|
LL | let a = "zero";
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:26:5
|
LL | let b;
| ^^^^^^ created here
LL | let c;
LL | b = 1;
| ^^^^^ initialised here
|
help: declare `b` here
|
LL | let b = 1;
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:27:5
|
LL | let c;
| ^^^^^^ created here
LL | b = 1;
LL | c = 2;
| ^^^^^ initialised here
|
help: declare `c` here
|
LL | let c = 2;
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:31:5
|
LL | let d: usize;
| ^^^^^^^^^^^^^ created here
LL | d = 1;
| ^^^^^ initialised here
|
help: declare `d` here
|
LL | let d: usize = 1;
| ~~~~~~~~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:34:5
|
LL | let e;
| ^^^^^^ created here
LL | e = format!("{}", d);
| ^^^^^^^^^^^^^^^^^^^^ initialised here
|
help: declare `e` here
|
LL | let e = format!("{}", d);
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:39:5
| |
LL | let a; LL | let a;
| ^^^^^^ | ^^^^^^
| |
= note: `-D clippy::needless-late-init` implied by `-D warnings`
help: declare `a` here help: declare `a` here
| |
LL | let a = match n { LL | let a = match n {
@ -21,7 +88,7 @@ LL | };
| + | +
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:31:5 --> $DIR/needless_late_init.rs:48:5
| |
LL | let b; LL | let b;
| ^^^^^^ | ^^^^^^
@ -42,7 +109,7 @@ LL | };
| + | +
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:38:5 --> $DIR/needless_late_init.rs:55:5
| |
LL | let d; LL | let d;
| ^^^^^^ | ^^^^^^
@ -63,7 +130,7 @@ LL | };
| + | +
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:46:5 --> $DIR/needless_late_init.rs:63:5
| |
LL | let e; LL | let e;
| ^^^^^^ | ^^^^^^
@ -84,7 +151,7 @@ LL | };
| + | +
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:53:5 --> $DIR/needless_late_init.rs:70:5
| |
LL | let f; LL | let f;
| ^^^^^^ | ^^^^^^
@ -100,7 +167,7 @@ LL + 1 => "three",
| |
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:59:5 --> $DIR/needless_late_init.rs:76:5
| |
LL | let g: usize; LL | let g: usize;
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
@ -120,7 +187,7 @@ LL | };
| + | +
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:67:5 --> $DIR/needless_late_init.rs:84:5
| |
LL | let x; LL | let x;
| ^^^^^^ created here | ^^^^^^ created here
@ -134,7 +201,7 @@ LL | let x = 1;
| ~~~~~ | ~~~~~
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:71:5 --> $DIR/needless_late_init.rs:88:5
| |
LL | let x; LL | let x;
| ^^^^^^ created here | ^^^^^^ created here
@ -148,7 +215,7 @@ LL | let x = SignificantDrop;
| ~~~~~ | ~~~~~
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:75:5 --> $DIR/needless_late_init.rs:92:5
| |
LL | let x; LL | let x;
| ^^^^^^ created here | ^^^^^^ created here
@ -162,7 +229,7 @@ LL | let x = SignificantDrop;
| ~~~~~ | ~~~~~
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:94:5 --> $DIR/needless_late_init.rs:111:5
| |
LL | let a; LL | let a;
| ^^^^^^ | ^^^^^^
@ -183,7 +250,7 @@ LL | };
| + | +
error: unneeded late initialization error: unneeded late initialization
--> $DIR/needless_late_init.rs:111:5 --> $DIR/needless_late_init.rs:128:5
| |
LL | let a; LL | let a;
| ^^^^^^ | ^^^^^^
@ -203,72 +270,5 @@ help: add a semicolon after the `match` expression
LL | }; LL | };
| + | +
error: unneeded late initialization
--> $DIR/needless_late_init.rs:244:9
|
LL | let a;
| ^^^^^^ created here
LL | a = "zero";
| ^^^^^^^^^^ initialised here
|
help: declare `a` here
|
LL | let a = "zero";
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:247:9
|
LL | let b;
| ^^^^^^ created here
LL | let c;
LL | b = 1;
| ^^^^^ initialised here
|
help: declare `b` here
|
LL | let b = 1;
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:248:9
|
LL | let c;
| ^^^^^^ created here
LL | b = 1;
LL | c = 2;
| ^^^^^ initialised here
|
help: declare `c` here
|
LL | let c = 2;
| ~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:252:9
|
LL | let d: usize;
| ^^^^^^^^^^^^^ created here
LL | d = 1;
| ^^^^^ initialised here
|
help: declare `d` here
|
LL | let d: usize = 1;
| ~~~~~~~~~~~~
error: unneeded late initialization
--> $DIR/needless_late_init.rs:255:9
|
LL | let e;
| ^^^^^^ created here
LL | e = format!("{}", d);
| ^^^^^^^^^^^^^^^^^^^^ initialised here
|
help: declare `e` here
|
LL | let e = format!("{}", d);
| ~~~~~
error: aborting due to 16 previous errors error: aborting due to 16 previous errors