Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
feat(solidity/linter/core): StateVisibility rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ByFishh authored and 0xmemorygrinder committed Oct 7, 2023
1 parent 5f66ff7 commit f3cf5fe
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 2 deletions.
1 change: 0 additions & 1 deletion remove-me-436d1796492c4a7b805d.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use std::collections::HashMap;

#[macro_use]
pub(crate) mod no_inline_assembly;
pub(crate) mod state_visibility;

// List all rules
use crate::rules::security::no_inline_assembly::NoInlineAssembly;
use crate::rules::security::state_visibility::StateVisibility;
use crate::rules::RuleBuilder;

pub fn create_default_rules() -> Vec<RuleEntry> {
vec![NoInlineAssembly::create_default()]
vec![NoInlineAssembly::create_default(), StateVisibility::create_default()]
}

pub fn create_rules() -> RulesMap {
Expand All @@ -20,5 +22,10 @@ pub fn create_rules() -> RulesMap {
NoInlineAssembly::create,
);

rules.insert(
state_visibility::RULE_ID.to_string(),
StateVisibility::create,
);

rules
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::linter::SolidFile;
use crate::rules::types::*;
use crate::types::*;
use ast_extractor::*;

pub const RULE_ID: &str = "state-visibility";
const MESSAGE: &str = "Explicitly mark visibility of state";

pub struct StateVisibility {
data: RuleEntry,
}

impl StateVisibility {
fn create_diag(
&self,
location: (ast_extractor::LineColumn, ast_extractor::LineColumn),
file: &SolidFile,
) -> LintDiag {
LintDiag {
id: RULE_ID.to_string(),
range: Range {
start: Position {
line: location.0.line,
character: location.0.column,
},
end: Position {
line: location.1.line,
character: location.1.column,
},
},
message: MESSAGE.to_string(),
severity: Some(self.data.severity),
code: None,
source: None,
uri: file.path.clone(),
source_file_content: file.content.clone(),
}
}
}

impl RuleType for StateVisibility {
fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> {
let mut res = Vec::new();
let contracts = retriever::retrieve_contract_nodes(&file.data);

for contract in contracts.iter() {
for node_var in contract.body.iter() {
if let Item::Variable(var) = node_var {
if var.attributes.visibility().is_none() {
let span = var.name.span();
res.push(self.create_diag((span.start(), span.end()), file));
}
}
}
}
res
}
}

impl StateVisibility {
pub(crate) fn create(data: RuleEntry) -> Box<dyn RuleType> {
let rule = StateVisibility { data };
Box::new(rule)
}

pub(crate) fn create_default() -> RuleEntry {
RuleEntry {
id: RULE_ID.to_string(),
severity: Severity::WARNING,
data: vec![],
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "solidhunter",
"includes": [],
"plugins": [],
"rules": [
{
"id": "line-max-len",
"severity": "WARNING",
"data": [
"80"
]
},
{
"id": "max-states-count",
"severity": "WARNING",
"data": [
"15"
]
},
{
"id": "function-max-lines",
"severity": "WARNING",
"data": [
"20"
]
},
{
"id": "reason-string",
"severity": "WARNING",
"data": [
"32"
]
},
{
"id": "contract-name-pascalcase",
"severity": "WARNING",
"data": []
},
{
"id": "func-name-camelcase",
"severity": "WARNING",
"data": []
},
{
"id": "func-param-name-camelcase",
"severity": "WARNING",
"data": []
},
{
"id": "use-forbidden-name",
"severity": "WARNING",
"data": []
},
{
"id": "import-on-top",
"severity": "WARNING",
"data": []
},
{
"id": "func-visibility",
"severity": "WARNING",
"data": [
"false"
]
},
{
"id": "state-visibility",
"severity": "WARNING",
"data": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity 0.8.0;

contract Test {
uint data;
uint public data;
uint private data;

string data = "test";
string public data = "test";
string private data = "test";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
state-visibility:4:9:4:13
state-visibility:8:11:8:15
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ test_directories! {
CustomErrors,
EventNameCamelCase
ConstNameSnakeCase,
StateVisibility,
}

0 comments on commit f3cf5fe

Please sign in to comment.