SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Rule Request: `implicit_return` should also enforce explicit returns

Open StevenSorial opened this issue 4 years ago • 3 comments

New Issue Checklist

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.

StevenSorial avatar May 12 '21 18:05 StevenSorial

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.

username0x0a avatar May 15 '21 16:05 username0x0a

I think introducing another explicit: array would cause confusion. a boolean should be enough.

implicit_return:
    included:
        - closure
        - getter
    enforceExplicitReturns: true // default to false

StevenSorial avatar May 15 '21 19:05 StevenSorial

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
    • implicit to force-omit the keyword in that case
    • explicit to 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

username0x0a avatar May 15 '21 19:05 username0x0a