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 detection for Unused User-Defined Error types #2587

Open
brianblank opened this issue Oct 10, 2024 · 0 comments
Open

Add detection for Unused User-Defined Error types #2587

brianblank opened this issue Oct 10, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@brianblank
Copy link

Describe the desired feature

If there exists a user-defined error type such as "Unauthorized" in the below example:

`// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract VendingMachine {
error Unauthorized(); // WARNING HERE ... DEFINED BUT NOT USED
address payable owner = payable(msg.sender);

function withdraw() public {
    owner.transfer(address(this).balance);
}
// ...

}`

the expectation is that the user-defined error type should either be leveraged somewhere in the code (see below example), or the user-defined error type should be removed from the code.

`// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract VendingMachine {
error Unauthorized();
address payable owner = payable(msg.sender);

function withdraw() public {
    if (msg.sender != owner)
        revert Unauthorized();   // WARNING GOES AWAY AS IT IS NOW USED

    owner.transfer(address(this).balance);
}
// ...

}`

The thought process is that if a user-defined error was defined but not used, then it's possible the developer missed insertion of a feature they intended to use.

This feature should work for also work for user-defined errors with parameters (the above example is of a user-defined error without parameters). An example of a user-defined error type with and without parameters can be found in the solidity documentation here: https://soliditylang.org/blog/2021/04/21/custom-errors/ .

It's possible that a user-defined error type can be used more than once, but slither should show a warning when it is never used.

Also note that solidity allows the user-defined error to be defined inside of , or outside of the contract. If the user-defined error is in a solidity file and defined outside of the contract, it should be used in that file, or there should be a way to silence the warning for files that are intended to be used as inclusions to other files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants
@brianblank and others