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

Commit

Permalink
fix(solidity/core/slither-server): ran fmt and cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSwapFeeder committed Feb 14, 2024
1 parent 3992b5a commit 4c7582d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 49 deletions.
1 change: 0 additions & 1 deletion remove-me-ba2314d8e5b1421b91ce.txt

This file was deleted.

1 change: 0 additions & 1 deletion toolchains/solidity/core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion toolchains/solidity/core/crates/slither-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ colored = "2.0.4"
thiserror = "1.0.50"
glob = "0.3.1"
toml = "0.8.8"
once_cell = "1.19.0"
tokio-utils = "0.1.2"
tokio-util = "0.7.10"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum SlitherError {
#[error("Error while runing slither")]
#[error("Error while runing slither: {0}")]
Unknown(String),
#[error("Error while runing the slither command: {0}")]
IoCommandError(#[from] std::io::Error),
Expand Down
39 changes: 19 additions & 20 deletions toolchains/solidity/core/crates/slither-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ impl LanguageServer for Backend {
}
}));
self.client
.log_message(MessageType::INFO, "Finished initializing diagnostic receiver!")
.log_message(
MessageType::INFO,
"Finished initializing diagnostic receiver!",
)
.await;

let folders = params.workspace_folders;
Expand Down Expand Up @@ -132,7 +135,6 @@ impl LanguageServer for Backend {
)
.await;
self.analyze_file(file.text_document.uri).await

}

async fn did_open(&self, file: DidOpenTextDocumentParams) {
Expand Down Expand Up @@ -160,21 +162,21 @@ impl Backend {

async fn analyze_file(&self, file: Url) {
if self.is_in_libs(file.path()).await
|| self.is_in_tests(file.path()).await
|| !self.is_in_src(file.path()).await
{
self.client
.log_message(
MessageType::INFO,
format!(
"File '{}' is not a source solidity code file, skipping analysis.",
file.path()
),
)
.await;
return;
}
self.launch_slither(file).await
|| self.is_in_tests(file.path()).await
|| !self.is_in_src(file.path()).await
{
self.client
.log_message(
MessageType::INFO,
format!(
"File '{}' is not a source solidity code file, skipping analysis.",
file.path()
),
)
.await;
return;
}
self.launch_slither(file).await
}

async fn is_in_libs(&self, path: &str) -> bool {
Expand Down Expand Up @@ -202,9 +204,6 @@ impl Backend {
}

async fn is_in_tests(&self, path: &str) -> bool {
if path.ends_with(".t.sol") {
return true;
}
let state = self.data.lock().await;
for test in state.tests_paths.iter() {
let fsrc = format!("/{}/", test.replace("\"", ""));
Expand Down
35 changes: 18 additions & 17 deletions toolchains/solidity/core/crates/slither-server/src/slither.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
use crate::{error::SlitherError, utils::normalize_slither_path, types::SlitherResult};
use crate::{error::SlitherError, types::SlitherResult, utils::normalize_slither_path};
use std::process::Stdio;
use tokio::{io::AsyncReadExt, process::Command};
use tower_lsp::lsp_types::Diagnostic;


pub async fn parse_slither_out(uri: &str) -> Result<Vec<Diagnostic>, SlitherError> {
let mut results: Vec<Diagnostic> = Vec::new();

eprintln!("SLITHER STARTING");
let mut output = exec_slither(uri)?;
let out = match output.stdout.take() {
Some(out) => out,
None => return Err(SlitherError::Unknown("Failed to get slither output pipe".to_string())),
None => {
return Err(SlitherError::Unknown(
"Failed to get slither output pipe".to_string(),
))
}
};

let mut buffer = tokio::io::BufReader::new(out);
let mut dst = String::new();

output.wait().await?;
eprintln!("SLITHER FINISHED");

buffer.read_to_string(&mut dst).await?;
let json: Result<SlitherResult, serde_json::Error> =
serde_json::from_str(&dst);

let json: Result<SlitherResult, serde_json::Error> = serde_json::from_str(&dst);

match json {
Ok(json) => {
for detector in json.results.detectors {
Expand All @@ -39,16 +41,15 @@ pub async fn parse_slither_out(uri: &str) -> Result<Vec<Diagnostic>, SlitherErro
Ok(results)
}


fn exec_slither(uri: &str) -> Result<tokio::process::Child, std::io::Error> {
Command::new("slither")
.arg(normalize_slither_path(uri))
.arg("--exclude")
.arg("naming-convention")
.arg("--json")
.arg("-")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::null())
.spawn()
.arg(normalize_slither_path(uri))
.arg("--exclude")
.arg("naming-convention")
.arg("--json")
.arg("-")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::null())
.spawn()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::{Deserialize, Serialize};
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity as Severity, Position, Range};
use std::vec;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio_util::sync::CancellationToken;
use tower_lsp::lsp_types::*;
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity as Severity, Position, Range};

#[derive(Debug)]
pub struct SlitherDiag {
Expand Down
13 changes: 6 additions & 7 deletions toolchains/solidity/core/crates/slither-server/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::error::SlitherError;
use crate::SlitherData;
use glob::glob;
use std::error::Error;
use std::process::Command as StdCommand;
use glob::glob;


pub fn is_slither_installed() -> bool {
let output = StdCommand::new("slither").arg("--version").output();
Expand Down Expand Up @@ -84,12 +83,12 @@ pub fn find_foundry_toml_config(workspace: &str) -> Result<String, Box<dyn Error
let mut foundry_toml_path = String::new();
for entry in glob(&format!("{}/**/foundry.toml", workspace))? {
match entry {
Ok(path) => {
foundry_toml_path = path.display().to_string();
break;
}
Err(e) => eprintln!("{:?}", e),
Ok(path) => {
foundry_toml_path = path.display().to_string();
break;
}
Err(e) => eprintln!("{:?}", e),
}
}
if foundry_toml_path.is_empty() {
return Err(Box::new(SlitherError::FoundryTomlNotFound));
Expand Down

0 comments on commit 4c7582d

Please sign in to comment.