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

Commit

Permalink
fix(solidity/linter/core): corrected reason string end character
Browse files Browse the repository at this point in the history
chore(solidity/linter/core): removed useless default data for reason string
  • Loading branch information
0xmemorygrinder committed Oct 9, 2023
1 parent bccb3d5 commit 03b3702
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 36 deletions.
3 changes: 3 additions & 0 deletions libs/ast-extractor/src/retriever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ mod stmts;
pub use stmts::*;

pub use finder::*;

mod expr_call;
pub use expr_call::*;
64 changes: 64 additions & 0 deletions libs/ast-extractor/src/retriever/expr_call.rs
Original file line number Diff line number Diff line change
@@ -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<ExprCall>,
}

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<ExprCall> {
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);
}
}
1 change: 1 addition & 0 deletions libs/ast-extractor/tests/files/expr_call/empty.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contract Empty {}
8 changes: 8 additions & 0 deletions libs/ast-extractor/tests/files/expr_call/one.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract One {

function emptyfn() public { }

function emptyCall() public {
emptyCall();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -44,41 +41,11 @@ impl ReasonString {
}
}

fn get_call_expressions(ast_nodes: &ast_extractor::File) -> Vec<ExprCall> {
let mut res = Vec::new();
let mut calls: Vec<ExprCall> = 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<LintDiag> {
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,
Expand Down Expand Up @@ -147,7 +114,7 @@ impl ReasonString {
RuleEntry {
id: RULE_ID.to_string(),
severity: DEFAULT_SEVERITY,
data: vec![DEFAULT_LENGTH.to_string()],
data: vec![],
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
reason-string:8:37:8:133
reason-string:8:37:8:75
reason-string:11:8:11:15

0 comments on commit 03b3702

Please sign in to comment.