Skip to content

Commit

Permalink
Auto merge of rust-lang#8912 - Alexendoo:needless-late-init-ice, r=gi…
Browse files Browse the repository at this point in the history
…raffate

needless_late_init: fix ICE when all branches return the never type

Fixes rust-lang#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`] rust-lang#8911
  • Loading branch information
bors committed May 31, 2022
2 parents 588e198 + 722f7d2 commit e1607e9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 117 deletions.
13 changes: 7 additions & 6 deletions clippy_lints/src/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,15 @@ fn assignment_suggestions<'tcx>(
}))
.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
// https://github.com/rust-lang/rustfix/issues/141
Applicability::Unspecified
} else {
Applicability::MachineApplicable
};
Some((applicability, suggestions))
_ => Some((Applicability::Unspecified, suggestions)),
}
}

struct Usage<'tcx> {
Expand Down
47 changes: 31 additions & 16 deletions tests/ui/needless_late_init.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(
unused,
clippy::assign_op_pattern,
clippy::blocks_in_if_conditions,
clippy::let_and_return,
clippy::let_unit_value,
clippy::nonminimal_bool
Expand All @@ -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() {

let n = 1;
Expand Down Expand Up @@ -237,22 +254,20 @@ fn does_not_lint() {
x = SignificantDrop;
}

mod fixable {
#![allow(dead_code)]

fn main() {

let a = "zero";



let b = 1;
let c = 2;


let d: usize = 1;
#[rustfmt::skip]
fn issue8911() -> u32 {
let x;
match 1 {
_ if { x = 1; false } => return 1,
_ => return 2,
}


let e = format!("{}", d);
let x;
if { x = 1; true } {
return 1;
} else {
return 2;
}

3
}
47 changes: 31 additions & 16 deletions tests/ui/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(
unused,
clippy::assign_op_pattern,
clippy::blocks_in_if_conditions,
clippy::let_and_return,
clippy::let_unit_value,
clippy::nonminimal_bool
Expand All @@ -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() {
let a;
let n = 1;
Expand Down Expand Up @@ -237,22 +254,20 @@ fn does_not_lint() {
x = SignificantDrop;
}

mod fixable {
#![allow(dead_code)]

fn main() {
let a;
a = "zero";

let b;
let c;
b = 1;
c = 2;

let d: usize;
d = 1;
#[rustfmt::skip]
fn issue8911() -> u32 {
let x;
match 1 {
_ if { x = 1; false } => return 1,
_ => return 2,
}

let e;
e = format!("{}", d);
let x;
if { x = 1; true } {
return 1;
} else {
return 2;
}

3
}
Loading

0 comments on commit e1607e9

Please sign in to comment.