Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration for for_where rule #4040

Closed
2 tasks done
marcelofabri opened this issue Jul 26, 2022 · 2 comments · Fixed by #4154
Closed
2 tasks done

Add configuration for for_where rule #4040

marcelofabri opened this issue Jul 26, 2022 · 2 comments · Fixed by #4154
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@marcelofabri
Copy link
Collaborator

New Issue Checklist


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.

@marcelofabri marcelofabri added the enhancement Ideas for improvements of existing features and rules. label Jul 26, 2022
@futuretap
Copy link

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.

@PangYenChen
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Ideas for improvements of existing features and rules.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants