@@ -1797,12 +1797,20 @@ fn lint_or_fun_call<'tcx>(
1797
1797
cx : & LateContext < ' tcx > ,
1798
1798
name : & str ,
1799
1799
method_span : Span ,
1800
- fun_span : Span ,
1801
1800
self_expr : & hir:: Expr < ' _ > ,
1802
1801
arg : & ' tcx hir:: Expr < ' _ > ,
1803
- or_has_args : bool ,
1804
1802
span : Span ,
1803
+ // None if lambda is required
1804
+ fun_span : Option < Span > ,
1805
1805
) {
1806
+ // (path, fn_has_argument, methods, suffix)
1807
+ static KNOW_TYPES : [ ( & [ & str ] , bool , & [ & str ] , & str ) ; 4 ] = [
1808
+ ( & paths:: BTREEMAP_ENTRY , false , & [ "or_insert" ] , "with" ) ,
1809
+ ( & paths:: HASHMAP_ENTRY , false , & [ "or_insert" ] , "with" ) ,
1810
+ ( & paths:: OPTION , false , & [ "map_or" , "ok_or" , "or" , "unwrap_or" ] , "else" ) ,
1811
+ ( & paths:: RESULT , true , & [ "or" , "unwrap_or" ] , "else" ) ,
1812
+ ] ;
1813
+
1806
1814
if let hir:: ExprKind :: MethodCall ( ref path, _, ref args, _) = & arg. kind {
1807
1815
if path. ident . as_str ( ) == "len" {
1808
1816
let ty = cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) . peel_refs ( ) ;
@@ -1818,32 +1826,32 @@ fn lint_or_fun_call<'tcx>(
1818
1826
}
1819
1827
}
1820
1828
1821
- // (path, fn_has_argument, methods, suffix)
1822
- let know_types: & [ ( & [ _ ] , _ , & [ _ ] , _ ) ] = & [
1823
- ( & paths:: BTREEMAP_ENTRY , false , & [ "or_insert" ] , "with" ) ,
1824
- ( & paths:: HASHMAP_ENTRY , false , & [ "or_insert" ] , "with" ) ,
1825
- ( & paths:: OPTION , false , & [ "map_or" , "ok_or" , "or" , "unwrap_or" ] , "else" ) ,
1826
- ( & paths:: RESULT , true , & [ "or" , "unwrap_or" ] , "else" ) ,
1827
- ] ;
1828
-
1829
1829
if_chain ! {
1830
- if know_types . iter( ) . any( |k| k. 2 . contains( & name) ) ;
1830
+ if KNOW_TYPES . iter( ) . any( |k| k. 2 . contains( & name) ) ;
1831
1831
1832
1832
if is_lazyness_candidate( cx, arg) ;
1833
1833
if !contains_return( & arg) ;
1834
1834
1835
1835
let self_ty = cx. typeck_results( ) . expr_ty( self_expr) ;
1836
1836
1837
1837
if let Some ( & ( _, fn_has_arguments, poss, suffix) ) =
1838
- know_types . iter( ) . find( |&&i| match_type( cx, self_ty, i. 0 ) ) ;
1838
+ KNOW_TYPES . iter( ) . find( |&&i| match_type( cx, self_ty, i. 0 ) ) ;
1839
1839
1840
1840
if poss. contains( & name) ;
1841
1841
1842
1842
then {
1843
- let sugg: Cow <' _, _> = match ( fn_has_arguments, !or_has_args) {
1844
- ( true , _) => format!( "|_| {}" , snippet_with_macro_callsite( cx, arg. span, ".." ) ) . into( ) ,
1845
- ( false , false ) => format!( "|| {}" , snippet_with_macro_callsite( cx, arg. span, ".." ) ) . into( ) ,
1846
- ( false , true ) => snippet_with_macro_callsite( cx, fun_span, ".." ) ,
1843
+ let sugg: Cow <' _, str > = {
1844
+ let ( snippet_span, use_lambda) = match ( fn_has_arguments, fun_span) {
1845
+ ( false , Some ( fun_span) ) => ( fun_span, false ) ,
1846
+ _ => ( arg. span, true ) ,
1847
+ } ;
1848
+ let snippet = snippet_with_macro_callsite( cx, snippet_span, ".." ) ;
1849
+ if use_lambda {
1850
+ let l_arg = if fn_has_arguments { "_" } else { "" } ;
1851
+ format!( "|{}| {}" , l_arg, snippet) . into( )
1852
+ } else {
1853
+ snippet
1854
+ }
1847
1855
} ;
1848
1856
let span_replace_word = method_span. with_hi( span. hi( ) ) ;
1849
1857
span_lint_and_sugg(
@@ -1864,28 +1872,13 @@ fn lint_or_fun_call<'tcx>(
1864
1872
hir:: ExprKind :: Call ( ref fun, ref or_args) => {
1865
1873
let or_has_args = !or_args. is_empty ( ) ;
1866
1874
if !check_unwrap_or_default ( cx, name, fun, & args[ 0 ] , & args[ 1 ] , or_has_args, expr. span ) {
1867
- check_general_case (
1868
- cx,
1869
- name,
1870
- method_span,
1871
- fun. span ,
1872
- & args[ 0 ] ,
1873
- & args[ 1 ] ,
1874
- or_has_args,
1875
- expr. span ,
1876
- ) ;
1875
+ let fun_span = if or_has_args { None } else { Some ( fun. span ) } ;
1876
+ check_general_case ( cx, name, method_span, & args[ 0 ] , & args[ 1 ] , expr. span , fun_span) ;
1877
1877
}
1878
1878
} ,
1879
- hir:: ExprKind :: MethodCall ( _, span, ref or_args, _) => check_general_case (
1880
- cx,
1881
- name,
1882
- method_span,
1883
- span,
1884
- & args[ 0 ] ,
1885
- & args[ 1 ] ,
1886
- !or_args. is_empty ( ) ,
1887
- expr. span ,
1888
- ) ,
1879
+ hir:: ExprKind :: Index ( ..) | hir:: ExprKind :: MethodCall ( ..) => {
1880
+ check_general_case ( cx, name, method_span, & args[ 0 ] , & args[ 1 ] , expr. span , None ) ;
1881
+ } ,
1889
1882
_ => { } ,
1890
1883
}
1891
1884
}
0 commit comments