Fix redundantClosure false positive for async let - #2659
Conversation
Closes #2658 Co-authored-by: calda <1811727+calda@users.noreply.github.com>
redundantClosure false positive for async let
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2659 +/- ##
===========================================
- Coverage 95.45% 95.45% -0.01%
===========================================
Files 178 178
Lines 27137 27209 +72
===========================================
+ Hits 25904 25971 +67
- Misses 1233 1238 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@copilot, actually the issue is that this closure is not redundant because an if with no else is not a valid if expression: // Not valid
async let doAsync =
if 1 == 1 { // error: 'if' must have an unconditional 'else' to be used as expression
print()
}
// Valid
async let doAsync =
if 1 == 1 {
print(1)
} else {
print(2)
}Ensure |
…nc let Don't remove closures containing non-exhaustive if expressions (if without else), since those are not valid Swift expressions. This correctly handles the async let case from #2658 and any other context where a non-exhaustive if statement is wrapped in an immediately-invoked closure. Co-authored-by: calda <1811727+calda@users.noreply.github.com>
... Updated the fix to check for non-exhaustive |
| extension Formatter { | ||
| /// Finds the `if` or `switch` keyword index inside a closure body, | ||
| /// skipping over `return`, `try`, `try?`, `try!`, and `await` keywords. | ||
| func indexOfConditionalKeywordInClosure(startOfBody: Int) -> Int? { |
There was a problem hiding this comment.
@copilot this new helper / the complex code in it shouldn't be necessary, check how other if expression rules like redundantReturn or conditionalAssignment handle this.
There was a problem hiding this comment.
Replaced the custom indexOfConditionalKeywordInClosure helper with a direct call to conditionalBranches(at:), which already handles skipping try/await keywords. See commit 7e1f5a3.
Co-authored-by: calda <1811727+calda@users.noreply.github.com>
|
@copilot please lint |
Co-authored-by: calda <1811727+calda@users.noreply.github.com>
Done in commit d89d44d. |
redundantClosureincorrectly flags closures inasync letdeclarations as redundant. The closure is not redundant here — it's required to defer execution until the value is awaited.Changes:
RedundantClosure.swiftthat detectsasync let/vardeclarations by walking backward from the=sign and skips the ruletestKeepsClosureInAsyncLetredundantClosurefalse positive forasync let#2658