@@ -334,8 +334,10 @@ pub(crate) fn highlight_branches(
334334 match token. kind ( ) {
335335 T ! [ match ] => {
336336 for token in sema. descend_into_macros ( token. clone ( ) ) {
337- let Some ( match_expr) =
338- sema. token_ancestors_with_macros ( token) . find_map ( ast:: MatchExpr :: cast)
337+ let Some ( match_expr) = sema
338+ . token_ancestors_with_macros ( token)
339+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
340+ . find_map ( ast:: MatchExpr :: cast)
339341 else {
340342 continue ;
341343 } ;
@@ -355,11 +357,14 @@ pub(crate) fn highlight_branches(
355357 }
356358 T ! [ =>] => {
357359 for token in sema. descend_into_macros ( token. clone ( ) ) {
358- let Some ( arm) =
359- sema. token_ancestors_with_macros ( token) . find_map ( ast:: MatchArm :: cast)
360+ let Some ( arm) = sema
361+ . token_ancestors_with_macros ( token)
362+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
363+ . find_map ( ast:: MatchArm :: cast)
360364 else {
361365 continue ;
362366 } ;
367+
363368 let file_id = sema. hir_file_for ( arm. syntax ( ) ) ;
364369 let range = arm. fat_arrow_token ( ) . map ( |token| token. text_range ( ) ) ;
365370 push_to_highlights ( file_id, range, & mut highlights) ;
@@ -368,9 +373,11 @@ pub(crate) fn highlight_branches(
368373 }
369374 }
370375 T ! [ if ] => {
371- for tok in sema. descend_into_macros ( token. clone ( ) ) {
372- let Some ( if_expr) =
373- sema. token_ancestors_with_macros ( tok) . find_map ( ast:: IfExpr :: cast)
376+ for token in sema. descend_into_macros ( token. clone ( ) ) {
377+ let Some ( if_expr) = sema
378+ . token_ancestors_with_macros ( token)
379+ . take_while ( |node| !ast:: MacroCall :: can_cast ( node. kind ( ) ) )
380+ . find_map ( ast:: IfExpr :: cast)
374381 else {
375382 continue ;
376383 } ;
@@ -2481,6 +2488,27 @@ fn main() {
24812488 )
24822489 }
24832490
2491+ #[ test]
2492+ fn match_in_macro ( ) {
2493+ // We should not highlight the outer `match` expression.
2494+ check (
2495+ r#"
2496+ macro_rules! M {
2497+ (match) => { 1 };
2498+ }
2499+
2500+ fn main() {
2501+ match Some(1) {
2502+ Some(x) => x,
2503+ None => {
2504+ M!(match$0)
2505+ }
2506+ }
2507+ }
2508+ "# ,
2509+ )
2510+ }
2511+
24842512 #[ test]
24852513 fn labeled_block_tail_expr ( ) {
24862514 check (
0 commit comments