realm / realm/SwiftLint

implicit_return should not trigger when there are subsequent statements

Open
#3,694 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Describe the bug

The rule triggers even though there are subsequent statements in a func, e.g.

    func asdf() -> String {
        return "qwer"
        return "asdf"
    }

will trigger the rule and get autocorrected to

    func asdf() -> String {
        "qwer"
        return "asdf"
    }

which effectively changes runtime behavior of the code.

Of course, there is no point in having subsequent statements after a return statement in production code. But it is a commonly used trick during debugging a specific situation to insert an explicit return at the beginning of a func, e.g. to force a specific value to be returned or bypass an operation that's normally executed by the func, for testing purposes.

The behavior can be quite confusing because it is likely that, by the time of autocorrection happening, the developer's focus is back on the simulator or on the code they are about to debug. They don't realize the changed code and are left to wonder why their code still behaves like "asdf" had been returned, even though they explicitly told the func to return "qwer"

The actual problem after adding the 2nd return is a different one: the code after the 1st return can never execute. But this is not concern of this rule.

Complete output when running SwiftLint, including the stack trace and command used
Pods/SwiftLint/swiftlint --fix path/to/MyFile.swift   
Correcting Swift files at paths path/to/MyFile.swift
Correcting 'MyFile.swift' (1/1)
path/to/MyFile.swift:28:9 Corrected Implicit Return
Done inspecting 1 file for auto-correction!
Environment
  • SwiftLint version: 0.43.1
  • Installation method used: CocoaPods
  • Paste your configuration file:
opt_in_rules:
  - implicit_return
# [...]

  • Are you using nested configurations? No
    If so, paste their relative paths and respective contents.
  • Which Xcode version are you using (check xcodebuild -version)? Xcode 12.5
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
    func asdf() -> String {
        // This triggers a violation:
        return "qwer"
        return "asdf"
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the implicit_return rule and reproduce the issue with the SwiftLint stdin command or the path/to/MyFile.swift example. Trace the autocorrection behavior for a return followed by another statement, then verify that such code is not implicitly-return corrected while ordinary eligible returns still are.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.