implicit_return should not trigger when there are subsequent statements
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
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 useswiftlint lint --path [file here] --no-cache --enable-all-rules.
func asdf() -> String {
// This triggers a violation:
return "qwer"
return "asdf"
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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