Skip to content

Add ignore_branches for synthetic else branches - #1189

Merged
sferik merged 1 commit into
mainfrom
ignore-branches-implicit-else
May 26, 2026
Merged

sferik merged 1 commit into
mainfrom
ignore-branches-implicit-else

Conversation

@sferik

@sferik sferik commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Ruby's Coverage library reports an :else branch for several syntactic constructs even when the source has no literal else keyword: exhaustive case/in pattern matches, case/when without an else arm, ||= / &&=, and if / unless without an else. Those synthetic branches show up as missed and depress branch-coverage percentages despite there being no source code to test against.

Only :implicit_else is supported today, but the variadic shape leaves room for future synthetic branch types without an API change. Unknown tokens raise SimpleCov::ConfigurationError immediately to catch typos.

The detection signal is structural rather than textual. An earlier draft scanned the source at the branch's reported start_col for the word else, but Coverage reports the position of the body of an explicit else arm (e.g. :nope on the line below the keyword), not the keyword itself, so the heuristic mis-classified explicit elses as synthetic. The shipped check compares start_line against the parent condition's start_line: a synthetic else inherits its parent's range and matches, while an explicit else arm carries its own line number. This matches every existing branch fixture in the spec suite (case, case_without_else, nested_branches, branches, inline, elsif, branch_tester) without source-text scanning.

This comment was marked as resolved.

Ruby's Coverage library reports an `:else` branch for several syntactic
constructs even when the source has no literal `else` keyword:
exhaustive `case/in` pattern matches, `case/when` without an `else`
arm, `||=` / `&&=`, and `if` / `unless` without an `else`. Those
synthetic branches show up as missed and depress branch-coverage
percentages despite there being no source code to test against.

Only `:implicit_else` is supported today, but the variadic shape leaves
room for future synthetic branch types without an API change. Unknown
tokens raise `SimpleCov::ConfigurationError` immediately to catch typos.

The detection signal is structural rather than textual. An earlier draft
scanned the source at the branch's reported `start_col` for the word
`else`, but `Coverage` reports the position of the *body* of an explicit
`else` arm (e.g. `:nope` on the line below the keyword), not the keyword
itself, so the heuristic mis-classified explicit elses as synthetic. The
shipped check compares `start_line` against the parent condition's
`start_line`: a synthetic else inherits its parent's range and matches,
while an explicit `else` arm carries its own line number. This matches
every existing branch fixture in the spec suite (case,
case_without_else, nested_branches, branches, inline, elsif,
branch_tester) without source-text scanning.

Resolves #1033.
@sferik
sferik force-pushed the ignore-branches-implicit-else branch from a0aad12 to a0722ab Compare May 26, 2026 17:55
@sferik
sferik merged commit 6e6da3a into main May 26, 2026
24 checks passed
@sferik
sferik deleted the ignore-branches-implicit-else branch August 12, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants