Skip to content

Prevent unrelated casts using as #2092

Open
@ahoppen

Description

@ahoppen

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.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions