Skip to content
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

Add exceptions for i_ and s_ in the naming-conventions detector #2125

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion slither/detectors/naming_convention/naming_convention.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class NamingConvention(AbstractDetector):
def is_cap_words(name: str) -> bool:
return re.search("^[A-Z]([A-Za-z0-9]+)?_?$", name) is not None

@staticmethod
def is_immutable_naming(name: str) -> bool:
return re.search("^i_[a-z]([A-Za-z0-9]+)?_?$", name) is not None

@staticmethod
def is_state_naming(name: str) -> bool:
return re.search("^s_[a-z]([A-Za-z0-9]+)?_?$", name) is not None

@staticmethod
def is_mixed_case(name: str) -> bool:
return re.search("^[a-z]([A-Za-z0-9]+)?_?$", name) is not None
Expand Down Expand Up @@ -167,10 +175,22 @@ def _detect(self) -> List[Output]:
results.append(res)

else:
if var.visibility in ["private", "internal"]:
# first priority: check for immutable variable naming conventions
if var.is_immutable:
correct_naming = self.is_immutable_naming(var.name)

# second priority: check for state variable naming conventions
elif self.is_state_naming(var.name):
correct_naming = True
0xalpharush marked this conversation as resolved.
Show resolved Hide resolved

# third priority: check if visibility is private or internal and check for underscore mixed case
elif var.visibility in ["private", "internal"]:
correct_naming = self.is_mixed_case_with_underscore(var.name)

# fourth priority: check for mixed case naming conventions
else:
correct_naming = self.is_mixed_case(var.name)

