Skip to content

Commit 78345b4

Browse files
committed
Clarify cfg detection process in matches.rs
1 parent 8912d65 commit 78345b4

File tree

1 file changed

+22
-1
lines changed
  • clippy_lints/src/matches

1 file changed

+22
-1
lines changed

clippy_lints/src/matches/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,27 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
668668
});
669669
let end = e.span.hi();
670670

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+
//|^
671688
let found = arm_spans.try_fold(start, |start, range| {
672689
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.
674692
return Err(());
675693
};
676694
let span = SpanData {
@@ -697,6 +715,7 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
697715
}
698716
}
699717

718+
/// Checks if the given span contains a `#[cfg(..)]` attribute
700719
fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
701720
let Some(snip) = snippet_opt(cx, s) else {
702721
// 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 {
708727
pos += t.len;
709728
(t.kind, start..pos)
710729
});
730+
731+
// Search for the token sequence [`#`, `[`, `cfg`]
711732
while iter.any(|(t, _)| matches!(t, TokenKind::Pound)) {
712733
let mut iter = iter.by_ref().skip_while(|(t, _)| {
713734
matches!(

0 commit comments

Comments
 (0)