SwiftLint
SwiftLint copied to clipboard
Add configuration for `for_where` rule
New Issue Checklist
- [x] Updated SwiftLint to the latest version
- [x] I searched for existing GitHub issues
While updating our code base to use SwiftLint 0.48.0, I realized for_where
is now triggering for some cases where it wasn't before. It's great to fix false positives, but it made me realize that this rule might not be always wanted.
In several places of our app, we use an if
inside a for
to test if any object in a given array meets a condition. Some examples:
for clause in clauses {
if clause.evaluate(object) {
return true
}
}
return false
for featureSet in minimalSupportedFeatureSets {
if device.supportsFeatureSet(featureSet) {
return true
}
}
return false
While we could update them to use where
, I kind of feel like it's against the spirit of the rule and it makes code harder to read:
for clause in clauses where clause.evaluate(object) {
return true
}
return false
Instead, the rule could have a configuration like allow_single_return
to not trigger a violation if the only statement inside the if
is a return
statement.
I also suggest to define an exception when the for clause and/or the if condition are pretty long and rewriting the code using where
would trigger a line length warning.
@marcelofabri The example you give can be rephrased as below:
return clauses.contains { clause in clause.evaluate(object) }
I am curious that is there another scenario you need allow_for_as_filter
?