@@ -318,8 +318,10 @@ pub(crate) fn highlight_branches(
318
318
match token. kind ( ) {
319
319
T ! [ match ] => {
320
320
for token in sema. descend_into_macros ( token. clone ( ) ) {
321
- let Some ( match_expr) =
322
- sema. token_ancestors_with_macros ( token) . find_map ( ast:: MatchExpr :: cast)
321
+ let Some ( match_expr) = sema
322
+ . token_ancestors_with_macros ( token)
323
+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
324
+ . find_map ( ast:: MatchExpr :: cast)
323
325
else {
324
326
continue ;
325
327
} ;
@@ -339,11 +341,14 @@ pub(crate) fn highlight_branches(
339
341
}
340
342
T ! [ =>] => {
341
343
for token in sema. descend_into_macros ( token. clone ( ) ) {
342
- let Some ( arm) =
343
- sema. token_ancestors_with_macros ( token) . find_map ( ast:: MatchArm :: cast)
344
+ let Some ( arm) = sema
345
+ . token_ancestors_with_macros ( token)
346
+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
347
+ . find_map ( ast:: MatchArm :: cast)
344
348
else {
345
349
continue ;
346
350
} ;
351
+
347
352
let file_id = sema. hir_file_for ( arm. syntax ( ) ) ;
348
353
let range = arm. fat_arrow_token ( ) . map ( |token| token. text_range ( ) ) ;
349
354
push_to_highlights ( file_id, range, & mut highlights) ;
@@ -352,9 +357,11 @@ pub(crate) fn highlight_branches(
352
357
}
353
358
}
354
359
T ! [ if ] => {
355
- for tok in sema. descend_into_macros ( token. clone ( ) ) {
356
- let Some ( if_expr) =
357
- sema. token_ancestors_with_macros ( tok) . find_map ( ast:: IfExpr :: cast)
360
+ for token in sema. descend_into_macros ( token. clone ( ) ) {
361
+ let Some ( if_expr) = sema
362
+ . token_ancestors_with_macros ( token)
363
+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
364
+ . find_map ( ast:: IfExpr :: cast)
358
365
else {
359
366
continue ;
360
367
} ;
@@ -2388,4 +2395,25 @@ fn main() {
2388
2395
"# ,
2389
2396
)
2390
2397
}
2398
+
2399
+ #[ test]
2400
+ fn match_in_macro ( ) {
2401
+ // We should not highlight the outer `match` expression.
2402
+ check (
2403
+ r#"
2404
+ macro_rules! M {
2405
+ (match) => { 1 };
2406
+ }
2407
+
2408
+ fn main() {
2409
+ match Some(1) {
2410
+ Some(x) => x,
2411
+ None => {
2412
+ M!(match$0)
2413
+ }
2414
+ }
2415
+ }
2416
+ "# ,
2417
+ )
2418
+ }
2391
2419
}
0 commit comments