Rule Request: `implicit_return` should also enforce explicit returns
New Issue Checklist
- [x] Updated SwiftLint to the latest version
- [x] I searched for existing GitHub issues
New rule request
implicit_return should also enforce explicit returns
Why should this rule be added?
When implicit_return is configured like:
implicit_return:
included:
- closure
- getter
I think it should also enforce explicit returns in contexts not included in the config (function in this example).
I always assumed it was a bug (and replied in #3029) until I read the source code.
Not really. 🤔 At least not to generate a lot of warnings version-to-version.
I'd rather introduce an explicit_return rule or rework this one to a generic return:
return:
implicit: // optional – check that `return` keyword is omitted
- closure
- getter
explicit: // optional – check that `return` keyword is present
- function
so that any possible state to cover with SwiftLint is defined strictly explicitly.
I think introducing another explicit: array would cause confusion. a boolean should be enough.
implicit_return:
included:
- closure
- getter
enforceExplicitReturns: true // default to false
Adding explicit option inside a rule called implicit seems more confusing – simply the rule alone is about enforcing implicit or skipping the check, so such option looks “out of scope” for that rule. Moreover, defining something like “return must be present in functions, otherwise we don't care” would be complicated.
My proposal would simply allow:
- specifying which rules must be followed
-
implicitto force-omit the keyword in that case -
explicitto force-contain the keyword in that case
-
- allowing to keep cases open (not checked) for those not present in any of the groups or by omitting the group completely