Rule request: no identical branches
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New rule request
When refactoring or deleting code I sometimes end up with an if-else where both branches have the same code. It would be nice if SwiftLint could report those branches so I can get rid of the code branching completely. Ideally this could be also applied to duplicated branches in switch statements.
- Why should this rule be added? Share links to existing discussion about what
the community thinks about this.
Having two branches that execute the same code based on a different condition means that either the condition is not necessary at all (e.g. for an if-else statement) or it could be refactored to combine the duplicated branches (e.g. when having a more complicated if-elseif-else statement). See also the sonarjs rule for JavaScript.
- Provide several examples of what would and wouldn't trigger violations.
Violation:
if foo {
bar()
} else {
bar() // duplicated
}
if foo {
bar()
else if baz {
bar() // duplicated
} else
other()
}
switch foo {
case "a":
bar()
case "b":
bar() // duplicated
}
Non-Violation:
if foo {
bar()
} else {
other()
}
if foo || bar {
bar()
} else
other()
}
switch foo {
case "a", "b":
bar()
}
- Should the rule be configurable, if so what parameters should be configurable?
No, it doesn’t need to be configurable
- Should the rule be opt-in or enabled by default? Why?
I think it would be good as a default rule because this rule almost always detects bad structured code. But personally I don’t really care about this, because I’m not relying on any defaults, using only_rules instead.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the linked SonarJS no-duplicated-branches rule and the Swift examples in this issue. No SwiftLint files or tests are named, so the implementation entry point must be located in the repository. Done means detecting identical if-else and switch branches while leaving the listed non-violations unaffected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100