Skip to content

Commit 51aa005

Browse files
authored
Merge pull request #38 from codewithzubair07/feat/uint8-vs-uint256-solidity-rule
feat(solidity): add uint8 vs uint256 gas optimization rule
2 parents e2edf58 + b15ae0d commit 51aa005

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

packages/rules/src/solidity/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod parser;
2+
pub mod uint8_vs_uint256;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::rule_engine::{Rule, RuleResult};
2+
use crate::solidity::parser::SolidityASTNode;
3+
4+
pub struct Uint8VsUint256Rule;
5+
6+
impl Rule for Uint8VsUint256Rule {
7+
fn id(&self) -> &'static str {
8+
"uint8-vs-uint256"
9+
}
10+
11+
fn description(&self) -> &'static str {
12+
"Using uint8 outside structs is often more gas-expensive than uint256 on EVM chains."
13+
}
14+
15+
fn analyze(&self, ast: &SolidityASTNode) -> Vec<RuleResult> {
16+
let mut results = Vec::new();
17+
18+
ast.walk(|node, parent| {
19+
// Match variable declarations
20+
if let SolidityASTNode::VariableDeclaration { type_name, location } = node {
21+
// Only uint8
22+
if type_name == "uint8" {
23+
// Ignore struct members
24+
if matches!(parent, Some(SolidityASTNode::StructDefinition { .. })) {
25+
return;
26+
}
27+
28+
results.push(RuleResult {
29+
rule_id: self.id(),
30+
message: "uint8 used outside a struct. Consider using uint256 for better gas efficiency.".to_string(),
31+
location: location.clone(),
32+
severity: "LOW",
33+
});
34+
}
35+
}
36+
});
37+
38+
results
39+
}
40+
}

0 commit comments

Comments
 (0)