diff --git a/CHANGELOG.md b/CHANGELOG.md index 52320cb2e..0670353d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,10 @@ ### Bug Fixes -* None. +* Treat closures in the branches of ternary expressions as escaping + when the whole expression is escaping in the `unneeded_escaping` rule. + [SimplyDanny](https://github.com/SimplyDanny) + [#6386](https://github.com/realm/SwiftLint/pull/6386) ## 0.63.0-rc.2: High-Speed Extraction diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededEscapingRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededEscapingRule.swift index a0c6de354..f77b04f8d 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededEscapingRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/UnneededEscapingRule.swift @@ -102,6 +102,11 @@ struct UnneededEscapingRule: Rule { var cs = [1: c] } """, excludeFromDocumentation: true), + Example(""" + func f(c: @escaping () -> Void) { + f(true ? c : { }) + } + """), ], triggeringExamples: [ Example(""" @@ -324,6 +329,9 @@ private final class EscapeChecker: SyntaxVisitor { let declRef = optChain.expression.as(DeclReferenceExprSyntax.self) { return taintedVariables.contains(declRef.baseName.text) } + if let ternary = expr.as(TernaryExprSyntax.self) { + return isTainted(ternary.thenExpression) || isTainted(ternary.elseExpression) + } return false } }