@@ -26,6 +26,7 @@ pub struct InlayHintsConfig {
26
26
pub lifetime_elision_hints : LifetimeElisionHints ,
27
27
pub param_names_for_lifetime_elision_hints : bool ,
28
28
pub hide_named_constructor_hints : bool ,
29
+ pub hide_closure_initialization_hints : bool ,
29
30
pub max_length : Option < usize > ,
30
31
pub closing_brace_hints_min_lines : Option < usize > ,
31
32
}
@@ -467,10 +468,11 @@ fn closure_ret_hints(
467
468
return None ;
468
469
}
469
470
470
- let param_list = match closure. body ( ) {
471
- Some ( ast:: Expr :: BlockExpr ( _) ) => closure. param_list ( ) ?,
472
- _ => return None ,
473
- } ;
471
+ if !closure_has_block_body ( & closure) {
472
+ return None ;
473
+ }
474
+
475
+ let param_list = closure. param_list ( ) ?;
474
476
475
477
let closure = sema. descend_node_into_attributes ( closure. clone ( ) ) . pop ( ) ?;
476
478
let ty = sema. type_of_expr ( & ast:: Expr :: ClosureExpr ( closure) ) ?. adjusted ( ) ;
@@ -693,7 +695,7 @@ fn bind_pat_hints(
693
695
let desc_pat = descended. as_ref ( ) . unwrap_or ( pat) ;
694
696
let ty = sema. type_of_pat ( & desc_pat. clone ( ) . into ( ) ) ?. original ;
695
697
696
- if should_not_display_type_hint ( sema, pat, & ty) {
698
+ if should_not_display_type_hint ( sema, config , pat, & ty) {
697
699
return None ;
698
700
}
699
701
@@ -848,6 +850,7 @@ fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir
848
850
849
851
fn should_not_display_type_hint (
850
852
sema : & Semantics < RootDatabase > ,
853
+ config : & InlayHintsConfig ,
851
854
bind_pat : & ast:: IdentPat ,
852
855
pat_ty : & hir:: Type ,
853
856
) -> bool {
@@ -863,6 +866,18 @@ fn should_not_display_type_hint(
863
866
}
864
867
}
865
868
869
+ if config. hide_closure_initialization_hints {
870
+ if let Some ( parent) = bind_pat. syntax ( ) . parent ( ) {
871
+ if let Some ( it) = ast:: LetStmt :: cast ( parent. clone ( ) ) {
872
+ if let Some ( ast:: Expr :: ClosureExpr ( closure) ) = it. initializer ( ) {
873
+ if closure_has_block_body ( & closure) {
874
+ return true ;
875
+ }
876
+ }
877
+ }
878
+ }
879
+ }
880
+
866
881
for node in bind_pat. syntax ( ) . ancestors ( ) {
867
882
match_ast ! {
868
883
match node {
@@ -889,6 +904,10 @@ fn should_not_display_type_hint(
889
904
false
890
905
}
891
906
907
+ fn closure_has_block_body ( closure : & ast:: ClosureExpr ) -> bool {
908
+ matches ! ( closure. body( ) , Some ( ast:: Expr :: BlockExpr ( _) ) )
909
+ }
910
+
892
911
fn should_hide_param_name_hint (
893
912
sema : & Semantics < RootDatabase > ,
894
913
callable : & hir:: Callable ,
@@ -1083,6 +1102,7 @@ mod tests {
1083
1102
reborrow_hints : ReborrowHints :: Always ,
1084
1103
binding_mode_hints : false ,
1085
1104
hide_named_constructor_hints : false ,
1105
+ hide_closure_initialization_hints : false ,
1086
1106
param_names_for_lifetime_elision_hints : false ,
1087
1107
max_length : None ,
1088
1108
closing_brace_hints_min_lines : None ,
@@ -2034,6 +2054,53 @@ fn main() {
2034
2054
) ;
2035
2055
}
2036
2056
2057
+ #[ test]
2058
+ fn skip_closure_type_hints ( ) {
2059
+ check_with_config (
2060
+ InlayHintsConfig {
2061
+ type_hints : true ,
2062
+ hide_closure_initialization_hints : true ,
2063
+ ..DISABLED_CONFIG
2064
+ } ,
2065
+ r#"
2066
+ //- minicore: fn
2067
+ fn main() {
2068
+ let multiple_2 = |x: i32| { x * 2 };
2069
+
2070
+ let multiple_2 = |x: i32| x * 2;
2071
+ // ^^^^^^^^^^ |i32| -> i32
2072
+
2073
+ let (not) = (|x: bool| { !x });
2074
+ // ^^^ |bool| -> bool
2075
+
2076
+ let (is_zero, _b) = (|x: usize| { x == 0 }, false);
2077
+ // ^^^^^^^ |usize| -> bool
2078
+ // ^^ bool
2079
+
2080
+ let plus_one = |x| { x + 1 };
2081
+ // ^ u8
2082
+ foo(plus_one);
2083
+
2084
+ let add_mul = bar(|x: u8| { x + 1 });
2085
+ // ^^^^^^^ impl FnOnce(u8) -> u8 + ?Sized
2086
+
2087
+ let closure = if let Some(6) = add_mul(2).checked_sub(1) {
2088
+ // ^^^^^^^ fn(i32) -> i32
2089
+ |x: i32| { x * 2 }
2090
+ } else {
2091
+ |x: i32| { x * 3 }
2092
+ };
2093
+ }
2094
+
2095
+ fn foo(f: impl FnOnce(u8) -> u8) {}
2096
+
2097
+ fn bar(f: impl FnOnce(u8) -> u8) -> impl FnOnce(u8) -> u8 {
2098
+ move |x: u8| f(x) * 2
2099
+ }
2100
+ "# ,
2101
+ ) ;
2102
+ }
2103
+
2037
2104
#[ test]
2038
2105
fn hint_truncation ( ) {
2039
2106
check_with_config (
0 commit comments