Skip to content
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

[SR-14646] Suggest to replace control flow condition to type-checking when result of downcasting is not used #56998

Closed
WowbaggersLiquidLunch opened this issue May 19, 2021 · 6 comments
Assignees
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement

Comments

@WowbaggersLiquidLunch
Copy link
Collaborator

Previous ID SR-14646
Radar rdar://problem/78276233
Original Reporter @WowbaggersLiquidLunch
Type Improvement
Status Closed
Resolution Done
Environment

Swift (Trunk) Development Snapshot 2021-05-17 (a)
Xcode 12.4 (12D4e)
macOS 10.15.7 (19H1030)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug
Assignee @WowbaggersLiquidLunch
Priority Medium

md5: 1cfced954c10e0b1e0facdc9ec346d1a

Issue Description:

2 examples:

func answer(of universe: Any?) -> Int {
    // current fix-it: Replace 'let bar = universe as? Foo' with '(universe as? Foo) != nil'
    // better fix-it: Replace 'let bar = universe as? Foo' with 'universe is Foo'
    if let bar = universe as? Foo {
        return 42
    } else {
        return 54
    }
}
let foo: Optional<Any> = 42 as Int

// current fix-it: Replace 'let bar = foo as? Int' with '(foo as? Int) != nil'
// better fix-it: Replace 'let bar = foo as? Int' with 'foo is Int'

if let bar = foo as? Int { 
    print("'foo' is of type 'Int'")
}
@typesanitizer
Copy link

That's strange. I see the following diagnostic:

Value 'bar' was defined but never used; consider replacing with boolean test
Replace 'let bar = foo as?' with 'foo is'

So the "better fix-it" behavior is what I see locally. Are you sure you're using the newer toolchain for testing?

@WowbaggersLiquidLunch
Copy link
Collaborator Author

My mistake. I was using the newer toolchain, but I overly-reduced the example without checking it first. The "current fix-it" occurs only when downcasting from Optional<Any>. I fixed the example, and added another one.

@typesanitizer
Copy link

@swift-ci create

@swift-ci
Copy link
Collaborator

Comment by Minhyuk Kim (JIRA)

I created a PR here! #37897

@xedin
Copy link
Member

xedin commented Jun 22, 2021

Fixed by #37897 Thank you, mininny (JIRA User)! @WowbaggersLiquidLunch Please use the next available snapshot of main branch to verify and close.

@WowbaggersLiquidLunch
Copy link
Collaborator Author

Sorry for the delay. I just verified that this has been fixed as of the 2021-07-09 macOS toolchain

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

4 participants