fix: void_function_in_ternary false positive inside if/switch expressions#6510
Open
WZBbiao wants to merge 3 commits intorealm:mainfrom
Open
fix: void_function_in_ternary false positive inside if/switch expressions#6510WZBbiao wants to merge 3 commits intorealm:mainfrom
WZBbiao wants to merge 3 commits intorealm:mainfrom
Conversation
Generated by 🚫 Danger |
added 2 commits
February 24, 2026 18:08
…hExprImplicitReturn IfExprSyntax and SwitchExprSyntax used as statements are wrapped in ExpressionStmtSyntax inside CodeBlockItemSyntax. The previous code incorrectly tried to cast the parent directly to CodeBlockItemSyntax, which always returned nil and caused the non-triggering examples to be incorrectly flagged as violations. Also adds CHANGELOG entry for the void_function_in_ternary fix.
…ssues Replace recursive call to isImplicitReturn inside isIfExprOrSwitchExprImplicitReturn with a new non-recursive isImplicitReturnExcludingIfSwitchExpr property. This avoids potential data races when multiple rules traverse the same syntax tree concurrently during parallel rule execution in SwiftLint's linter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
void_function_in_ternaryrule incorrectly flags ternary expressions that are used as the return value of aniforswitchexpression (Swift 5.9+). When a ternary is the sole expression in a branch of anif/switchexpression that itself serves as an implicit return value, its result is being used — so the rule should not flag it.Minimal reproducer:
Related reports: #5611
Root Cause
The rule determines whether a ternary is a "void call" by checking whether its containing
CodeBlockItemSyntaxis in an implicit return position (viaisImplicitReturn). However,isImplicitReturnonly checked for:It did not account for a ternary being the sole expression inside a branch of an
iforswitchexpression that itself occupies one of those implicit-return positions.Solution
Added a new computed property
isIfExprOrSwitchExprImplicitReturnto theCodeBlockItemSyntaxextension. It walks up the syntax tree:ifexpression branch —CodeBlockItemListSyntax → CodeBlockSyntax → IfExprSyntax— and then checks whether theIfExprSyntax's own containingCodeBlockItemSyntaxsatisfiesisImplicitReturn(recursively, to handle nesting).switchexpression case —CodeBlockItemListSyntax → SwitchCaseSyntax → SwitchCaseListSyntax → SwitchExprSyntax— and applies the same recursive check on theSwitchExprSyntax's container.This correctly handles arbitrarily nested
if/switchexpressions inside any implicit-return context (functions, closures, computed properties, subscripts, accessors).Testing
Two new non-triggering examples were added to
VoidFunctionInTernaryConditionRule.description:ifexpression branch that is the function's implicit return.switchexpression case that is the function's implicit return.The existing triggering examples remain unchanged, ensuring that legitimate void-function ternaries (e.g. ternaries with additional statements following them in the same block) are still caught.
Fixes #5611