argotorg / argotorg/solidity

Functions that contain "unnamed return variables" should trigger a compiler error and not a warning.

Open
#14,017 4 comments 0 reactions 0 assignees View on GitHub
breaking change :warning: needs design
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
2d 19h
Merged PRs (30d)
29

Description

## Description

I noticed that the compiler just throws a simple warning when your contract contains a function with "unnamed return variables" and that specific function does not have an explicit `return` statement.

After compiling the code that you can see in the "Steps to Reproduce" section, the compiler outputs this

```
➜ solidity-noerror-returns git:(master) ✗ forge build
[⠢] Compiling...
[⠒] Compiling 20 files with 0.8.19
[⠰] Solc 0.8.19 finished in 1.93s
Compiler run successful (with warnings)
warning[6321]: Warning: Unnamed return variable can remain unassigned. Add an explicit return with value to all non-reverting code paths or name the variable.
--> src/Contract.sol:8:76:
|
8 | function functionWithoutExplicitReturn(uint256 b) public view returns (uint256) {
| ^^^^^^^

warning[6321]: Warning: Unnamed return variable can remain unassigned. Add an explicit return with value to all non-reverting code paths or name the variable.
--> src/Contract.sol:12:82:
|
12 | function functionWithoutExplicitReturnIfCase(uint256 b) public view returns (uint256) {
| ^^^^^^^

```

I honestly can't think about a common use case where you define a function that must return some values, and it does not require an explicit return statement.

I think that this scenario should throw a compiler error and the developer must implement an explicit return statement to make the compilation finish successfully.

I can easily see cases where the developer forgets to add the `return` statement and the function just returns `0` (the default value for `uint256` type). The problem is even more aggravated when the code is more complex and contains branches like in the `functionWithoutExplicitReturnIfCase` where this error could be missed by the developer.

I think that functions with "Unnamed return variables" should always contain explicit `return` statements or `revert` for each possible branch.

## Environment

- Compiler version: 0.8.19
- Framework/IDE (e.g. Truffle or Remix): foundry
- Operating system: macOS

## Steps to Reproduce

Simply run `forge build`

```solidity
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.19;

// solhint-disable-next-line no-empty-blocks
contract Contract {
uint256 private a = 1;

function functionWithoutExplicitReturn(uint256 b) public view returns (uint256) {
uint256 c = a + b;
}

function functionWithoutExplicitReturnIfCase(uint256 b) public view returns (uint256) {
uint256 c = a + b;

if (c > 10) {
return 100;
}
}
}

```

## Conclusion

Functions with "Unnamed return variables" should always contain explicit `return` statements or `revert` for each possible branch.

If not, the compiler should throw an error and force the user to fix it.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the warning with the Solidity contract in the Steps to Reproduce section and `forge build`, then trace compiler handling of warning 6321. Done means the shown functions fail compilation unless every possible non-reverting path has an explicit return or reverts, rather than only emitting a warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, solidity
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.