-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fix void_function_in_ternary incorrectly triggered with if statement #5674
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
base: main
Are you sure you want to change the base?
Changes from all commits
65f72bc
0ca536b
f439ad2
65d7eac
29b49ce
e810d70
cbf755c
c16a7c0
cc6fc9d
0e9e428
a33917a
8d5b9f8
be1efd1
dbeef12
ae1cf7e
df72a00
5544376
a38ce32
e52ccd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+90
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code can be refactored and nested if-else can be removed.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| triggeringExamples: [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Example("success ↓? askQuestion() : exit()"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } 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" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -152,7 +260,7 @@ private extension CodeBlockItemSyntax { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var isImplicitReturn: Bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isClosureImplictReturn || isFunctionImplicitReturn || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isVariableImplicitReturn || isSubscriptImplicitReturn || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isAcessorImplicitReturn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isAcessorImplicitReturn || isIfOrSwitchExprImplicitReturn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var isClosureImplictReturn: Bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return targetSyntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if let ifExprSyntax = targetSyntax?.as(IfExprSyntax.self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ifExprSyntax.body.statements.last != codeBlockItem { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.