@@ -1821,21 +1821,19 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
1821
1821
crate fn add_missing_lifetime_specifiers_label (
1822
1822
& self ,
1823
1823
err : & mut DiagnosticBuilder < ' _ > ,
1824
- spans : Vec < Span > ,
1825
- counts : Vec < usize > ,
1824
+ spans_with_counts : Vec < ( Span , usize ) > ,
1826
1825
lifetime_names : & FxHashSet < Symbol > ,
1827
1826
lifetime_spans : Vec < Span > ,
1828
1827
params : & [ ElisionFailureInfo ] ,
1829
1828
) {
1830
- let snippets: Vec < Option < String > > = spans
1829
+ let snippets: Vec < Option < String > > = spans_with_counts
1831
1830
. iter ( )
1832
- . copied ( )
1833
- . map ( |span| self . tcx . sess . source_map ( ) . span_to_snippet ( span) . ok ( ) )
1831
+ . map ( |( span, _) | self . tcx . sess . source_map ( ) . span_to_snippet ( * span) . ok ( ) )
1834
1832
. collect ( ) ;
1835
1833
1836
- for ( span, count) in spans . iter ( ) . zip ( counts . iter ( ) ) {
1834
+ for ( span, count) in & spans_with_counts {
1837
1835
err. span_label (
1838
- span. clone ( ) ,
1836
+ * span,
1839
1837
format ! (
1840
1838
"expected {} lifetime parameter{}" ,
1841
1839
if * count == 1 { "named" . to_string( ) } else { count. to_string( ) } ,
@@ -1847,7 +1845,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
1847
1845
let suggest_existing =
1848
1846
|err : & mut DiagnosticBuilder < ' _ > ,
1849
1847
name : & str ,
1850
- formatters : & Vec < Option < Box < dyn Fn ( & str ) -> String > > > | {
1848
+ formatters : Vec < Option < Box < dyn Fn ( & str ) -> String > > > | {
1851
1849
if let Some ( MissingLifetimeSpot :: HigherRanked { span : for_span, span_type } ) =
1852
1850
self . missing_named_lifetime_spots . iter ( ) . rev ( ) . next ( )
1853
1851
{
@@ -1892,9 +1890,9 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
1892
1890
}
1893
1891
}
1894
1892
introduce_suggestion. push ( ( * for_span, for_sugg) ) ;
1895
- for ( span, formatter) in spans . iter ( ) . copied ( ) . zip ( formatters. iter ( ) ) {
1893
+ for ( ( span, _ ) , formatter) in spans_with_counts . iter ( ) . zip ( formatters. iter ( ) ) {
1896
1894
if let Some ( formatter) = formatter {
1897
- introduce_suggestion. push ( ( span, formatter ( & lt_name) ) ) ;
1895
+ introduce_suggestion. push ( ( * span, formatter ( & lt_name) ) ) ;
1898
1896
}
1899
1897
}
1900
1898
err. multipart_suggestion_with_style (
@@ -1905,12 +1903,12 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
1905
1903
) ;
1906
1904
}
1907
1905
1908
- let mut spans_suggs: Vec < _ > = Vec :: new ( ) ;
1909
- for ( span , fmt ) in spans . iter ( ) . copied ( ) . zip ( formatters . iter ( ) ) {
1910
- if let Some ( formatter ) = fmt {
1911
- spans_suggs . push ( ( span , formatter ( name ) ) ) ;
1912
- }
1913
- }
1906
+ let spans_suggs: Vec < _ > = formatters
1907
+ . into_iter ( )
1908
+ . filter_map ( | fmt| fmt )
1909
+ . zip ( spans_with_counts . iter ( ) )
1910
+ . map ( | ( formatter , ( span , _ ) ) | ( * span , formatter ( name ) ) )
1911
+ . collect ( ) ;
1914
1912
err. multipart_suggestion_with_style (
1915
1913
& format ! (
1916
1914
"consider using the `{}` lifetime" ,
@@ -1921,7 +1919,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
1921
1919
SuggestionStyle :: ShowAlways ,
1922
1920
) ;
1923
1921
} ;
1924
- let suggest_new = |err : & mut DiagnosticBuilder < ' _ > , suggs : & Vec < Option < String > > | {
1922
+ let suggest_new = |err : & mut DiagnosticBuilder < ' _ > , suggs : Vec < Option < String > > | {
1925
1923
for missing in self . missing_named_lifetime_spots . iter ( ) . rev ( ) {
1926
1924
let mut introduce_suggestion = vec ! [ ] ;
1927
1925
let msg;
@@ -1967,8 +1965,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
1967
1965
}
1968
1966
MissingLifetimeSpot :: Static => {
1969
1967
let mut spans_suggs = Vec :: new ( ) ;
1970
- for ( ( span, snippet ) , count ) in
1971
- spans . iter ( ) . copied ( ) . zip ( snippets. iter ( ) ) . zip ( counts . iter ( ) . copied ( ) )
1968
+ for ( ( span, count ) , snippet ) in
1969
+ spans_with_counts . iter ( ) . copied ( ) . zip ( snippets. iter ( ) )
1972
1970
{
1973
1971
let ( span, sugg) = match snippet. as_deref ( ) {
1974
1972
Some ( "&" ) => ( span. shrink_to_hi ( ) , "'static " . to_owned ( ) ) ,
@@ -2018,7 +2016,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
2018
2016
}
2019
2017
}
2020
2018
}
2021
- for ( span, sugg) in spans . iter ( ) . copied ( ) . zip ( suggs. iter ( ) ) {
2019
+ for ( ( span, _ ) , sugg) in spans_with_counts . iter ( ) . copied ( ) . zip ( suggs. iter ( ) ) {
2022
2020
if let Some ( sugg) = sugg {
2023
2021
introduce_suggestion. push ( ( span, sugg. to_string ( ) ) ) ;
2024
2022
}
@@ -2039,68 +2037,57 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
2039
2037
match & lifetime_names[ ..] {
2040
2038
[ name] => {
2041
2039
let mut suggs: Vec < Option < Box < dyn Fn ( & str ) -> String > > > = Vec :: new ( ) ;
2042
- for ( snippet, count) in snippets. iter ( ) . cloned ( ) . zip ( counts. iter ( ) . copied ( ) ) {
2043
- if snippet == Some ( "&" . to_string ( ) ) {
2044
- suggs. push ( Some ( Box :: new ( |name| format ! ( "&{} " , name) ) ) ) ;
2045
- } else if snippet == Some ( "'_" . to_string ( ) ) {
2046
- suggs. push ( Some ( Box :: new ( |n| n. to_string ( ) ) ) ) ;
2047
- } else if snippet == Some ( "" . to_string ( ) ) {
2048
- suggs. push ( Some ( Box :: new ( move |n| format ! ( "{}, " , n) . repeat ( count) ) ) ) ;
2049
- } else if let Some ( snippet) = snippet {
2050
- if !snippet. ends_with ( '>' ) {
2051
- suggs. push ( Some ( Box :: new ( move |name| {
2052
- format ! (
2053
- "{}<{}>" ,
2054
- snippet,
2055
- std:: iter:: repeat( name. to_string( ) )
2056
- . take( count)
2057
- . collect:: <Vec <_>>( )
2058
- . join( ", " )
2059
- )
2060
- } ) ) ) ;
2061
- } else {
2062
- suggs. push ( None ) ;
2063
- }
2064
- } else {
2065
- suggs. push ( None ) ;
2066
- }
2040
+ for ( snippet, ( _, count) ) in snippets. iter ( ) . zip ( spans_with_counts. iter ( ) . copied ( ) )
2041
+ {
2042
+ suggs. push ( match snippet. as_deref ( ) {
2043
+ Some ( "&" ) => Some ( Box :: new ( |name| format ! ( "&{} " , name) ) ) ,
2044
+ Some ( "'_" ) => Some ( Box :: new ( |n| n. to_string ( ) ) ) ,
2045
+ Some ( "" ) => Some ( Box :: new ( move |n| format ! ( "{}, " , n) . repeat ( count) ) ) ,
2046
+ Some ( snippet) if !snippet. ends_with ( '>' ) => Some ( Box :: new ( move |name| {
2047
+ format ! (
2048
+ "{}<{}>" ,
2049
+ snippet,
2050
+ std:: iter:: repeat( name. to_string( ) )
2051
+ . take( count)
2052
+ . collect:: <Vec <_>>( )
2053
+ . join( ", " )
2054
+ )
2055
+ } ) ) ,
2056
+ _ => None ,
2057
+ } ) ;
2067
2058
}
2068
- suggest_existing ( err, & name. as_str ( ) [ ..] , & suggs) ;
2059
+ suggest_existing ( err, & name. as_str ( ) [ ..] , suggs) ;
2069
2060
}
2070
2061
[ ] => {
2071
- let mut suggs: Vec < Option < String > > = Vec :: new ( ) ;
2072
- for ( snippet, count) in snippets. iter ( ) . cloned ( ) . zip ( counts. iter ( ) . copied ( ) ) {
2073
- if snippet == Some ( "&" . to_string ( ) ) {
2074
- suggs. push ( Some ( "&'a " . to_string ( ) ) ) ;
2075
- } else if snippet == Some ( "'_" . to_string ( ) ) {
2076
- suggs. push ( Some ( "'a" . to_string ( ) ) ) ;
2077
- } else if let Some ( snippet) = snippet {
2078
- if snippet == "" {
2079
- suggs. push ( Some (
2080
- std:: iter:: repeat ( "'a, " ) . take ( count) . collect :: < Vec < _ > > ( ) . join ( "" ) ,
2081
- ) ) ;
2082
- } else {
2083
- suggs. push ( Some ( format ! (
2084
- "{}<{}>" ,
2085
- snippet,
2086
- std:: iter:: repeat( "'a" ) . take( count) . collect:: <Vec <_>>( ) . join( ", " )
2087
- ) ) ) ;
2062
+ let mut suggs = Vec :: new ( ) ;
2063
+ for ( snippet, ( _, count) ) in
2064
+ snippets. iter ( ) . cloned ( ) . zip ( spans_with_counts. iter ( ) . copied ( ) )
2065
+ {
2066
+ suggs. push ( match snippet. as_deref ( ) {
2067
+ Some ( "&" ) => Some ( "&'a " . to_string ( ) ) ,
2068
+ Some ( "'_" ) => Some ( "'a" . to_string ( ) ) ,
2069
+ Some ( "" ) => {
2070
+ Some ( std:: iter:: repeat ( "'a, " ) . take ( count) . collect :: < Vec < _ > > ( ) . join ( "" ) )
2088
2071
}
2089
- } else {
2090
- suggs. push ( None ) ;
2091
- }
2072
+ Some ( snippet) => Some ( format ! (
2073
+ "{}<{}>" ,
2074
+ snippet,
2075
+ std:: iter:: repeat( "'a" ) . take( count) . collect:: <Vec <_>>( ) . join( ", " ) ,
2076
+ ) ) ,
2077
+ None => None ,
2078
+ } ) ;
2092
2079
}
2093
- suggest_new ( err, & suggs) ;
2080
+ suggest_new ( err, suggs) ;
2094
2081
}
2095
2082
lts if lts. len ( ) > 1 => {
2096
2083
err. span_note ( lifetime_spans, "these named lifetimes are available to use" ) ;
2097
2084
2098
2085
let mut spans_suggs: Vec < _ > = Vec :: new ( ) ;
2099
- for ( span, snippet) in spans . iter ( ) . copied ( ) . zip ( snippets. iter ( ) ) {
2100
- if Some ( "" ) == snippet. as_deref ( ) {
2101
- spans_suggs. push ( ( span, "'lifetime, " . to_string ( ) ) ) ;
2102
- } else if Some ( "&" ) == snippet . as_deref ( ) {
2103
- spans_suggs . push ( ( span , "&'lifetime " . to_string ( ) ) ) ;
2086
+ for ( ( span, _ ) , snippet) in spans_with_counts . iter ( ) . copied ( ) . zip ( snippets. iter ( ) ) {
2087
+ match snippet. as_deref ( ) {
2088
+ Some ( "" ) => spans_suggs. push ( ( span, "'lifetime, " . to_string ( ) ) ) ,
2089
+ Some ( "&" ) => spans_suggs . push ( ( span , "&'lifetime " . to_string ( ) ) ) ,
2090
+ _ => { }
2104
2091
}
2105
2092
}
2106
2093
0 commit comments