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

Fix void_function_in_ternary incorrectly triggered with if statement #5674

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,56 @@ struct VoidFunctionInTernaryConditionRule: Rule {
subscript(index: Int) -> Int {
index == 0 ? defaultValue() : compute(index)
"""),
Example("""
func example(index: Int) -> String {
if true {
return index == 0 ? defaultValue() : defaultValue()
} else {
return "Default"
}
}
"""),
Example("""
func exampleNestedIfExpr(index: Int) -> String {
if true {
return index == 0 ? defaultValue() : defaultValue()
} else {
return "Default"
}
u-abyss marked this conversation as resolved.
Show resolved Hide resolved
}
"""),
Example("""
func collectionView() -> CGSize {
switch indexPath.section {
case 0: return isEditing ? CGSize(width: 150, height: 20) : CGSize(width: 100, height: 20)
default: .zero
}
}
"""),
Example("""
func exampleNestedIfExprAndSwitchExpr(index: Int) -> String {
if index <= 3 {
switch index {
case 1:
if isTrue {
return isTrue ? "1" : "2"
} else {
return "3"
}
case 2:
if isTrue {
return "4"
} else {
return "5"
}
default:
return "6"
}
} else {
return "7"
}
}
"""),
],
triggeringExamples: [
Example("success ↓? askQuestion() : exit()"),
Expand Down Expand Up @@ -104,6 +154,64 @@ struct VoidFunctionInTernaryConditionRule: Rule {
return index
}
"""),
Example("""
func example() -> Void {
if true {
isTrue ↓? defaultValue() : defaultValue()
} else {
print("false")
}
}
"""),
Example("""
func collectionView() -> CGSize {
switch indexPath.section {
case 0: isEditing ↓? CGSize(width: 150, height: 20) : CGSize(width: 100, height: 20)
default: .zero
}
return hoge
}
"""),
Example("""
func exampleNestedIfExpr() -> String {
if true {
if true {
isTrue ↓? defaultValue() : defaultValue()
retun "True"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
retun "True"
return "True"

} else {
return "False"
}
} else {
return "Default"
}
return "hoge"
}
"""),
Example("""
func exampleNestedIfExprAndSwitchExpr() -> String {
if true {
switch value {
case 1:
if flag {
isTrue ↓? print("hoge") : print("hoge")
return "3"
} else {
return "3"
}
case 2:
if true {
return "4"
} else {
return "5"
}
default:
return "6"
}
} else {
return "7"
}
}
"""),
]
)
}
Expand Down Expand Up @@ -152,7 +260,7 @@ private extension CodeBlockItemSyntax {
var isImplicitReturn: Bool {
isClosureImplictReturn || isFunctionImplicitReturn ||
isVariableImplicitReturn || isSubscriptImplicitReturn ||
isAcessorImplicitReturn
isAcessorImplicitReturn || isIfOrSwitchExprImplicitReturn
}

var isClosureImplictReturn: Bool {
Expand Down Expand Up @@ -199,6 +307,37 @@ private extension CodeBlockItemSyntax {

return parent.children(viewMode: .sourceAccurate).count == 1
}

// 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) {
Copy link
Collaborator

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.

return targetSyntax
}
if let ifExprSyntax = targetSyntax?.as(IfExprSyntax.self) {
if ifExprSyntax.body.statements.last != codeBlockItem {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about else blocks?

return nil
}
guard let codeBlockItemSyntax = ifExprSyntax.parent?.parent?.as(CodeBlockItemSyntax.self) else {
return nil
}
return getFunctionDeclSyntax(codeBlockItem: codeBlockItemSyntax)
}

if let switchExpr = targetSyntax?.parent?.as(SwitchExprSyntax.self) {
guard let codeBlockItemSyntax = switchExpr.parent?.parent?.as(CodeBlockItemSyntax.self) else {
return nil
}
return getFunctionDeclSyntax(codeBlockItem: codeBlockItemSyntax)
}

return nil
}

var isIfOrSwitchExprImplicitReturn: Bool {
guard let functionDeclSyntax = getFunctionDeclSyntax(codeBlockItem: self) else { return false }
return functionDeclSyntax.signature.allowsImplicitReturns
}
}

private extension FunctionSignatureSyntax {
Expand Down