@@ -335,8 +335,10 @@ pub(crate) fn highlight_branches(
335
335
match token. kind ( ) {
336
336
T ! [ match ] => {
337
337
for token in sema. descend_into_macros ( token. clone ( ) ) {
338
- let Some ( match_expr) =
339
- sema. token_ancestors_with_macros ( token) . find_map ( ast:: MatchExpr :: cast)
338
+ let Some ( match_expr) = sema
339
+ . token_ancestors_with_macros ( token)
340
+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
341
+ . find_map ( ast:: MatchExpr :: cast)
340
342
else {
341
343
continue ;
342
344
} ;
@@ -356,11 +358,14 @@ pub(crate) fn highlight_branches(
356
358
}
357
359
T ! [ =>] => {
358
360
for token in sema. descend_into_macros ( token. clone ( ) ) {
359
- let Some ( arm) =
360
- sema. token_ancestors_with_macros ( token) . find_map ( ast:: MatchArm :: cast)
361
+ let Some ( arm) = sema
362
+ . token_ancestors_with_macros ( token)
363
+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
364
+ . find_map ( ast:: MatchArm :: cast)
361
365
else {
362
366
continue ;
363
367
} ;
368
+
364
369
let file_id = sema. hir_file_for ( arm. syntax ( ) ) ;
365
370
let range = arm. fat_arrow_token ( ) . map ( |token| token. text_range ( ) ) ;
366
371
push_to_highlights ( file_id, range, & mut highlights) ;
@@ -369,9 +374,11 @@ pub(crate) fn highlight_branches(
369
374
}
370
375
}
371
376
T ! [ if ] => {
372
- for tok in sema. descend_into_macros ( token. clone ( ) ) {
373
- let Some ( if_expr) =
374
- sema. token_ancestors_with_macros ( tok) . find_map ( ast:: IfExpr :: cast)
377
+ for token in sema. descend_into_macros ( token. clone ( ) ) {
378
+ let Some ( if_expr) = sema
379
+ . token_ancestors_with_macros ( token)
380
+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
381
+ . find_map ( ast:: IfExpr :: cast)
375
382
else {
376
383
continue ;
377
384
} ;
@@ -2418,6 +2425,27 @@ fn main() {
2418
2425
)
2419
2426
}
2420
2427
2428
+ #[ test]
2429
+ fn match_in_macro ( ) {
2430
+ // We should not highlight the outer `match` expression.
2431
+ check (
2432
+ r#"
2433
+ macro_rules! M {
2434
+ (match) => { 1 };
2435
+ }
2436
+
2437
+ fn main() {
2438
+ match Some(1) {
2439
+ Some(x) => x,
2440
+ None => {
2441
+ M!(match$0)
2442
+ }
2443
+ }
2444
+ }
2445
+ "# ,
2446
+ )
2447
+ }
2448
+
2421
2449
#[ test]
2422
2450
fn labeled_block_tail_expr ( ) {
2423
2451
check (
0 commit comments