This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: PR #148 from feature/50-solidity-linter-rules/101-no-empty-blo…
…cks-rule-staging 101 - No Empty Blocks Rule
- Loading branch information
Showing
8 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,24 @@ | ||
use syn_solidity::{Block, Visit}; | ||
|
||
struct BlockVisitor { | ||
blocks: Vec<Block>, | ||
} | ||
|
||
impl BlockVisitor { | ||
pub fn new() -> Self { | ||
Self { blocks: Vec::new() } | ||
} | ||
} | ||
|
||
impl<'ast> Visit<'ast> for BlockVisitor { | ||
fn visit_block(&mut self, i: &Block) { | ||
self.blocks.push(i.clone()); | ||
syn_solidity::visit::visit_block(self, i); | ||
} | ||
} | ||
|
||
pub fn retrieve_block_nodes(ast: &syn_solidity::File) -> Vec<Block> { | ||
let mut visitor = BlockVisitor::new(); | ||
visitor.visit_file(ast); | ||
visitor.blocks | ||
} |
87 changes: 87 additions & 0 deletions
87
toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/empty_block.rs
This file contains 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,87 @@ | ||
use crate::linter::SolidFile; | ||
use crate::rules::types::*; | ||
use crate::types::*; | ||
use ast_extractor::{ | ||
retriever::{retrieve_block_nodes, retrieve_contract_nodes}, | ||
Spanned, | ||
}; | ||
|
||
// const DEFAULT_SEVERITY: &str = "warn"; | ||
const DEFAULT_MESSAGE: &str = "should not be an empty block"; | ||
pub const RULE_ID: &str = "empty-block"; | ||
|
||
pub struct EmptyBlock { | ||
_data: RuleEntry, | ||
} | ||
|
||
impl RuleType for EmptyBlock { | ||
fn diagnose(&self, _file: &SolidFile, _files: &[SolidFile]) -> Vec<LintDiag> { | ||
let mut res = Vec::new(); | ||
let _reports = check_empty_block(_file); | ||
for report in _reports.iter().flatten() { | ||
res.push(LintDiag { | ||
id: RULE_ID.to_string(), | ||
severity: Some(Severity::WARNING), | ||
range: report.clone(), | ||
code: None, | ||
source: None, | ||
message: DEFAULT_MESSAGE.to_string(), | ||
uri: _file.path.clone(), | ||
source_file_content: _file.content.clone(), | ||
}); | ||
} | ||
res | ||
} | ||
} | ||
|
||
fn check_empty_block(file: &SolidFile) -> Vec<Option<Range>> { | ||
let mut res: Vec<Option<Range>> = Vec::new(); | ||
|
||
let contracts = retrieve_contract_nodes(&file.data); | ||
for contract in contracts.iter() { | ||
if contract.body.is_empty() { | ||
res.push(Some(Range { | ||
start: Position { | ||
line: contract.span().start().line, | ||
character: contract.span().start().column + 1, | ||
}, | ||
end: Position { | ||
line: contract.span().end().line, | ||
character: contract.span().end().column, | ||
}, | ||
})); | ||
} | ||
} | ||
|
||
let blocks = retrieve_block_nodes(&file.data); | ||
for block in blocks.iter() { | ||
if block.stmts.is_empty() { | ||
res.push(Some(Range { | ||
start: Position { | ||
line: block.span().start().line, | ||
character: block.span().start().column + 1, | ||
}, | ||
end: Position { | ||
line: block.span().end().line, | ||
character: block.span().end().column, | ||
}, | ||
})); | ||
} | ||
} | ||
res | ||
} | ||
|
||
impl EmptyBlock { | ||
pub fn create(data: RuleEntry) -> Box<dyn RuleType> { | ||
let rule = EmptyBlock { _data: data }; | ||
Box::new(rule) | ||
} | ||
|
||
pub fn create_default() -> RuleEntry { | ||
RuleEntry { | ||
id: RULE_ID.to_string(), | ||
severity: Severity::WARNING, | ||
data: vec![], | ||
} | ||
} | ||
} |
This file contains 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
60 changes: 60 additions & 0 deletions
60
toolchains/solidity/linter/core/solidhunter-lib/testdata/EmptyBlock/.solidhunter.json
This file contains 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,60 @@ | ||
{ | ||
"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": "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": "empty-block", | ||
"severity": "WARNING", | ||
"data": [] | ||
} | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
toolchains/solidity/linter/core/solidhunter-lib/testdata/EmptyBlock/file.sol
This file contains 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,5 @@ | ||
pragma solidity 0.8.19; | ||
|
||
contract Test {} | ||
|
||
contract Test_b {} |
2 changes: 2 additions & 0 deletions
2
toolchains/solidity/linter/core/solidhunter-lib/testdata/EmptyBlock/findings.csv
This file contains 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,2 @@ | ||
empty-block:3:10:3:13 | ||
empty-block:5:10:5:15 |
This file contains 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 |
---|---|---|
|
@@ -157,5 +157,6 @@ test_directories! { | |
EventNameCamelCase, | ||
ConstNameSnakeCase, | ||
StateVisibility, | ||
EmptyBlock, | ||
GlobalImport | ||
} |