Skip to content

[SR-13899] Missing conditional cast fix-it #56297

@dan-zheng

Description

@dan-zheng
Previous ID SR-13899
Radar None
Original Reporter @dan-zheng
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, DiagnosticsQol, StarterBug, TypeChecker
Assignee luciano (JIRA)
Priority Medium

md5: 79f973c3624b69402e948da2cfdd28c7

Issue Description:

let any: Any = 1
if let int = any as Int {
  // ...
}
$ swiftc missing-checked-cast-fixit.swift
missing-conditional-cast-fixit.swift:2:18: error: 'Any' is not convertible to 'Int'; did you mean to use 'as!' to force downcast?
if let int = any as Int {
             ~~~~^~~~~~
                 as!

There's only a fix-it for force downcasting (as!). It could also make sense to have one for conditional downcasting (as?), I ran into such a case in practice. Example:

missing-checked-cast-fixit.swift:2:18: error: 'Any' is not convertible to 'Int'; did you mean to use 'as?' to conditionally downcast?
if let int = any as Int {
             ~~~~^~~~~~
                 as?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationgood first issueGood for newcomerstype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions