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

Don't trigger the return_value_from_void_function warning from initializers #5429

Conversation

mrbkap
Copy link
Contributor

@mrbkap mrbkap commented Jan 18, 2024

Consider the code:

func foo() {
   enum Bar: RawRepresentable() {
       case one
       case two

       init?(rawValue: String) {
           return nil
       }
   }
}

This example should not trigger return_value_from_void_function in the initializer because the initializer is nullable and can return nil. However, using the latest version of SwiftLint I'm seeing this rule trigger on this example.

The problem appears to be that Syntax.enclosingFunction extension climbs the AST's parent chain looking for a FunctionDecl but initializers are neither blocks nor variable declarations. By bailing when we see an initializer, we won't trigger this rule incorrectly.

@SwiftLintBot
Copy link

SwiftLintBot commented Jan 18, 2024

18 Messages
📖 Linting Aerial with this PR took 1.19s vs 1.16s on main (2% slower)
📖 Linting Alamofire with this PR took 1.62s vs 1.61s on main (0% slower)
📖 Linting Brave with this PR took 9.2s vs 9.2s on main (0% slower)
📖 Linting DuckDuckGo with this PR took 4.89s vs 4.89s on main (0% slower)
📖 Linting Firefox with this PR took 11.0s vs 11.0s on main (0% slower)
📖 Linting Kickstarter with this PR took 11.16s vs 11.16s on main (0% slower)
📖 Linting Moya with this PR took 0.61s vs 0.61s on main (0% slower)
📖 Linting NetNewsWire with this PR took 3.3s vs 3.29s on main (0% slower)
📖 Linting Nimble with this PR took 0.89s vs 0.89s on main (0% slower)
📖 Linting PocketCasts with this PR took 8.97s vs 8.94s on main (0% slower)
📖 Linting Quick with this PR took 0.39s vs 0.39s on main (0% slower)
📖 Linting Realm with this PR took 5.74s vs 5.75s on main (0% faster)
📖 Linting Sourcery with this PR took 2.74s vs 2.71s on main (1% slower)
📖 Linting Swift with this PR took 5.55s vs 5.54s on main (0% slower)
📖 Linting VLC with this PR took 1.57s vs 1.56s on main (0% slower)
📖 Linting Wire with this PR took 19.85s vs 19.86s on main (0% faster)
📖 Linting WordPress with this PR took 13.23s vs 13.3s on main (0% faster)
📖 This PR fixed a violation in Brave: /Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Paged/EthereumProviderScriptHandler.swift:139:18: warning: Return Value from Void Function Violation: Returning values from Void functions should be avoided (return_value_from_void_function)

Generated by 🚫 Danger

@mrbkap mrbkap force-pushed the return-value-from-void-initializer-false-positive branch from 4793adf to 8fd6c97 Compare January 18, 2024 23:21
Copy link
Collaborator

@SimplyDanny SimplyDanny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks!

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@SimplyDanny SimplyDanny enabled auto-merge (squash) January 19, 2024 19:18
@SimplyDanny SimplyDanny merged commit 2b6e0ee into realm:main Jan 19, 2024
12 checks passed
@mrbkap mrbkap deleted the return-value-from-void-initializer-false-positive branch January 20, 2024 05:29
@mrbkap
Copy link
Contributor Author

mrbkap commented Jan 20, 2024

Thanks for the quick action and cleanups!

u-abyss pushed a commit to u-abyss/SwiftLint that referenced this pull request Feb 11, 2024
kohtenko pushed a commit to kohtenko/SwiftLint that referenced this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants