crytic / crytic/slither

Add detection for Unused User-Defined Error types

Open
#2,587 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
6.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

### 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.