Skip to content

swift-format still can't treat unsafe expressions correctly in some cases #977

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

Closed
kkebo opened this issue Apr 5, 2025 · 5 comments · Fixed by #1021
Closed

swift-format still can't treat unsafe expressions correctly in some cases #977

kkebo opened this issue Apr 5, 2025 · 5 comments · Fixed by #1021

Comments

@kkebo
Copy link
Contributor

kkebo commented Apr 5, 2025

Thanks for fixing #972. However, it seems that swift-format still can't treat unsafe expressions correctly in some cases. The cases are when a l-value is unsafe.

Input

let ptr: UnsafePointer<UInt8>?
// comment
unsafe ptr = .init(bitPattern: 0)
print(ptr)

Expected result

let ptr: UnsafePointer<UInt8>?
// comment
unsafe ptr = .init(bitPattern: 0)
print(ptr)

Actual result

let ptr: UnsafePointer<UInt8>?
// comment

// comment
unsafe ptr = .init(bitPattern: 0)
print(ptr)

Environment

@TTOzzi
Copy link
Member

TTOzzi commented Apr 5, 2025

I think this might be an issue with swift-syntax 🤔

Image

@TTOzzi
Copy link
Member

TTOzzi commented Apr 5, 2025

I've opened a PR with the fix here swiftlang/swift-syntax#3043!

@ahoppen
Copy link
Member

ahoppen commented Apr 5, 2025

Synced to Apple’s issue tracker as rdar://148657851

@kkebo
Copy link
Contributor Author

kkebo commented May 15, 2025

I've confirmed that swift-format's format mode have been fixed thanks to swiftlang/swift-syntax#3043.

However, its lint mode is still broken. Here is how to reproduce the issue.

Input (test.swift)

let ptr: UnsafePointer<UInt8>?
// comment
unsafe ptr = .init(bitPattern: 0)
print(ptr)

.swift-format

{
  "rules" : {
    "NoAssignmentInExpressions" : true
  },
  "version" : 1
}

Steps to reproduce

  1. Create test.swift
  2. Create .swift-format
  3. Run swift format lint test.swift

Expected result

(no warnings)

Actual result

test.swift:3:8: warning: [NoAssignmentInExpressions] move this assignment expression into its own statement
test.swift:3:8: warning: [NoAssignmentInExpressions] move this assignment expression into its own statement

kkebo added a commit to kkebo/swift_os that referenced this issue May 15, 2025
kkebo added a commit to kkebo/swift-format that referenced this issue May 15, 2025
@kkebo
Copy link
Contributor Author

kkebo commented May 15, 2025

I've created a PR: #1021

kkebo added a commit to kkebo/swift-format that referenced this issue May 15, 2025
kkebo added a commit to kkebo/swift_os that referenced this issue May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants