3
3
//! fn f(a: i32, b: i32) -> i32 { a + b }
4
4
//! let _x /* i32 */= f(4, 4);
5
5
//! ```
6
- use hir:: { Semantics , TypeInfo } ;
6
+ use hir:: Semantics ;
7
7
use ide_db:: { base_db:: FileId , famous_defs:: FamousDefs , RootDatabase } ;
8
8
9
9
use itertools:: Itertools ;
@@ -28,11 +28,41 @@ pub(super) fn hints(
28
28
return None ;
29
29
}
30
30
31
+ let parent = pat. syntax ( ) . parent ( ) ?;
32
+ let type_ascriptable = match_ast ! {
33
+ match parent {
34
+ ast:: Param ( it) => {
35
+ if it. ty( ) . is_some( ) {
36
+ return None ;
37
+ }
38
+ Some ( it. colon_token( ) )
39
+ } ,
40
+ ast:: LetStmt ( it) => {
41
+ if config. hide_closure_initialization_hints {
42
+ if let Some ( ast:: Expr :: ClosureExpr ( closure) ) = it. initializer( ) {
43
+ if closure_has_block_body( & closure) {
44
+ return None ;
45
+ }
46
+ }
47
+ }
48
+ if it. ty( ) . is_some( ) {
49
+ return None ;
50
+ }
51
+ Some ( it. colon_token( ) )
52
+ } ,
53
+ _ => None
54
+ }
55
+ } ;
56
+
31
57
let descended = sema. descend_node_into_attributes ( pat. clone ( ) ) . pop ( ) ;
32
58
let desc_pat = descended. as_ref ( ) . unwrap_or ( pat) ;
33
59
let ty = sema. type_of_binding_in_pat ( desc_pat) ?;
34
60
35
- if should_not_display_type_hint ( sema, config, pat, & ty) {
61
+ if ty. is_unknown ( ) {
62
+ return None ;
63
+ }
64
+
65
+ if sema. resolve_bind_pat_to_const ( pat) . is_some ( ) {
36
66
return None ;
37
67
}
38
68
@@ -44,11 +74,6 @@ pub(super) fn hints(
44
74
return None ;
45
75
}
46
76
47
- let type_ascriptable = desc_pat. syntax ( ) . parent ( ) . and_then ( |it| {
48
- ast:: LetStmt :: cast ( it. clone ( ) )
49
- . map ( |it| it. colon_token ( ) )
50
- . or_else ( || ast:: Param :: cast ( it) . map ( |it| it. colon_token ( ) ) )
51
- } ) ;
52
77
let text_edit = if let Some ( colon_token) = & type_ascriptable {
53
78
ty_to_text_edit (
54
79
sema,
@@ -89,57 +114,6 @@ pub(super) fn hints(
89
114
Some ( ( ) )
90
115
}
91
116
92
- fn should_not_display_type_hint (
93
- sema : & Semantics < ' _ , RootDatabase > ,
94
- config : & InlayHintsConfig ,
95
- bind_pat : & ast:: IdentPat ,
96
- pat_ty : & hir:: Type ,
97
- ) -> bool {
98
- let db = sema. db ;
99
-
100
- if pat_ty. is_unknown ( ) {
101
- return true ;
102
- }
103
-
104
- if sema. resolve_bind_pat_to_const ( bind_pat) . is_some ( ) {
105
- return true ;
106
- }
107
-
108
- for node in bind_pat. syntax ( ) . ancestors ( ) {
109
- match_ast ! {
110
- match node {
111
- ast:: LetStmt ( it) => {
112
- if config. hide_closure_initialization_hints {
113
- if let Some ( ast:: Expr :: ClosureExpr ( closure) ) = it. initializer( ) {
114
- if closure_has_block_body( & closure) {
115
- return true ;
116
- }
117
- }
118
- }
119
- return it. ty( ) . is_some( )
120
- } ,
121
- // FIXME: We might wanna show type hints in parameters for non-top level patterns as well
122
- ast:: Param ( it) => return it. ty( ) . is_some( ) ,
123
- ast:: MatchArm ( _) => return pat_is_enum_variant( db, bind_pat, pat_ty) ,
124
- ast:: LetExpr ( _) => return pat_is_enum_variant( db, bind_pat, pat_ty) ,
125
- ast:: IfExpr ( _) => return false ,
126
- ast:: WhileExpr ( _) => return false ,
127
- ast:: ForExpr ( it) => {
128
- // We *should* display hint only if user provided "in {expr}" and we know the type of expr (and it's not unit).
129
- // Type of expr should be iterable.
130
- return it. in_token( ) . is_none( ) ||
131
- it. iterable( )
132
- . and_then( |iterable_expr| sema. type_of_expr( & iterable_expr) )
133
- . map( TypeInfo :: original)
134
- . map_or( true , |iterable_ty| iterable_ty. is_unknown( ) || iterable_ty. is_unit( ) )
135
- } ,
136
- _ => ( ) ,
137
- }
138
- }
139
- }
140
- false
141
- }
142
-
143
117
fn is_named_constructor (
144
118
sema : & Semantics < ' _ , RootDatabase > ,
145
119
pat : & ast:: IdentPat ,
@@ -193,19 +167,6 @@ fn is_named_constructor(
193
167
( ctor_name == ty_name) . then_some ( ( ) )
194
168
}
195
169
196
- fn pat_is_enum_variant ( db : & RootDatabase , bind_pat : & ast:: IdentPat , pat_ty : & hir:: Type ) -> bool {
197
- if let Some ( hir:: Adt :: Enum ( enum_data) ) = pat_ty. as_adt ( ) {
198
- let pat_text = bind_pat. to_string ( ) ;
199
- enum_data
200
- . variants ( db)
201
- . into_iter ( )
202
- . map ( |variant| variant. name ( db) . to_smol_str ( ) )
203
- . any ( |enum_name| enum_name == pat_text)
204
- } else {
205
- false
206
- }
207
- }
208
-
209
170
#[ cfg( test) ]
210
171
mod tests {
211
172
// This module also contains tests for super::closure_ret
0 commit comments