Skip to content

[lib/Sema] Suggest return when the last statement would be a valid … #42415

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

Conversation

moritzdietsche
Copy link
Contributor

@moritzdietsche moritzdietsche commented Apr 16, 2022

…return value

When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.

Note: SR-10988 proposes to extend this behavior (for non-opaque and opaque return types) further by looking for valid return values in if statements. I do not fully understand the benefits of having this but will gladly provide an implementation if it is decided that such functionality is desired.

Resolves #53378

…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
@moritzdietsche
Copy link
Contributor Author

Thank you @LucianoPAlmeida for your review. I implemented the suggested changes in the latest commit.

…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
moritzdietsche and others added 3 commits April 20, 2022 07:43
…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
…statement-of-a-multi-statement-function-body-would-be-a-valid-return-value
@moritzdietsche moritzdietsche requested a review from xedin April 20, 2022 12:03
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 think the check makes sense. Could you please add some more tests where e.g. some type is a protocol composition and one case where result doesn't conform?

moritzdietsche and others added 3 commits April 22, 2022 22:29
…statement-of-a-multi-statement-function-body-would-be-a-valid-return-value
…statement-of-a-multi-statement-function-body-would-be-a-valid-return-value
…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
@xedin
Copy link
Contributor

xedin commented Apr 22, 2022

@swift-ci please test

@xedin
Copy link
Contributor

xedin commented Apr 22, 2022

Looks great now, thank you @moritzdietsche!

@moritzdietsche
Copy link
Contributor Author

Looks great now, thank you @moritzdietsche!

Thanks for your review and help!

@LucianoPAlmeida
Copy link
Contributor

@swift-ci Please test Linux Platform

@xedin
Copy link
Contributor

xedin commented Apr 23, 2022

@moritzdietsche Looks like you need to update one more test.

@LucianoPAlmeida
Copy link
Contributor

Ah, sorry @xedin ... I just looked the Jenkins and saw No identified problem so I thought it was some flaky failure and re-kicked it. Should've looked at the logs regardless

@xedin
Copy link
Contributor

xedin commented Apr 23, 2022

No worries! :)

…return value

Addresses SR-10988. When a multi-statement function body contains no return keyword but the last statement would be a valid return value, the compiler currently proposes a fixit to add the return keyword.
This changes extends this behavior to functions with opaque return types.
@moritzdietsche
Copy link
Contributor Author

@LucianoPAlmeida I think I'm ready for a new test run.

@LucianoPAlmeida
Copy link
Contributor

@swift-ci Please test

@xedin xedin merged commit 5d55ffe into swiftlang:main Apr 23, 2022
@moritzdietsche moritzdietsche deleted the SR-10988-Suggest-return-when-the-last-statement-of-a-multi-statement-function-body-would-be-a-valid-return-value branch April 24, 2022 07:01
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.

[SR-10988] Suggest return when the last statement of a multi-statement function body would be a valid return value
3 participants