From 94e321a6ff9a224f690b355fd4298caf6342e883 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 5 Jun 2022 07:14:31 +0200 Subject: [PATCH 1/3] needless_late_init refactoring Remove duplication in creating suggestions by first mapping assignments to spans and then suggestions. --- clippy_lints/src/needless_late_init.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/needless_late_init.rs b/clippy_lints/src/needless_late_init.rs index 26c694a71..9c4e2ef67 100644 --- a/clippy_lints/src/needless_late_init.rs +++ b/clippy_lints/src/needless_late_init.rs @@ -185,13 +185,13 @@ fn assignment_suggestions<'tcx>( let suggestions = assignments .iter() - .map(|assignment| Some((assignment.span.until(assignment.rhs_span), String::new()))) - .chain(assignments.iter().map(|assignment| { - Some(( - assignment.rhs_span.shrink_to_hi().with_hi(assignment.span.hi()), - String::new(), - )) - })) + .map(|assignment| assignment.span.until(assignment.rhs_span)) + .chain( + assignments + .iter() + .map(|assignment| assignment.rhs_span.shrink_to_hi().with_hi(assignment.span.hi())), + ) + .map(|span| Some((span, String::new()))) .collect::>>()?; match suggestions.len() { From 2a1a80d80cdc75ed0bb9f35744591089b8a89f31 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 5 Jun 2022 07:22:45 +0200 Subject: [PATCH 2/3] needless_late_init refactoring Simplify the creation of suggestions by using `flat_map` instead of `chain`. Note that the order of the suggestions is not important. --- clippy_lints/src/needless_late_init.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/needless_late_init.rs b/clippy_lints/src/needless_late_init.rs index 9c4e2ef67..5e97c606e 100644 --- a/clippy_lints/src/needless_late_init.rs +++ b/clippy_lints/src/needless_late_init.rs @@ -185,12 +185,12 @@ fn assignment_suggestions<'tcx>( let suggestions = assignments .iter() - .map(|assignment| assignment.span.until(assignment.rhs_span)) - .chain( - assignments - .iter() - .map(|assignment| assignment.rhs_span.shrink_to_hi().with_hi(assignment.span.hi())), - ) + .flat_map(|assignment| { + [ + assignment.span.until(assignment.rhs_span), + assignment.rhs_span.shrink_to_hi().with_hi(assignment.span.hi()), + ] + }) .map(|span| Some((span, String::new()))) .collect::>>()?; From a2de34720d984669499e981b640cde050c2a4dfa Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 5 Jun 2022 07:28:28 +0200 Subject: [PATCH 3/3] needless_late_init refactoring Remove the unneeded wrapping and unwrapping in suggestion creation. Collecting to Option> only returns None if one of the elements is None and that is never the case here. --- clippy_lints/src/needless_late_init.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/needless_late_init.rs b/clippy_lints/src/needless_late_init.rs index 5e97c606e..4154c71b4 100644 --- a/clippy_lints/src/needless_late_init.rs +++ b/clippy_lints/src/needless_late_init.rs @@ -191,8 +191,8 @@ fn assignment_suggestions<'tcx>( assignment.rhs_span.shrink_to_hi().with_hi(assignment.span.hi()), ] }) - .map(|span| Some((span, String::new()))) - .collect::>>()?; + .map(|span| (span, String::new())) + .collect::>(); match suggestions.len() { // All of `exprs` are never types