SwiftLint
SwiftLint copied to clipboard
Add IfSwitchExpressionRule for enforcing if and switch statements. In…
…clude non-triggering and triggering examples.
Add IfSwitchExpressionRule for enforcing if and switch statements. Include non-triggering and triggering examples.
| 1 Error | |
|---|---|
| :no_entry_sign: | Could not build branch |
| 2 Warnings | |
|---|---|
| :warning: | If this is a user-facing change, please include a CHANGELOG entry to credit yourself! You can find it at CHANGELOG.md. |
| :warning: | This PR may need tests. |
Here's an example of your CHANGELOG entry:
* Add IfSwitchExpressionRule for enforcing if and switch statements. In….
[AballahNsour](https://github.com/AballahNsour)
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)
note: There are two invisible spaces after the entry's text.
Generated by :no_entry_sign: Danger
Hi @AballahNsour! Your examples are good, but the implementation of the rule is definitely not sufficient. Are you going to continue on this PR?
hi @SimplyDanny , If you can give more guidance I can continue working on this RP. if not I not continue working on it
So if we start with if statements only, we expect
func f(value: Bool) -> Int {
if value {
return 1
} else {
return 2
}
}
to trigger the rule as it can be rewritten to
func f(value: Bool) -> Int {
return if value {
1
} else {
2
}
}
or even
func f(value: Bool) -> Int {
if value {
1
} else {
2
}
}
if one prefers implicit returns. Is that correct?
Your current implementation triggers on each and every if statement/expression, though. You need to add more logic to find the correct cases on which the rule shall trigger on considering the different examples you have already provided.
I have to say that his is not an easy rule to implement. Hence, it's fine for me if you disregard it. At the moment, though, I cannot see at least a direction of where this is heading given the rudimentary implementation.