@@ -274,6 +274,11 @@ fn replace_usages(
274
274
{
275
275
edit. replace ( ty_annotation. syntax ( ) . text_range ( ) , "Bool" ) ;
276
276
replace_bool_expr ( edit, initializer) ;
277
+ } else if let Some ( receiver) = find_method_call_expr_usage ( & new_name) {
278
+ edit. replace (
279
+ receiver. syntax ( ) . text_range ( ) ,
280
+ format ! ( "({} == Bool::True)" , receiver) ,
281
+ ) ;
277
282
} else if new_name. syntax ( ) . ancestors ( ) . find_map ( ast:: UseTree :: cast) . is_none ( ) {
278
283
// for any other usage in an expression, replace it with a check that it is the true variant
279
284
if let Some ( ( record_field, expr) ) = new_name
@@ -426,6 +431,17 @@ fn find_assoc_const_usage(name: &ast::NameLike) -> Option<(ast::Type, ast::Expr)
426
431
Some ( ( const_. ty ( ) ?, const_. body ( ) ?) )
427
432
}
428
433
434
+ fn find_method_call_expr_usage ( name : & ast:: NameLike ) -> Option < ast:: Expr > {
435
+ let method_call = name. syntax ( ) . ancestors ( ) . find_map ( ast:: MethodCallExpr :: cast) ?;
436
+ let receiver = method_call. receiver ( ) ?;
437
+
438
+ if !receiver. syntax ( ) . descendants ( ) . contains ( name. syntax ( ) ) {
439
+ return None ;
440
+ }
441
+
442
+ Some ( receiver)
443
+ }
444
+
429
445
/// Adds the definition of the new enum before the target node.
430
446
fn add_enum_def (
431
447
edit : & mut SourceChangeBuilder ,
@@ -1287,6 +1303,38 @@ fn main() {
1287
1303
)
1288
1304
}
1289
1305
1306
+ #[ test]
1307
+ fn field_method_chain_usage ( ) {
1308
+ check_assist (
1309
+ bool_to_enum,
1310
+ r#"
1311
+ struct Foo {
1312
+ $0bool: bool,
1313
+ }
1314
+
1315
+ fn main() {
1316
+ let foo = Foo { bool: true };
1317
+
1318
+ foo.bool.then(|| 2);
1319
+ }
1320
+ "# ,
1321
+ r#"
1322
+ #[derive(PartialEq, Eq)]
1323
+ enum Bool { True, False }
1324
+
1325
+ struct Foo {
1326
+ bool: Bool,
1327
+ }
1328
+
1329
+ fn main() {
1330
+ let foo = Foo { bool: Bool::True };
1331
+
1332
+ (foo.bool == Bool::True).then(|| 2);
1333
+ }
1334
+ "# ,
1335
+ )
1336
+ }
1337
+
1290
1338
#[ test]
1291
1339
fn field_non_bool ( ) {
1292
1340
cov_mark:: check!( not_applicable_non_bool_field) ;
0 commit comments