Skip to content

Commit 2bb3d0c

Browse files
refactor(elidable_lifetimes): harmonize suggestion-building logic with extra_unused_type_parameters (rust-lang#15908)
It's nice to come up with a clever algorithm (rust-lang/rust-clippy#15667), but it's even nicer to reuse logic that already exists elsewhere. changelog: none r? @samueltardieu as you reviewed the original PR
2 parents fd20839 + 17f1942 commit 2bb3d0c

File tree

2 files changed

+40
-75
lines changed

2 files changed

+40
-75
lines changed

clippy_lints/src/extra_unused_type_parameters.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ impl<'cx, 'tcx> TypeWalker<'cx, 'tcx> {
157157
let spans = if explicit_params.len() == extra_params.len() {
158158
vec![self.generics.span] // Remove the entire list of generics
159159
} else {
160+
// 1. Start from the last extra param
161+
// 2. While the params preceding it are also extra, construct spans going from the current param to
162+
// the comma before it
163+
// 3. Once this chain of extra params stops, switch to constructing spans going from the current
164+
// param to the comma _after_ it
160165
let mut end: Option<LocalDefId> = None;
161166
extra_params
162167
.iter()

clippy_lints/src/lifetimes.rs

Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -856,89 +856,49 @@ fn elision_suggestions(
856856
.filter(|param| !param.is_elided_lifetime() && !param.is_impl_trait())
857857
.collect::<Vec<_>>();
858858

859-
if !elidable_lts
860-
.iter()
861-
.all(|lt| explicit_params.iter().any(|param| param.def_id == *lt))
862-
{
863-
return None;
864-
}
865-
866-
let mut suggestions = if elidable_lts.is_empty() {
867-
vec![]
868-
} else if elidable_lts.len() == explicit_params.len() {
859+
let mut suggestions = if elidable_lts.len() == explicit_params.len() {
869860
// if all the params are elided remove the whole generic block
870861
//
871862
// fn x<'a>() {}
872863
// ^^^^
873864
vec![(generics.span, String::new())]
874865
} else {
875-
match &explicit_params[..] {
876-
// no params, nothing to elide
877-
[] => unreachable!("handled by `elidable_lts.is_empty()`"),
878-
[param] => {
879-
if elidable_lts.contains(&param.def_id) {
880-
unreachable!("handled by `elidable_lts.len() == explicit_params.len()`")
866+
// 1. Start from the last elidable lifetime
867+
// 2. While the lifetimes preceding it are also elidable, construct spans going from the current
868+
// lifetime to the comma before it
869+
// 3. Once this chain of elidable lifetimes stops, switch to constructing spans going from the
870+
// current lifetime to the comma _after_ it
871+
let mut end: Option<LocalDefId> = None;
872+
elidable_lts
873+
.iter()
874+
.rev()
875+
.map(|&id| {
876+
let (idx, param) = explicit_params.iter().find_position(|param| param.def_id == id)?;
877+
878+
let span = if let Some(next) = explicit_params.get(idx + 1)
879+
&& end != Some(next.def_id)
880+
{
881+
// Extend the current span forward, up until the next param in the list.
882+
// fn x<'prev, 'a, 'next>() {}
883+
// ^^^^
884+
param.span.until(next.span)
881885
} else {
882-
unreachable!("handled by `elidable_lts.is_empty()`")
883-
}
884-
},
885-
[_, _, ..] => {
886-
// Given a list like `<'a, 'b, 'c, 'd, ..>`,
887-
//
888-
// If there is a cluster of elidable lifetimes at the beginning, say `'a` and `'b`, we should
889-
// suggest removing them _and_ the trailing comma. The span for that is `a.span.until(c.span)`:
890-
// <'a, 'b, 'c, 'd, ..> => <'a, 'b, 'c, 'd, ..>
891-
// ^^ ^^ ^^^^^^^^
892-
//
893-
// And since we know that `'c` isn't elidable--otherwise it would've been in the cluster--we can go
894-
// over all the lifetimes after it, and for each elidable one, add a suggestion spanning the
895-
// lifetime itself and the comma before, because each individual suggestion is guaranteed to leave
896-
// the list valid:
897-
// <.., 'c, 'd, 'e, 'f, 'g, ..> => <.., 'c, 'd, 'e, 'f, 'g, ..>
898-
// ^^ ^^ ^^ ^^^^ ^^^^^^^^
899-
//
900-
// In case there is no such starting cluster, we only need to do the second part of the algorithm:
901-
// <'a, 'b, 'c, 'd, 'e, 'f, 'g, ..> => <'a, 'b , 'c, 'd, 'e, 'f, 'g, ..>
902-
// ^^ ^^ ^^ ^^ ^^^^^^^^^ ^^^^^^^^
903-
904-
// Split off the starting cluster
905-
// TODO: use `slice::split_once` once stabilized (github.com/rust-lang/rust/issues/112811):
906-
// ```
907-
// let Some(split) = explicit_params.split_once(|param| !elidable_lts.contains(&param.def_id)) else {
908-
// // there were no lifetime param that couldn't be elided
909-
// unreachable!("handled by `elidable_lts.len() == explicit_params.len()`")
910-
// };
911-
// match split { /* .. */ }
912-
// ```
913-
let Some(split_pos) = explicit_params
914-
.iter()
915-
.position(|param| !elidable_lts.contains(&param.def_id))
916-
else {
917-
// there were no lifetime param that couldn't be elided
918-
unreachable!("handled by `elidable_lts.len() == explicit_params.len()`")
886+
// Extend the current span back to include the comma following the previous
887+
// param. If the span of the next param in the list has already been
888+
// extended, we continue the chain. This is why we're iterating in reverse.
889+
end = Some(param.def_id);
890+
891+
// `idx` will never be 0, else we'd be removing the entire list of generics
892+
let prev = explicit_params.get(idx - 1)?;
893+
894+
// fn x<'prev, 'a>() {}
895+
// ^^^^
896+
param.span.with_lo(prev.span.hi())
919897
};
920-
let split = explicit_params
921-
.split_at_checked(split_pos)
922-
.expect("got `split_pos` from `position` on the same Vec");
923-
924-
match split {
925-
([..], []) => unreachable!("handled by `elidable_lts.len() == explicit_params.len()`"),
926-
([], [_]) => unreachable!("handled by `explicit_params.len() == 1`"),
927-
(cluster, rest @ [rest_first, ..]) => {
928-
// the span for the cluster
929-
(cluster.first().map(|fw| fw.span.until(rest_first.span)).into_iter())
930-
// the span for the remaining lifetimes (calculations independent of the cluster)
931-
.chain(
932-
rest.array_windows()
933-
.filter(|[_, curr]| elidable_lts.contains(&curr.def_id))
934-
.map(|[prev, curr]| curr.span.with_lo(prev.span.hi())),
935-
)
936-
.map(|sp| (sp, String::new()))
937-
.collect()
938-
},
939-
}
940-
},
941-
}
898+
899+
Some((span, String::new()))
900+
})
901+
.collect::<Option<Vec<_>>>()?
942902
};
943903

944904
suggestions.extend(usages.iter().map(|&usage| {

0 commit comments

Comments
 (0)