@@ -27,7 +27,9 @@ mod wild_in_or_pats;
27
27
use clippy_config:: Conf ;
28
28
use clippy_utils:: msrvs:: { self , Msrv } ;
29
29
use clippy_utils:: source:: walk_span_to_context;
30
- use clippy_utils:: { higher, is_direct_expn_of, is_in_const_context, is_span_match, span_contains_cfg} ;
30
+ use clippy_utils:: {
31
+ higher, is_direct_expn_of, is_in_const_context, is_span_match, span_contains_cfg, span_extract_comments,
32
+ } ;
31
33
use rustc_hir:: { Arm , Expr , ExprKind , LetStmt , MatchSource , Pat , PatKind } ;
32
34
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
33
35
use rustc_middle:: lint:: in_external_macro;
@@ -1059,7 +1061,28 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
1059
1061
}
1060
1062
1061
1063
redundant_pattern_match:: check_match ( cx, expr, ex, arms) ;
1062
- single_match:: check ( cx, ex, arms, expr) ;
1064
+ let source_map = cx. tcx . sess . source_map ( ) ;
1065
+ let mut match_comments = span_extract_comments ( source_map, expr. span ) ;
1066
+ // We remove comments from inside arms block.
1067
+ if !match_comments. is_empty ( ) {
1068
+ for arm in arms {
1069
+ for comment in span_extract_comments ( source_map, arm. body . span ) {
1070
+ if let Some ( index) = match_comments
1071
+ . iter ( )
1072
+ . enumerate ( )
1073
+ . find ( |( _, cm) | * * cm == comment)
1074
+ . map ( |( index, _) | index)
1075
+ {
1076
+ match_comments. remove ( index) ;
1077
+ }
1078
+ }
1079
+ }
1080
+ }
1081
+ // If there are still comments, it means they are outside of the arms, therefore
1082
+ // we should not lint.
1083
+ if match_comments. is_empty ( ) {
1084
+ single_match:: check ( cx, ex, arms, expr) ;
1085
+ }
1063
1086
match_bool:: check ( cx, ex, arms, expr) ;
1064
1087
overlapping_arms:: check ( cx, ex, arms) ;
1065
1088
match_wild_enum:: check ( cx, ex, arms) ;
0 commit comments