Skip to content

[Diagnostics] Warn when the result of a Void-returning function is ignored (by assigning into '_') #29576

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

Merged
merged 1 commit into from
Feb 4, 2020

Conversation

theblixguy
Copy link
Collaborator

Emit a warning if the user ignores the result of a Void-returning function by assigning into a discard expression, because it is redundant.

func returnsVoid() {}
_ = returnsVoid() // warning

Resolves SR-1553

@theblixguy theblixguy requested a review from xedin January 31, 2020 21:55
@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

2 similar comments
@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

Ok I need to update a test in SourceKit-LSP too...

@theblixguy
Copy link
Collaborator Author

swiftlang/sourcekit-lsp#221

@swift-ci please smoke test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this warning is really worse it since in warnings-as-errors setup everybody would get a bunch of new errors about this, which is seems so minor it doesn't really matter?...

@owenv
Copy link
Contributor

owenv commented Feb 3, 2020

@xedin I don’t think the compiler test suite is close enough to real-world code to determine the impact of this change since it uses _ = far more often than the average project. Maybe the source compatibility suite could better estimate the impact this would have on -warnings-as-errors users.

@xedin
Copy link
Contributor

xedin commented Feb 3, 2020

I'm not sure if there is way to run source compatibility in such mode. What I really question is whether such warning is useful or it's just noise and potential project failure source when compiled with warnings-as-errors.

@theblixguy
Copy link
Collaborator Author

theblixguy commented Feb 3, 2020

I think it’s a pretty minor warning since it only affects situations where the function returns Void, which I would assume is not that common. The bug report talks about a real scenario where this could’ve been useful. Personally I think this is similar to other redundancy warnings we have - it doesn’t affect anything, just warns about the redundancy... might be useful in some scenarios like the one in the SR.

@xedin
Copy link
Contributor

xedin commented Feb 3, 2020

Alright, fair enough.

@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@theblixguy theblixguy merged commit 543d649 into swiftlang:master Feb 4, 2020
@theblixguy theblixguy deleted the fix/SR-1553 branch February 4, 2020 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants