Skip to content

Fix unsafe expressions in NoAssignmentInExpressions #1021

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

Merged
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Sources/SwiftFormat/Rules/NoAssignmentInExpressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ public final class NoAssignmentInExpressions: SyntaxFormatRule {

/// Returns a value indicating whether the given node is a standalone assignment statement.
///
/// This function considers try/await expressions and automatically walks up through them as
/// needed. This is because `try f().x = y` should still be a standalone assignment for our
/// This function considers try/await/unsafe expressions and automatically walks up through them
/// as needed. This is because `try f().x = y` should still be a standalone assignment for our
/// purposes, even though a `TryExpr` will wrap the `InfixOperatorExpr` and thus would not be
/// considered a standalone assignment if we only checked the infix expression for a
/// `CodeBlockItem` parent.
private func isStandaloneAssignmentStatement(_ node: InfixOperatorExprSyntax) -> Bool {
var node = Syntax(node)
while let parent = node.parent,
parent.is(TryExprSyntax.self) || parent.is(AwaitExprSyntax.self)
parent.is(TryExprSyntax.self) || parent.is(AwaitExprSyntax.self) || parent.is(UnsafeExprSyntax.self)
{
node = parent
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,21 @@ final class NoAssignmentInExpressionsTests: LintOrFormatRuleTestCase {
)
}

func testTryAndAwaitAssignmentExpressionsAreUnchanged() {
func testTryAndAwaitAndUnsafeAssignmentExpressionsAreUnchanged() {
assertFormatting(
NoAssignmentInExpressions.self,
input: """
func foo() {
try a.b = c
await a.b = c
unsafe a.b = c
}
""",
expected: """
func foo() {
try a.b = c
await a.b = c
unsafe a.b = c
}
""",
findings: []
Expand Down