Skip to content

Commit 3b557e2

Browse files
authored
Add helper functions and docstrings (#18)
1 parent f9d5289 commit 3b557e2

File tree

19 files changed

+621
-294
lines changed

19 files changed

+621
-294
lines changed

compact-scanner/src/main.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![warn(clippy::pedantic)]
12
use clap::Parser;
23
use compact_security_detectors::all_detectors;
34
use compact_security_detectors_sdk::{
@@ -7,7 +8,7 @@ use compact_security_detectors_sdk::{
78
use libloading::{Library, Symbol};
89
use parser::Cli;
910
use serde_json::{json, Map};
10-
use std::collections::HashMap;
11+
use std::{collections::HashMap, path::PathBuf};
1112

1213
mod parser;
1314

@@ -46,17 +47,18 @@ fn main() {
4647
corpus.insert(path.to_string_lossy().to_string(), file_content);
4748
}
4849
}
50+
let mut files_scanned = Vec::new();
51+
let mut detector_responses = Map::new();
4952
if !corpus.is_empty() {
50-
let result = execute_detectors(&corpus, detectors, load_lib);
53+
let result = execute_detectors(&corpus, detectors.as_ref(), load_lib);
5154

52-
let files_scanned: Vec<String> = corpus
55+
files_scanned = corpus
5356
.keys()
54-
.map(|k| relative_file_path(k, &project_root))
57+
.map(|k| relative_file_path(k, project_root.as_ref()))
5558
.collect();
5659

57-
let mut detector_responses = Map::new();
5860
for (detector_name, errors) in result {
59-
let instances = detector_result_to_json(errors, &project_root);
61+
let instances = detector_result_to_json(errors, project_root.as_ref());
6062

6163
let detector_response = json!({
6264
"findings": [
@@ -69,15 +71,14 @@ fn main() {
6971
});
7072
detector_responses.insert(detector_name, detector_response);
7173
}
72-
73-
let res = json!({
74-
"errors": [],
75-
"scanned": files_scanned,
76-
"detector_responses": detector_responses,
77-
});
78-
79-
println!("{}", serde_json::to_string_pretty(&res).unwrap());
8074
}
75+
let res = json!({
76+
"errors": [],
77+
"scanned": files_scanned,
78+
"detector_responses": detector_responses,
79+
});
80+
81+
println!("{}", serde_json::to_string_pretty(&res).unwrap());
8182
}
8283
parser::Commands::Metadata => {
8384
println!("{}", get_scanner_metadata());
@@ -87,7 +88,7 @@ fn main() {
8788

8889
fn execute_detectors(
8990
files: &HashMap<String, String>,
90-
rules: Option<Vec<String>>,
91+
rules: Option<&Vec<String>>,
9192
load_lib: Option<std::path::PathBuf>,
9293
) -> HashMap<String, Vec<DetectorResult>> {
9394
let codebase = build_codebase(files).unwrap();
@@ -107,7 +108,7 @@ fn execute_detectors(
107108
let selected_detectors: Vec<_> = available_detectors()
108109
.into_iter()
109110
.filter(|detector| {
110-
if let Some(ref rules) = rules {
111+
if let Some(rules) = rules {
111112
rules.contains(&detector.id().to_string())
112113
|| (rules.len() == 1 && rules[0].eq_ignore_ascii_case("all"))
113114
} else {
@@ -127,7 +128,7 @@ fn execute_detectors(
127128

128129
fn detector_result_to_json(
129130
errors: Vec<DetectorResult>,
130-
project_root: &Option<std::path::PathBuf>,
131+
project_root: Option<&PathBuf>,
131132
) -> serde_json::Value {
132133
let mut json_errors = Vec::new();
133134
for error in errors {
@@ -145,7 +146,7 @@ fn detector_result_to_json(
145146
json!(json_errors)
146147
}
147148

148-
fn relative_file_path(file_path: &str, project_root: &Option<std::path::PathBuf>) -> String {
149+
fn relative_file_path(file_path: &str, project_root: Option<&PathBuf>) -> String {
149150
if let Some(root) = project_root {
150151
if let Ok(relative_path) = std::path::Path::new(file_path).strip_prefix(root) {
151152
relative_path.to_string_lossy().to_string()
@@ -186,7 +187,7 @@ fn get_scanner_metadata() -> String {
186187
"report": {
187188
"severity": detector.severity(),
188189
"tags": detector.tags(),
189-
"template": yml_string_to_json(detector.template())
190+
"template": yml_string_to_json(&detector.template())
190191
}
191192
});
192193
detectors.push(json_detector);
@@ -202,6 +203,6 @@ fn get_scanner_metadata() -> String {
202203
serde_json::to_string_pretty(&scanner_json).unwrap()
203204
}
204205

205-
fn yml_string_to_json(yml_string: String) -> Option<serde_json::Value> {
206-
serde_yaml::from_str::<serde_json::Value>(&yml_string).ok()
206+
fn yml_string_to_json(yml_string: &str) -> Option<serde_json::Value> {
207+
serde_yaml::from_str::<serde_json::Value>(yml_string).ok()
207208
}

detectors/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ fn main() {
104104
let closing = template["closing"].as_str().unwrap_or_default();
105105
let type_def = format!(
106106
r#"
107+
#[allow(clippy::manual_string_new)]
107108
impl DetectorReportTemplate for {type_name} {{
108109
fn id(&self) -> String {{ "{id}".to_string() }}
109110
fn uid(&self) -> String {{ "{uid}".to_string() }}
@@ -140,7 +141,7 @@ impl DetectorReportTemplate for {type_name} {{
140141

141142
fs::write(&mod_file_path, mods).unwrap();
142143
let mut register_code = String::from(
143-
"pub fn all_detectors() -> Vec<compact_security_detectors_sdk::detector::CompactDetector> {\n vec![\n",
144+
"#[must_use] pub fn all_detectors() -> Vec<compact_security_detectors_sdk::detector::CompactDetector> {\n vec![\n",
144145
);
145146
for type_name in &detector_type_names {
146147
register_code.push_str(&format!(" Box::new({type_name}),\n",));

detectors/src/array_loop_bound_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ compact_security_detectors_sdk::detector! {
4343
};
4444
errors.push(
4545
DetectorResult {
46-
file_path: codebase.find_node_file(index_access.id).unwrap().fname,
46+
file_path: codebase.find_node_file(index_access.id).unwrap().file_path,
4747
offset_start: index_access.location.offset_start,
4848
offset_end: index_access.location.offset_end,
4949
extra: {

detectors/src/assertion_error_message_verbose.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ compact_security_detectors_sdk::detector! {
1515
for assert_node in codebase.list_assert_nodes() {
1616
if assert_node
1717
.message()
18-
.map(|msg| msg.trim().is_empty() || msg.len() < 3)
19-
.unwrap_or(true)
18+
.is_none_or(|msg| msg.trim().is_empty() || msg.len() < 3)
2019
{
2120
let parent = codebase.get_parent_container(assert_node.id);
2221
let mut parent_type = "circuit";
@@ -29,7 +28,7 @@ compact_security_detectors_sdk::detector! {
2928
_ => String::new(),
3029
};
3130
errors.push(DetectorResult {
32-
file_path: codebase.find_node_file(assert_node.id).unwrap().fname,
31+
file_path: codebase.find_node_file(assert_node.id).unwrap().file_path,
3332
offset_start: assert_node.location.offset_start,
3433
offset_end: assert_node.location.offset_end,
3534
extra: {

detectors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![warn(clippy::pedantic)]
12
include!(concat!(env!("OUT_DIR"), "/mod_includes.rs"));
23
include!(concat!(env!("OUT_DIR"), "/detector_report_templates.rs"));
34
include!(concat!(env!("OUT_DIR"), "/register.rs"));

sdk/src/ast/builder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![warn(clippy::pedantic)]
21
use anyhow::{anyhow, bail, Ok, Result};
32
use std::rc::Rc;
43
use tree_sitter::Node;
@@ -2514,7 +2513,7 @@ mod tests {
25142513
let mut codebase = Codebase::new();
25152514
let ast = build_ast(&mut codebase, &root_node, content)?;
25162515
let source_code_file = SourceCodeFile {
2517-
fname: fname.to_string(),
2516+
file_path: fname.to_string(),
25182517
ast,
25192518
};
25202519
Ok(source_code_file)

sdk/src/ast/declaration.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![warn(clippy::pedantic)]
21
use std::rc::Rc;
32

43
use crate::{ast::expression::Expression, ast_enum, ast_nodes, ast_nodes_impl};
@@ -23,7 +22,6 @@ ast_enum! {
2322
@symbol Ledger(Rc<Ledger>),
2423
@scope Constructor(Rc<Constructor>),
2524
@scope Contract(Rc<Contract>),
26-
@raw Definition(Definition), //TODO: why is it here?
2725
@symbol PatternArgument(Rc<PatternArgument>),
2826
StructPatternField(Rc<StructPatternField>),
2927
}

sdk/src/ast/definition.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![warn(clippy::pedantic)]
21
use std::{rc::Rc, vec};
32

43
use crate::{ast::statement::Statement, ast_enum, ast_nodes, ast_nodes_impl};

sdk/src/ast/directive.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![warn(clippy::pedantic)]
21
use std::rc::Rc;
32

43
use crate::{ast_enum, ast_nodes, ast_nodes_impl};

sdk/src/ast/literal.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![warn(clippy::pedantic)]
21
use std::rc::Rc;
32

43
use crate::{ast_enum, ast_nodes, ast_nodes_impl};

0 commit comments

Comments
 (0)