@@ -668,9 +668,27 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
668
668
} ) ;
669
669
let end = e. span . hi ( ) ;
670
670
671
+ // Walk through all the non-code space before each match arm. The space trailing the final arm is
672
+ // handled after the `try_fold` e.g.
673
+ //
674
+ // match foo {
675
+ // _________^- everything between the scrutinee and arm1
676
+ //| arm1 => (),
677
+ //|---^___________^ everything before arm2
678
+ //| #[cfg(feature = "enabled")]
679
+ //| arm2 => some_code(),
680
+ //|---^____________________^ everything before arm3
681
+ //| // some comment about arm3
682
+ //| arm3 => some_code(),
683
+ //|---^____________________^ everything after arm3
684
+ //| #[cfg(feature = "disabled")]
685
+ //| arm4 = some_code(),
686
+ //|};
687
+ //|^
671
688
let found = arm_spans. try_fold ( start, |start, range| {
672
689
let Some ( ( end, next_start) ) = range else {
673
- // Shouldn't happen, but treat this as though a `cfg` attribute were found
690
+ // Shouldn't happen as macros can't expand to match arms, but treat this as though a `cfg` attribute were
691
+ // found.
674
692
return Err ( ( ) ) ;
675
693
} ;
676
694
let span = SpanData {
@@ -697,6 +715,7 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
697
715
}
698
716
}
699
717
718
+ /// Checks if the given span contains a `#[cfg(..)]` attribute
700
719
fn span_contains_cfg ( cx : & LateContext < ' _ > , s : Span ) -> bool {
701
720
let Some ( snip) = snippet_opt ( cx, s) else {
702
721
// Assume true. This would require either an invalid span, or one which crosses file boundaries.
@@ -708,6 +727,8 @@ fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
708
727
pos += t. len ;
709
728
( t. kind , start..pos)
710
729
} ) ;
730
+
731
+ // Search for the token sequence [`#`, `[`, `cfg`]
711
732
while iter. any ( |( t, _) | matches ! ( t, TokenKind :: Pound ) ) {
712
733
let mut iter = iter. by_ref ( ) . skip_while ( |( t, _) | {
713
734
matches ! (
0 commit comments