-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Type: Feature / Diagnostic
Priority: Medium
Context
We implemented a new diagnostic to detect unreachable else if branches where the condition is equivalent to a previous if condition. This helps surface dead branches caused by duplicated or redundant conditions in control-flow chains, including nested and looped contexts.
Goals
Detect duplicate else if conditions in the same if/else-if chain.
Emit a warning on the duplicated else if condition span.
Support commutative comparisons (e.g., 0 == x vs x == 0).
Avoid false positives in unrelated if statements or guard-style patterns.
Ensure diagnostics appear in both HumanReadable and JSON/SARIF outputs.
Implementation Summary
New diagnostic DuplicateIfCondition at IR level:
Detects duplicate else if by comparing canonicalized conditions.
Supports commutative comparison normalization.
Tracks operands to avoid false positives when stores intervene.
Uses dominator tree plus source-based “else token” filter to restrict to actual else-if chains.
Added tests under test/diagnostics/:
- [duplicate-else-if-basic.c] (positive)
- [duplicate-else-if-commutative.c] (positive)
- [duplicate-else-if-distinct.c] (negative)
- [duplicate-else-if-separate.c] (negative)
- [duplicate-else-if-nested-loop.c] (positive, nested + macro)
- [duplicate-else-if-bool.c] (positive, boolean)
Acceptance Criteria
Sample duplicated else-if produces a warning on the second condition.
No warnings for distinct else-if conditions or separate if statements.
Works in nested/looped contexts.
Output consistent across human-readable and machine outputs.
Notes
This is an IR-level diagnostic; behavior depends on optimization and constant folding.
attribute((optnone)) or frontend optnone injection may be required to preserve conditions for analysis.