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

Add compiler option for warn on implicit return statement. #2951

Closed
MicahZoltu opened this issue Sep 22, 2017 · 11 comments
Closed

Add compiler option for warn on implicit return statement. #2951

MicahZoltu opened this issue Sep 22, 2017 · 11 comments
Labels
closed due inactivity The issue/PR was automatically closed due to inactivity. stale The issue/PR was marked as stale because it has been open for too long.

Comments

@MicahZoltu
Copy link
Contributor

function fruit() returns (bytes32 fruitName) {
  fruitName = "apple";
}

This function does not result in an error or warning because an implicit return statement is added to the end of the function that returns whatever is in the fruitName local variable. In a large codebase this can lead to subtle bugs due to the implicit nature of the return statement. I would like the ability to set a compiler flag that allows me to make this a warning. Alternatively, making it always be a compiler warning is fine by me as I think it is not a good practice to have implicit return values.

@MicahZoltu
Copy link
Contributor Author

Note: I want to use the named return values because they get included in the ABI that the compiler generates which makes interacting with the contracts from external sources notably easier in some cases and it provides a certain amount of self-documentation.

@chriseth
Copy link
Contributor

Do I understand you correctly that this is about a warning if there is a code path that reaches the end of a function (which returns something) without an explicit return statement?

@chriseth
Copy link
Contributor

Perhaps we could have something like

function fruit() returns implicitly (bytes32 fruitName) {
  fruitName = "apple";
}

@redsquirrel
Copy link
Contributor

Instead of adding implicitly to the language, why not deprecate implicit returns?

@chriseth
Copy link
Contributor

@redsquirrel hm, perhaps I'm the only one who really likes them?

@roadriverrail
Copy link
Contributor

roadriverrail commented Sep 22, 2017 via email

@redsquirrel
Copy link
Contributor

redsquirrel commented Sep 22, 2017

especially where readability and security matter

See also: smart contracts 😉

@MicahZoltu
Copy link
Contributor Author

MicahZoltu commented Sep 25, 2017

I'm okay with returns implicitly or a pragma. In a perfect world, explicit would be the default and pragma/compiler options/keyword would be available if you wanted your compiler to be more lenient. In general my philosophy is that compilers should be ultra strict by default with options to be less strict rather than the other way around.

However, I will take anything I can get, and if that means the default is current behavior and I have to add a pragma or do returns explicitly (bytes32 fruitName) I can accept that and be happy.

@chriseth
Copy link
Contributor

@MicahZoltu oh of course explicit returns would be the default, otherwise the keyword would be returns explicitly.

@github-actions
Copy link

This issue has been marked as stale due to inactivity for the last 90 days.
It will be automatically closed in 7 days.

@github-actions github-actions bot added the stale The issue/PR was marked as stale because it has been open for too long. label Feb 13, 2023
@github-actions
Copy link

Hi everyone! This issue has been automatically closed due to inactivity.
If you think this issue is still relevant in the latest Solidity version and you have something to contribute, feel free to reopen.
However, unless the issue is a concrete proposal that can be implemented, we recommend starting a language discussion on the forum instead.

@github-actions github-actions bot added the closed due inactivity The issue/PR was automatically closed due to inactivity. label Feb 21, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed due inactivity The issue/PR was automatically closed due to inactivity. stale The issue/PR was marked as stale because it has been open for too long.
Projects
None yet
Development

No branches or pull requests

6 participants