1
- use ide_db:: assists:: { AssistId , AssistKind } ;
1
+ use ide_db:: {
2
+ assists:: { AssistId , AssistKind } ,
3
+ famous_defs:: FamousDefs ,
4
+ } ;
2
5
use syntax:: {
3
- ast:: { self , make, HasArgList } ,
6
+ ast:: { self , make, Expr , HasArgList } ,
4
7
AstNode ,
5
8
} ;
6
9
@@ -21,6 +24,9 @@ use crate::{AssistContext, Assists};
21
24
// ```
22
25
pub ( crate ) fn replace_or_with_or_else ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
23
26
let call: ast:: MethodCallExpr = ctx. find_node_at_offset ( ) ?;
27
+
28
+ is_option_or_result ( call. receiver ( ) ?, ctx) ?;
29
+
24
30
let ( name, arg_list) = ( call. name_ref ( ) ?, call. arg_list ( ) ?) ;
25
31
26
32
let replace = match & * name. text ( ) {
@@ -76,6 +82,8 @@ pub(crate) fn replace_or_with_or_else(acc: &mut Assists, ctx: &AssistContext<'_>
76
82
pub ( crate ) fn replace_or_else_with_or ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
77
83
let call: ast:: MethodCallExpr = ctx. find_node_at_offset ( ) ?;
78
84
85
+ is_option_or_result ( call. receiver ( ) ?, ctx) ?;
86
+
79
87
let ( name, arg_list) = ( call. name_ref ( ) ?, call. arg_list ( ) ?) ;
80
88
81
89
let replace = match & * name. text ( ) {
@@ -115,9 +123,32 @@ pub(crate) fn replace_or_else_with_or(acc: &mut Assists, ctx: &AssistContext<'_>
115
123
)
116
124
}
117
125
126
+ fn is_option_or_result ( receiver : Expr , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
127
+ let ty = ctx. sema . type_of_expr ( & receiver) ?. adjusted ( ) . as_adt ( ) ?. as_enum ( ) ?;
128
+ let option_enum =
129
+ FamousDefs ( & ctx. sema , ctx. sema . scope ( receiver. syntax ( ) ) ?. krate ( ) ) . core_option_Option ( ) ;
130
+
131
+ if let Some ( option_enum) = option_enum {
132
+ if ty == option_enum {
133
+ return Some ( ( ) ) ;
134
+ }
135
+ }
136
+
137
+ let result_enum =
138
+ FamousDefs ( & ctx. sema , ctx. sema . scope ( receiver. syntax ( ) ) ?. krate ( ) ) . core_result_Result ( ) ;
139
+
140
+ if let Some ( result_enum) = result_enum {
141
+ if ty == result_enum {
142
+ return Some ( ( ) ) ;
143
+ }
144
+ }
145
+
146
+ None
147
+ }
148
+
118
149
#[ cfg( test) ]
119
150
mod tests {
120
- use crate :: tests:: check_assist;
151
+ use crate :: tests:: { check_assist, check_assist_not_applicable } ;
121
152
122
153
use super :: * ;
123
154
@@ -126,6 +157,7 @@ mod tests {
126
157
check_assist (
127
158
replace_or_with_or_else,
128
159
r#"
160
+ //- minicore: option
129
161
fn foo() {
130
162
let foo = Some(1);
131
163
return foo.unwrap_$0or(2);
@@ -145,6 +177,7 @@ fn foo() {
145
177
check_assist (
146
178
replace_or_with_or_else,
147
179
r#"
180
+ //- minicore: option
148
181
fn foo() {
149
182
let foo = Some(1);
150
183
return foo.unwrap_$0or(x());
@@ -164,6 +197,7 @@ fn foo() {
164
197
check_assist (
165
198
replace_or_with_or_else,
166
199
r#"
200
+ //- minicore: option
167
201
fn foo() {
168
202
let foo = Some(1);
169
203
return foo.unwrap_$0or({
@@ -195,6 +229,7 @@ fn foo() {
195
229
check_assist (
196
230
replace_or_else_with_or,
197
231
r#"
232
+ //- minicore: option
198
233
fn foo() {
199
234
let foo = Some(1);
200
235
return foo.unwrap_$0or_else(|| 2);
@@ -214,6 +249,7 @@ fn foo() {
214
249
check_assist (
215
250
replace_or_else_with_or,
216
251
r#"
252
+ //- minicore: option
217
253
fn foo() {
218
254
let foo = Some(1);
219
255
return foo.unwrap_$0or_else(x);
@@ -224,6 +260,39 @@ fn foo() {
224
260
let foo = Some(1);
225
261
return foo.unwrap_or(x());
226
262
}
263
+ "# ,
264
+ )
265
+ }
266
+
267
+ #[ test]
268
+ fn replace_or_else_with_or_result ( ) {
269
+ check_assist (
270
+ replace_or_else_with_or,
271
+ r#"
272
+ //- minicore: result
273
+ fn foo() {
274
+ let foo = Ok(1);
275
+ return foo.unwrap_$0or_else(x);
276
+ }
277
+ "# ,
278
+ r#"
279
+ fn foo() {
280
+ let foo = Ok(1);
281
+ return foo.unwrap_or(x());
282
+ }
283
+ "# ,
284
+ )
285
+ }
286
+
287
+ #[ test]
288
+ fn replace_or_else_with_or_not_applicable ( ) {
289
+ check_assist_not_applicable (
290
+ replace_or_else_with_or,
291
+ r#"
292
+ fn foo() {
293
+ let foo = Ok(1);
294
+ return foo.unwrap_$0or_else(x);
295
+ }
227
296
"# ,
228
297
)
229
298
}
0 commit comments