swiftlang / swiftlang/swift-syntax
Prevent unrelated casts using `as`
Open
enhancement
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 553
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 16
Description
Currently, `SyntaxProtocol` has an `as` method that allows casting to any other syntax node type. Because of this, it’s possible to e.g. cast a `FunctionDeclSyntax` to an `IdentifierExprSyntax` without any compiler errors or warnings, even though we know that the cast will always fail.
What we should do instead, is to only have a `as(_: ExprSyntaxProtocol.Type)` function on `ExprSyntax` (analogous for `StmtSyntax` and the other base nodes).
The `as` function on `SyntaxProtocol` can then be marked as deprecated and produce a warning that the cast will always fail.
`is` and `cast` will need to be updated accordingly.
Contributor guide
Assessment
This issue has not been assessed yet.