-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(forge-lint): [claude] check for unwrapped modifiers #10967
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
Open
0xClandestine
wants to merge
5
commits into
foundry-rs:master
Choose a base branch
from
0xClandestine:feat/claude-unwrapped-modifier-lint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+244
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,59 @@ | ||
use super::UnwrappedModifierLogic; | ||
use crate::{ | ||
linter::{EarlyLintPass, LintContext}, | ||
sol::{Severity, SolLint}, | ||
}; | ||
use solar_ast::{ExprKind, ItemFunction, Stmt, StmtKind}; | ||
|
||
declare_forge_lint!( | ||
UNWRAPPED_MODIFIER_LOGIC, | ||
Severity::Gas, | ||
"unwrapped-modifier-logic", | ||
"modifier logic should be wrapped to avoid code duplication and reduce codesize" | ||
); | ||
|
||
impl<'ast> EarlyLintPass<'ast> for UnwrappedModifierLogic { | ||
fn check_item_function(&mut self, ctx: &LintContext<'_>, func: &'ast ItemFunction<'ast>) { | ||
// If not a modifier, skip. | ||
if !func.kind.is_modifier() { | ||
return; | ||
} | ||
|
||
// If modifier has no contents, skip. | ||
let Some(body) = &func.body else { return }; | ||
|
||
// If body contains unwrapped logic, emit. | ||
if body.iter().any(|stmt| !is_valid_stmt(stmt)) | ||
&& let Some(name) = func.header.name | ||
{ | ||
ctx.emit(&UNWRAPPED_MODIFIER_LOGIC, name.span); | ||
} | ||
} | ||
} | ||
|
||
fn is_valid_stmt(stmt: &Stmt<'_>) -> bool { | ||
match &stmt.kind { | ||
// If the statement is an expression, emit if not valid. | ||
StmtKind::Expr(expr) => is_valid_expr(expr), | ||
|
||
// If the statement is a placeholder, skip. | ||
StmtKind::Placeholder => true, | ||
|
||
// Disallow all other statements. | ||
_ => false, | ||
} | ||
} | ||
|
||
// TODO: Support library member calls like `Lib.foo` (throws false positives). | ||
fn is_valid_expr(expr: &solar_ast::Expr<'_>) -> bool { | ||
// If the expression is a call, continue. | ||
if let ExprKind::Call(func_expr, _) = &expr.kind | ||
&& let ExprKind::Ident(ident) = &func_expr.kind | ||
{ | ||
// If the call is a built-in control flow function, emit. | ||
return !matches!(ident.name.as_str(), "require" | "assert"); | ||
} | ||
|
||
// Disallow all other expressions. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title UnwrappedModifierLogicTest | ||
* @notice Test cases for the unwrapped-modifier-logic lint | ||
* @dev This lint helps optimize gas by preventing modifier code duplication. | ||
* Solidity inlines modifier code at each usage point instead of using jumps, | ||
* so any logic in modifiers gets duplicated, increasing deployment costs. | ||
*/ | ||
contract UnwrappedModifierLogicTest { | ||
mapping(address => bool) public isOwner; | ||
|
||
// Helpers | ||
|
||
function checkPublic(address sender) public { | ||
require(isOwner[sender], "Not owner"); | ||
} | ||
|
||
function checkPrivate(address sender) private { | ||
require(isOwner[sender], "Not owner"); | ||
} | ||
|
||
function checkInternal(address sender) internal { | ||
require(isOwner[sender], "Not owner"); | ||
} | ||
|
||
// Good patterns | ||
|
||
modifier empty() { | ||
_; | ||
} | ||
|
||
modifier publicFn() { | ||
checkPublic(msg.sender); | ||
_; | ||
} | ||
|
||
modifier privateFn() { | ||
checkPrivate(msg.sender); | ||
_; | ||
} | ||
|
||
modifier internalFn() { | ||
checkInternal(msg.sender); | ||
_; | ||
} | ||
|
||
modifier publicPrivateInternal(address owner0, address owner1, address owner2) { | ||
checkPublic(owner0); | ||
checkPrivate(owner1); | ||
checkInternal(owner2); | ||
_; | ||
} | ||
|
||
// Bad patterns | ||
|
||
modifier requireBuiltIn() { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(msg.sender); | ||
require(isOwner[msg.sender], "Not owner"); | ||
checkPrivate(msg.sender); | ||
_; | ||
checkInternal(msg.sender); | ||
} | ||
|
||
modifier assertBuiltIn() { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(msg.sender); | ||
assert(isOwner[msg.sender]); | ||
checkPrivate(msg.sender); | ||
_; | ||
checkInternal(msg.sender); | ||
} | ||
|
||
modifier conditionalRevert() { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(msg.sender); | ||
if (!isOwner[msg.sender]) { | ||
revert("Not owner"); | ||
} | ||
checkPrivate(msg.sender); | ||
_; | ||
checkInternal(msg.sender); | ||
} | ||
|
||
modifier assign(address sender) { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(sender); | ||
bool _isOwner = true; | ||
checkPrivate(sender); | ||
isOwner[sender] = _isOwner; | ||
_; | ||
checkInternal(sender); | ||
} | ||
|
||
modifier assemblyBlock(address sender) { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(sender); | ||
assembly { | ||
let x := sender | ||
} | ||
checkPrivate(sender); | ||
_; | ||
checkInternal(sender); | ||
} | ||
|
||
modifier uncheckedBlock(address sender) { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(sender); | ||
unchecked { | ||
sender; | ||
} | ||
checkPrivate(sender); | ||
_; | ||
checkInternal(sender); | ||
} | ||
|
||
event DidSomething(address who); | ||
|
||
modifier emitEvent(address sender) { //~NOTE: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
checkPublic(sender); | ||
emit DidSomething(sender); | ||
checkPrivate(sender); | ||
_; | ||
checkInternal(sender); | ||
} | ||
} |
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,56 @@ | ||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
58 | modifier requireBuiltIn() { | ||
| -------------- | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
66 | modifier assertBuiltIn() { | ||
| ------------- | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
74 | modifier conditionalRevert() { | ||
| ----------------- | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
84 | modifier assign(address sender) { | ||
| ------ | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
93 | modifier assemblyBlock(address sender) { | ||
| ------------- | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
103 | modifier uncheckedBlock(address sender) { | ||
| -------------- | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
||
note[unwrapped-modifier-logic]: modifier logic should be wrapped to avoid code duplication and reduce codesize | ||
--> ROOT/testdata/UnwrappedModifierLogic.sol:LL:CC | ||
| | ||
115 | modifier emitEvent(address sender) { | ||
| --------- | ||
| | ||
= help: https://book.getfoundry.sh/reference/forge/forge-lint#unwrapped-modifier-logic | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be simplified to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e75f687