-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(lint): add incorrect ERC20 interface lint #14428
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
Merged
mablr
merged 7 commits into
foundry-rs:master
from
figtracer:lint/incorrect-erc20-interface
Apr 24, 2026
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5b70bef
feat(lint): add incorrect ERC20 interface lint
figtracer 5e348c7
test(lint): cover direct-name IERC20 case
figtracer b7e7dfd
Merge branch 'master' into lint/incorrect-erc20-interface
zerosnacks 1cdeb39
Merge branch 'master' into lint/incorrect-erc20-interface
zerosnacks 8035434
fix(lint): revert IncorrectERC20Interface test to use empty IERC20 base
figtracer dd55d24
test: clarify incorrect ERC20 interface fixtures
figtracer 174aed1
test: align ERC20 fixture with ERC721
figtracer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use super::IncorrectERC20Interface; | ||
| use crate::{ | ||
| linter::{LateLintPass, LintContext}, | ||
| sol::{Severity, SolLint}, | ||
| }; | ||
| use solar::sema::hir; | ||
|
|
||
| declare_forge_lint!( | ||
| INCORRECT_ERC20_INTERFACE, | ||
| Severity::Med, | ||
| "incorrect-erc20-interface", | ||
| "incorrect ERC20 function interface" | ||
| ); | ||
|
|
||
| impl<'hir> LateLintPass<'hir> for IncorrectERC20Interface { | ||
| fn check_contract( | ||
| &mut self, | ||
| ctx: &LintContext, | ||
| hir: &'hir hir::Hir<'hir>, | ||
| contract: &'hir hir::Contract<'hir>, | ||
| ) { | ||
| // Check if the contract is a possible ERC20 by name or inheritance. | ||
| let is_erc20 = contract.linearized_bases.iter().any(|base_id| { | ||
| let name = hir.contract(*base_id).name.as_str(); | ||
| name == "ERC20" || name == "IERC20" | ||
| }); | ||
|
|
||
| if !is_erc20 { | ||
| return; | ||
| } | ||
|
|
||
| // If this contract implements a function from ERC721, we can assume it is an ERC721 token. | ||
| // These tokens offer functions which are similar to ERC20, but are not compatible. | ||
| let is_erc721 = contract.linearized_bases.iter().any(|base_id| { | ||
| let name = hir.contract(*base_id).name.as_str(); | ||
| name == "ERC721" || name == "IERC721" | ||
| }); | ||
|
|
||
| if is_erc721 { | ||
| return; | ||
| } | ||
|
|
||
| // Check each function in the contract for incorrect ERC20 signatures. | ||
| for item_id in contract.items { | ||
| let Some(fid) = item_id.as_function() else { continue }; | ||
| let func = hir.function(fid); | ||
|
|
||
| if !func.kind.is_function() { | ||
| continue; | ||
| } | ||
|
|
||
| let Some(name) = func.name else { continue }; | ||
|
|
||
| if has_incorrect_erc20_signature(hir, name.as_str(), func.parameters, func.returns) { | ||
| ctx.emit(&INCORRECT_ERC20_INTERFACE, func.span); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Checks if a function signature does not match the expected ERC20 specification. | ||
| /// | ||
| /// Returns `true` if the function name and parameter types match an ERC20 function but the return | ||
| /// types are incorrect. | ||
| fn has_incorrect_erc20_signature( | ||
| hir: &hir::Hir<'_>, | ||
| name: &str, | ||
| parameters: &[hir::VariableId], | ||
| returns: &[hir::VariableId], | ||
| ) -> bool { | ||
| let is_type = |var_id: hir::VariableId, type_str: &str| { | ||
| matches!( | ||
| &hir.variable(var_id).ty.kind, | ||
| hir::TypeKind::Elementary(ty) if ty.to_abi_str() == type_str | ||
| ) | ||
| }; | ||
|
|
||
| let params_match = |params: &[hir::VariableId], expected: &[&str]| -> bool { | ||
| params.len() == expected.len() | ||
| && params.iter().zip(expected).all(|(&id, &ty)| is_type(id, ty)) | ||
| }; | ||
|
|
||
| let returns_match = |rets: &[hir::VariableId], expected: &[&str]| -> bool { | ||
| rets.len() == expected.len() && rets.iter().zip(expected).all(|(&id, &ty)| is_type(id, ty)) | ||
| }; | ||
|
|
||
| match name { | ||
| // function transfer(address,uint256) external returns (bool) | ||
| "transfer" if params_match(parameters, &["address", "uint256"]) => { | ||
| !returns_match(returns, &["bool"]) | ||
| } | ||
| // function transferFrom(address,address,uint256) external returns (bool) | ||
| "transferFrom" if params_match(parameters, &["address", "address", "uint256"]) => { | ||
| !returns_match(returns, &["bool"]) | ||
| } | ||
| // function approve(address,uint256) external returns (bool) | ||
| "approve" if params_match(parameters, &["address", "uint256"]) => { | ||
| !returns_match(returns, &["bool"]) | ||
| } | ||
| // function allowance(address,address) external view returns (uint256) | ||
| "allowance" if params_match(parameters, &["address", "address"]) => { | ||
| !returns_match(returns, &["uint256"]) | ||
| } | ||
| // function balanceOf(address) external view returns (uint256) | ||
| "balanceOf" if params_match(parameters, &["address"]) => { | ||
| !returns_match(returns, &["uint256"]) | ||
| } | ||
| // function totalSupply() external view returns (uint256) | ||
| "totalSupply" if params_match(parameters, &[]) => !returns_match(returns, &["uint256"]), | ||
| _ => false, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| //@compile-flags: --severity high med low info | ||
|
|
||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.18; | ||
|
|
||
| interface IERC20 {} | ||
|
|
||
| // SHOULD FAIL: Interface named ERC20 with incorrect function signatures | ||
| interface ERC20 { | ||
| function transfer(address to, uint256 value) external returns (uint256); //~WARN: incorrect ERC20 function interface | ||
| function approve(address spender, uint256 value) external returns (uint256); //~WARN: incorrect ERC20 function interface | ||
| } | ||
|
|
||
| // SHOULD FAIL: Interface inheriting from IERC20 with incorrect function signatures | ||
| interface IERC20Incorrect is IERC20 { | ||
| function transfer(address to, uint256 value) external returns (uint256); //~WARN: incorrect ERC20 function interface | ||
| function transferFrom(address from, address to, uint256 value) external returns (uint256); //~WARN: incorrect ERC20 function interface | ||
| function approve(address spender, uint256 value) external returns (uint256); //~WARN: incorrect ERC20 function interface | ||
| function allowance(address owner, address spender) external view returns (bool); //~WARN: incorrect ERC20 function interface | ||
| function balanceOf(address account) external view returns (bool); //~WARN: incorrect ERC20 function interface | ||
| function totalSupply() external view returns (bool); //~WARN: incorrect ERC20 function interface | ||
| } | ||
|
|
||
| // SHOULD PASS: Correct ERC20 interface inheriting from IERC20 | ||
| interface IERC20Correct is IERC20 { | ||
| function transfer(address to, uint256 value) external returns (bool); | ||
| function transferFrom(address from, address to, uint256 value) external returns (bool); | ||
| function approve(address spender, uint256 value) external returns (bool); | ||
| function allowance(address owner, address spender) external view returns (uint256); | ||
| function balanceOf(address account) external view returns (uint256); | ||
| function totalSupply() external view returns (uint256); | ||
| } | ||
|
|
||
| // SHOULD PASS: Interface named IERC20 with correct function signatures | ||
| interface IERC20NamedCorrect { | ||
| function transfer(address to, uint256 value) external returns (bool); | ||
| function balanceOf(address account) external view returns (uint256); | ||
| } | ||
|
|
||
| // SHOULD PASS: Contract that is NOT named ERC20 and does not inherit from one | ||
| interface INotERC20 { | ||
| function transfer(address to, uint256 value) external returns (uint256); | ||
| function balanceOf(address account) external view returns (bool); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two take the same path: not
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| note[interface-naming]: interface names should be prefixed with 'I' | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface ERC20 { | ||
| │ ━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#interface-naming | ||
|
|
||
| note[multi-contract-file]: prefer having only one contract, interface or library per file | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface IERC20 {} | ||
| │ ━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#multi-contract-file | ||
|
|
||
| note[multi-contract-file]: prefer having only one contract, interface or library per file | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface ERC20 { | ||
| │ ━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#multi-contract-file | ||
|
|
||
| note[multi-contract-file]: prefer having only one contract, interface or library per file | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface IERC20Incorrect is IERC20 { | ||
| │ ━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#multi-contract-file | ||
|
|
||
| note[multi-contract-file]: prefer having only one contract, interface or library per file | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface IERC20Correct is IERC20 { | ||
| │ ━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#multi-contract-file | ||
|
|
||
| note[multi-contract-file]: prefer having only one contract, interface or library per file | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface IERC20NamedCorrect { | ||
| │ ━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#multi-contract-file | ||
|
|
||
| note[multi-contract-file]: prefer having only one contract, interface or library per file | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ interface INotERC20 { | ||
| │ ━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#multi-contract-file | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function transfer(address to, uint256 value) external returns (uint256); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function approve(address spender, uint256 value) external returns (uint256); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function transfer(address to, uint256 value) external returns (uint256); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function transferFrom(address from, address to, uint256 value) external returns (uint256); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function approve(address spender, uint256 value) external returns (uint256); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function allowance(address owner, address spender) external view returns (bool); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function balanceOf(address account) external view returns (bool); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
||
| warning[incorrect-erc20-interface]: incorrect ERC20 function interface | ||
| ╭▸ ROOT/testdata/IncorrectERC20Interface.sol:LL:CC | ||
| │ | ||
| LL │ function totalSupply() external view returns (bool); | ||
| │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| │ | ||
| ╰ help: https://book.getfoundry.sh/reference/forge/forge-lint#incorrect-erc20-interface | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.