swiftlang / swiftlang/swift-syntax

Prevent unrelated casts using `as`

Open
#2,092 11 comments 0 reactions 1 assignee Claimed by @Matejkob View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.