Navigation Menu

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-11511] A returning value of Array<Void> should give a warning, like Optional<Void> #53912

Closed
BasThomas opened this issue Sep 24, 2019 · 7 comments
Labels
compiler The Swift compiler in itself good first issue Good for newcomers improvement

Comments

@BasThomas
Copy link
Contributor

Previous ID SR-11511
Radar None
Original Reporter @BasThomas
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, StarterBug
Assignee jder (JIRA)
Priority Medium

md5: ebcfa84ec27da83a255c80a10ed317da

Issue Description:

let optOne: Int? = 1
let y = optOne.map { _ in return () } // Constant 'y' inferred to have type '()?', which may be unexpected
let z = [""].map { _ in return () } // no warning.

// The expected warning would be:
// Constant 'z' inferred to have type '[()]', which may be unexpected

For more information see the thread in this tweet.

@belkadan
Copy link
Contributor

@xedin, @hborla, what do you think? Helpful or not worth it? Also, starter-bug-able or not?

@theblixguy
Copy link
Collaborator

Yeah, it's starter-bug worthy. We need to add another check here to account for an ArraySliceType with Void as base type.

@ZevEisenberg
Copy link
Contributor

Should it also give a suggestion, like putting an extra set of parentheses or writing Void explicitly to silence the warning? Or is there no reasonable case where you would actually intend to do this?

@theblixguy
Copy link
Collaborator

I think there should be a fix-it to insert an explicit type annotation, I can’t remember if we do it for the Optional<Void> case as well.

@belkadan
Copy link
Contributor

Looks like we don't today but we totally should.

<stdin>:2:5: warning: constant 'y' inferred to have type '()?', which may be unexpected
let y = x.map { print($0) }
    ^
<stdin>:2:5: note: add an explicit type annotation to silence this warning
let y = x.map { print($0) }
    ^

@swift-ci
Copy link
Collaborator

Comment by Jesse Rusak (JIRA)

I made a quick PR for these two ideas here #27797

Feedback very welcome!

@swift-ci
Copy link
Collaborator

Comment by Jesse Rusak (JIRA)

PR is now merged.

@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 good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

5 participants