-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fix void_function_in_ternary incorrectly triggered with if statement #5674
base: main
Are you sure you want to change the base?
Conversation
Here's an example of your CHANGELOG entry: * Fix void_function_in_ternary incorrectly triggered with if statement.
[u-abyss](https://github.com/u-abyss)
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number) note: There are two invisible spaces after the entry's text. Generated by 🚫 Danger |
529c862
to
337358f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks taking up the issue!
The current implementation is very specific to implicit returns. However, if
and switch
expression can also appear as variable initializers and they can be nested into each other. I don't think the current solution addresses these cases, does it?
Source/SwiftLintBuiltInRules/Rules/Idiomatic/VoidFunctionInTernaryConditionRule.swift
Show resolved
Hide resolved
@SimplyDanny |
Can you provide an example? The code func f(i: Int) -> Int {
if i > 1 {
let j = i - 1
j
} else {
0
}
} wouldn't compile. There you need the |
if true { | ||
if true { | ||
isTrue ↓? defaultValue() : defaultValue() | ||
retun "True" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
retun "True" | |
return "True" |
return targetSyntax | ||
} | ||
if let ifExprSyntax = targetSyntax?.as(IfExprSyntax.self) { | ||
if ifExprSyntax.body.statements.last != codeBlockItem { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about else
blocks?
// For codeBlockItem, recursively traverse it to determine if it is within its own FunctionDeclSyntax. | ||
func getFunctionDeclSyntax(codeBlockItem: CodeBlockItemSyntax) -> FunctionDeclSyntax? { | ||
let targetSyntax = codeBlockItem.parent?.parent?.parent?.parent | ||
if let targetSyntax = targetSyntax?.as(FunctionDeclSyntax.self) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This covers only functions, but there are also the other cases which are checked above in the is...Return
properties.
Fixes #5611
This PR corrects that VoidFunctionInTernaryConditionRule incorrectly triggered with if statement.
This new rule is triggered or not depending on the return type of the function.