if not correct_naming:
info = ["Variable ", var, " is not in mixedCase\n"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Struct naming.test (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#14-16) is not in CapWords

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#70) is not in mixedCase
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#71) is not in mixedCase

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#70) is single letter l, O, or I, which should not be used
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#71) is single letter l, O, or I, which should not be used

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#69) is not in mixedCase
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#70) is not in mixedCase

Variable naming.Var_One (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#11) is not in mixedCase

Expand All @@ -14,19 +14,19 @@ Contract naming (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_c

Enum naming.numbers (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#6) is not in CapWords

Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#60) is not in mixedCase
Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#61) is not in mixedCase

Variable T._myPublicVar (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#57) is not in mixedCase

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#69) is single letter l, O, or I, which should not be used
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#70) is single letter l, O, or I, which should not be used

Event naming.event_(uint256) (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#23) is not in CapWords

Modifier naming.CantDo() (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#41-43) is not in mixedCase

Function naming.GetOne() (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#30-33) is not in mixedCase

Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#68) is single letter l, O, or I, which should not be used
Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#69) is single letter l, O, or I, which should not be used

Parameter naming.setInt(uint256,uint256).Number2 (tests/e2e/detectors/test_data/naming-convention/0.4.25/naming_convention.sol#35) is not in mixedCase

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Struct naming.test (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#14-16) is not in CapWords

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#70) is not in mixedCase
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#71) is not in mixedCase

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#70) is single letter l, O, or I, which should not be used
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#71) is single letter l, O, or I, which should not be used

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#69) is not in mixedCase
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#70) is not in mixedCase

Variable naming.Var_One (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#11) is not in mixedCase

Expand All @@ -14,19 +14,19 @@ Contract naming (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_c

Enum naming.numbers (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#6) is not in CapWords

Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#60) is not in mixedCase
Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#61) is not in mixedCase

Variable T._myPublicVar (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#57) is not in mixedCase

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#69) is single letter l, O, or I, which should not be used
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#70) is single letter l, O, or I, which should not be used

Event naming.event_(uint256) (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#23) is not in CapWords

Modifier naming.CantDo() (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#41-43) is not in mixedCase

Function naming.GetOne() (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#30-33) is not in mixedCase

Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#68) is single letter l, O, or I, which should not be used
Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#69) is single letter l, O, or I, which should not be used

Parameter naming.setInt(uint256,uint256).Number2 (tests/e2e/detectors/test_data/naming-convention/0.5.16/naming_convention.sol#35) is not in mixedCase

Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
Struct naming.test (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#14-16) is not in CapWords
Struct naming.test (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#16-18) is not in CapWords

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#70) is not in mixedCase
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#73) is not in mixedCase

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#70) is single letter l, O, or I, which should not be used
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#73) is single letter l, O, or I, which should not be used

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#69) is not in mixedCase
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#72) is not in mixedCase

Variable naming.Var_One (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#11) is not in mixedCase
Variable naming.Var_One (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#13) is not in mixedCase

Constant naming.MY_other_CONSTANT (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#9) is not in UPPER_CASE_WITH_UNDERSCORES

Contract naming (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#3-48) is not in CapWords
Contract naming (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#3-50) is not in CapWords

Enum naming.numbers (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#6) is not in CapWords

Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#60) is not in mixedCase
Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#63) is not in mixedCase

Variable T._myPublicVar (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#57) is not in mixedCase
Variable T._myPublicVar (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#59) is not in mixedCase

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#69) is single letter l, O, or I, which should not be used
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#72) is single letter l, O, or I, which should not be used

Event naming.event_(uint256) (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#23) is not in CapWords
Event naming.event_(uint256) (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#25) is not in CapWords

Modifier naming.CantDo() (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#41-43) is not in mixedCase
Modifier naming.CantDo() (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#43-45) is not in mixedCase

Function naming.GetOne() (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#30-33) is not in mixedCase
Function naming.GetOne() (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#32-35) is not in mixedCase

Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#68) is single letter l, O, or I, which should not be used
Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#71) is single letter l, O, or I, which should not be used

Parameter naming.setInt(uint256,uint256).Number2 (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#35) is not in mixedCase
Parameter naming.setInt(uint256,uint256).Number2 (tests/e2e/detectors/test_data/naming-convention/0.6.11/naming_convention.sol#37) is not in mixedCase

Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
Struct naming.test (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#14-16) is not in CapWords
Struct naming.test (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#16-18) is not in CapWords

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#70) is not in mixedCase
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#73) is not in mixedCase

Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#70) is single letter l, O, or I, which should not be used
Variable T.I (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#73) is single letter l, O, or I, which should not be used

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#69) is not in mixedCase
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#72) is not in mixedCase

Variable naming.Var_One (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#11) is not in mixedCase
Variable naming.Var_One (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#13) is not in mixedCase

Constant naming.MY_other_CONSTANT (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#9) is not in UPPER_CASE_WITH_UNDERSCORES

Contract naming (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#3-48) is not in CapWords
Contract naming (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#3-50) is not in CapWords

Enum naming.numbers (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#6) is not in CapWords

Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#60) is not in mixedCase
Parameter T.test(uint256,uint256)._used (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#63) is not in mixedCase

Variable T._myPublicVar (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#57) is not in mixedCase
Variable T._myPublicVar (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#59) is not in mixedCase

Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#69) is single letter l, O, or I, which should not be used
Variable T.O (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#72) is single letter l, O, or I, which should not be used

Event naming.event_(uint256) (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#23) is not in CapWords
Event naming.event_(uint256) (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#25) is not in CapWords

Modifier naming.CantDo() (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#41-43) is not in mixedCase
Modifier naming.CantDo() (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#43-45) is not in mixedCase

Function naming.GetOne() (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#30-33) is not in mixedCase
Function naming.GetOne() (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#32-35) is not in mixedCase

Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#68) is single letter l, O, or I, which should not be used
Variable T.l (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#71) is single letter l, O, or I, which should not be used

Parameter naming.setInt(uint256,uint256).Number2 (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#35) is not in mixedCase
Parameter naming.setInt(uint256,uint256).Number2 (tests/e2e/detectors/test_data/naming-convention/0.7.6/naming_convention.sol#37) is not in mixedCase

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ contract T {
uint private _myPrivateVar;
uint internal _myInternalVar;
uint public _myPublicVar;

uint public s_myStateVar;
uint public myPublicVar;

function test(uint _unused, uint _used) public returns(uint){
return _used;}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ contract T {
uint private _myPrivateVar;
uint internal _myInternalVar;
uint public _myPublicVar;

uint public s_myStateVar;
uint public myPublicVar;

function test(uint _unused, uint _used) public returns(uint){
return _used;}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ contract naming {
uint constant MY_CONSTANT = 1;
uint constant MY_other_CONSTANT = 2;

uint public immutable i_myImutableVar = 1;

uint Var_One = 1;
uint varTwo = 2;

Expand Down Expand Up @@ -55,7 +57,8 @@ contract T {
uint private _myPrivateVar;
uint internal _myInternalVar;
uint public _myPublicVar;

uint public s_myStateVar;
uint public myPublicVar;

function test(uint _unused, uint _used) public returns(uint){
return _used;}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ contract naming {
uint constant MY_CONSTANT = 1;
uint constant MY_other_CONSTANT = 2;

uint public immutable i_myImutableVar = 1;

uint Var_One = 1;
uint varTwo = 2;

Expand Down Expand Up @@ -55,7 +57,8 @@ contract T {
uint private _myPrivateVar;
uint internal _myInternalVar;
uint public _myPublicVar;

uint public s_myStateVar;
uint public myPublicVar;

function test(uint _unused, uint _used) public returns(uint){
return _used;}
Expand Down
Binary file not shown.
Binary file not shown.