From ba700c121e29f87101e2697dc6862d0fb75a7c79 Mon Sep 17 00:00:00 2001 From: 0xMemoryGrinder <35138272+0xMemoryGrinder@users.noreply.github.com> Date: Sun, 8 Oct 2023 20:31:23 -0400 Subject: [PATCH] fix(solidity/linter/core): corrected reason string end character chore(solidity/linter/core): removed useless default data for reason string --- libs/ast-extractor/src/retriever.rs | 3 + libs/ast-extractor/src/retriever/expr_call.rs | 64 +++++++++++++++++++ .../tests/files/expr_calls/one.sol | 8 +++ .../src/rules/best_practises/reason_string.rs | 37 +---------- .../testdata/ReasonString/findings.csv | 2 +- 5 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 libs/ast-extractor/src/retriever/expr_call.rs create mode 100644 libs/ast-extractor/tests/files/expr_calls/one.sol diff --git a/libs/ast-extractor/src/retriever.rs b/libs/ast-extractor/src/retriever.rs index 304d0fc5..896fbd61 100644 --- a/libs/ast-extractor/src/retriever.rs +++ b/libs/ast-extractor/src/retriever.rs @@ -28,3 +28,6 @@ mod stmts; pub use stmts::*; pub use finder::*; + +mod expr_call; +pub use expr_call::*; \ No newline at end of file diff --git a/libs/ast-extractor/src/retriever/expr_call.rs b/libs/ast-extractor/src/retriever/expr_call.rs new file mode 100644 index 00000000..62065000 --- /dev/null +++ b/libs/ast-extractor/src/retriever/expr_call.rs @@ -0,0 +1,64 @@ +/** + * expr_call.rs + * Function to retrieve expr calls from AST + * author: EnergyCube + * + * !!! UNTESTED !!! +*/ +use syn_solidity::{ExprCall, Visit}; + +struct CallVisitor { + calls: Vec, +} + +impl CallVisitor { + pub fn new() -> Self { + Self { calls: Vec::new() } + } +} + +impl<'ast> Visit<'ast> for CallVisitor { + fn visit_expr_call(&mut self, i: &ExprCall) { + self.calls.push(i.clone()); + syn_solidity::visit::visit_expr_call(self, i); + } +} + +pub fn retrieve_expr_call_nodes(ast: &syn_solidity::File) -> Vec { + let mut visitor = CallVisitor::new(); + visitor.visit_file(ast); + visitor.calls +} + +#[cfg(test)] +mod tests { + use proc_macro2::TokenStream; + + use super::*; + use std::fs; + use std::path::PathBuf; + use std::str::FromStr; + + #[test] + fn test_retrieve_expr_call_nodes_empty() { + let source = String::from("pragma solidity ^0.8.0;"); + let tokens = TokenStream::from_str(source.as_str()).unwrap(); + let ast = syn_solidity::parse2(tokens).unwrap(); + let res = retrieve_expr_call_nodes(&ast); + assert_eq!(res.len(), 0); + } + + #[test] + fn test_retrieve_expr_call_nodes_one() { + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + path.push("tests"); + path.push("files"); + path.push("expr_calls"); + path.push("one.sol"); + let source = fs::read_to_string(path).unwrap(); + let tokens = TokenStream::from_str(source.as_str()).unwrap(); + let ast = syn_solidity::parse2(tokens).unwrap(); + let res = retrieve_expr_call_nodes(&ast); + assert_eq!(res.len(), 1); + } +} \ No newline at end of file diff --git a/libs/ast-extractor/tests/files/expr_calls/one.sol b/libs/ast-extractor/tests/files/expr_calls/one.sol new file mode 100644 index 00000000..c7adfec1 --- /dev/null +++ b/libs/ast-extractor/tests/files/expr_calls/one.sol @@ -0,0 +1,8 @@ +contract One { + + function emptyfn() public { } + + function emptyCall() public { + emptyCall(); + } +} \ No newline at end of file diff --git a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs index 0a0b832a..13ab1c2b 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs +++ b/toolchains/solidity/linter/core/solidhunter-lib/src/rules/best_practises/reason_string.rs @@ -7,9 +7,6 @@ use crate::types::{LintDiag, Position, Range, Severity}; pub const RULE_ID: &str = "reason-string"; const DEFAULT_SEVERITY: Severity = Severity::WARNING; -// Specific -const DEFAULT_LENGTH: u32 = 32; - pub struct ReasonString { max_length: u32, data: RuleEntry, @@ -44,41 +41,11 @@ impl ReasonString { } } -fn get_call_expressions(ast_nodes: &ast_extractor::File) -> Vec { - let mut res = Vec::new(); - let mut calls: Vec = Vec::new(); - let contract = ast_nodes - .items - .iter() - .filter_map(|item| match item { - ast_extractor::Item::Contract(contract) => Some(contract), - _ => None, - }) - .next(); - - if let Some(contract) = contract { - res = ast_extractor::retriever::retrieve_functions_nodes(contract); - } - for func in res { - if let FunctionBody::Block(fn_body) = func.body { - for stmt in fn_body.stmts { - if let Stmt::Expr(stmt_expr) = stmt { - if let Expr::Call(call_expr) = stmt_expr.expr { - calls.push(call_expr); - } - } - } - } - } - calls -} - impl RuleType for ReasonString { fn diagnose(&self, file: &SolidFile, _files: &[SolidFile]) -> Vec { let mut res = Vec::new(); - let calls = get_call_expressions(&file.data); - for call_expr in calls { + for call_expr in retriever::retrieve_expr_call_nodes(&file.data) { let expr_require = match *call_expr.expr { Expr::Ident(require_ident) => require_ident, _ => continue, @@ -147,7 +114,7 @@ impl ReasonString { RuleEntry { id: RULE_ID.to_string(), severity: DEFAULT_SEVERITY, - data: vec![DEFAULT_LENGTH.to_string()], + data: vec![], } } } diff --git a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv index abf94805..d0c35af6 100644 --- a/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv +++ b/toolchains/solidity/linter/core/solidhunter-lib/testdata/ReasonString/findings.csv @@ -1,2 +1,2 @@ -reason-string:8:37:8:133 +reason-string:8:37:8:75 reason-string:11:8:11:15 \ No newline at end of file