Skip to content

Commit e9496f6

Browse files
fix(forge lint): do not flag fn test* and fn invariant* (#10607)
* fix: do not flag `fn test*` and `fn invariant*` * fix: ignore `fn statefulFuzz*` * test: ensure disabled * test: expand unit tests Co-authored-by: zerosnacks <[email protected]> * fix: wildcard on `invariant_` rather than `invariant` --------- Co-authored-by: zerosnacks <[email protected]>
1 parent 3f1e943 commit e9496f6

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

crates/lint/src/sol/info/mixed_case.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'ast> EarlyLintPass<'ast> for MixedCaseFunction {
1919
fn check_item_function(&mut self, ctx: &LintContext<'_>, func: &'ast ItemFunction<'ast>) {
2020
if let Some(name) = func.header.name {
2121
let name = name.as_str();
22-
if !is_mixed_case(name) && name.len() > 1 {
22+
if !is_mixed_case(name, true) {
2323
ctx.emit(&MIXED_CASE_FUNCTION, func.body_span);
2424
}
2525
}
@@ -42,7 +42,7 @@ impl<'ast> EarlyLintPass<'ast> for MixedCaseVariable {
4242
if var.mutability.is_none() {
4343
if let Some(name) = var.name {
4444
let name = name.as_str();
45-
if !is_mixed_case(name) {
45+
if !is_mixed_case(name, false) {
4646
ctx.emit(&MIXED_CASE_VARIABLE, var.span);
4747
}
4848
}
@@ -54,11 +54,17 @@ impl<'ast> EarlyLintPass<'ast> for MixedCaseVariable {
5454
///
5555
/// To avoid false positives like `fn increment()` or `uint256 counter`,
5656
/// lowercase strings are treated as mixedCase.
57-
pub fn is_mixed_case(s: &str) -> bool {
57+
pub fn is_mixed_case(s: &str, is_fn: bool) -> bool {
5858
if s.len() <= 1 {
5959
return true;
6060
}
6161

6262
// Remove leading/trailing underscores like `heck` does
63-
s.trim_matches('_') == format!("{}", heck::AsLowerCamelCase(s)).as_str()
63+
if s.trim_matches('_') == format!("{}", heck::AsLowerCamelCase(s)).as_str() {
64+
return true
65+
}
66+
67+
// Ignore `fn test*`, `fn invariant_*`, and `fn statefulFuzz*` patterns, as they usually contain
68+
// (allowed) underscores.
69+
is_fn && (s.starts_with("test") || s.starts_with("invariant_") || s.starts_with("statefulFuzz"))
6470
}

crates/lint/testdata/MixedCase.sol

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,37 @@ contract MixedCaseTest {
2626
function Functionmixedcase() public {} //~NOTE: function names should use mixedCase
2727
function FUNCTION_MIXED_CASE() public {} //~NOTE: function names should use mixedCase
2828
function FunctionMixedCase() public {} //~NOTE: function names should use mixedCase
29+
function function_mixed_case() public {} //~NOTE: function names should use mixedCase
30+
31+
// mixedCase checks are disabled for functions that starting with:
32+
// `test`, `invariant_`, and `statefulFuzz`
33+
function test_MixedCase_Disabled() public {}
34+
function test_mixedcase_disabled() public {}
35+
function testMixedCaseDisabled() public {}
36+
function testmixedcasedisabled() public {}
37+
38+
function testFuzz_MixedCase_Disabled() public {}
39+
function testFuzz_mixedcase_disabled() public {}
40+
function testFuzzMixedCaseDisabled() public {}
41+
function testfuzzmixedcasedisabled() public {}
42+
43+
function testRevert_MixedCase_Disabled() public {}
44+
function testRevert_mixedcase_disabled() public {}
45+
function testRevertMixedCaseDisabled() public {}
46+
function testrevertmixedcasedisabled() public {}
47+
48+
function invariant_MixedCase_Disabled() public {}
49+
function invariant_mixedcase_disabled() public {}
50+
function invariant_MixedCaseDisabled() public {}
51+
function invariant_mixedcasedisabled() public {}
52+
53+
function invariantBalance_MixedCase_Enabled() public {} //~NOTE: function names should use mixedCase
54+
function invariantbalance_mixedcase_enabled() public {} //~NOTE: function names should use mixedCase
55+
function invariantBalanceMixedCaseEnabled() public {}
56+
function invariantbalancemixedcaseenabled() public {}
57+
58+
function statefulFuzz_MixedCase_Disabled() public {}
59+
function statefulFuzz_mixedcase_disabled() public {}
60+
function statefulFuzzMixedCaseDisabled() public {}
61+
function statefulFuzzmixedcasedisabled() public {}
2962
}

crates/lint/testdata/MixedCase.stderr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,27 @@ note[mixed-case-function]: function names should use mixedCase
6464
|
6565
= help: https://docs.soliditylang.org/en/latest/style-guide.html#function-names
6666

67+
note[mixed-case-function]: function names should use mixedCase
68+
--> ROOT/testdata/MixedCase.sol:LL:CC
69+
|
70+
29 | function function_mixed_case() public {}
71+
| --
72+
|
73+
= help: https://docs.soliditylang.org/en/latest/style-guide.html#function-names
74+
75+
note[mixed-case-function]: function names should use mixedCase
76+
--> ROOT/testdata/MixedCase.sol:LL:CC
77+
|
78+
53 | function invariantBalance_MixedCase_Enabled() public {}
79+
| --
80+
|
81+
= help: https://docs.soliditylang.org/en/latest/style-guide.html#function-names
82+
83+
note[mixed-case-function]: function names should use mixedCase
84+
--> ROOT/testdata/MixedCase.sol:LL:CC
85+
|
86+
54 | function invariantbalance_mixedcase_enabled() public {}
87+
| --
88+
|
89+
= help: https://docs.soliditylang.org/en/latest/style-guide.html#function-names
90+

0 commit comments

Comments
 (0)