Skip to content

Commit 2ad6a94

Browse files
committed
[Lib] Fixed lint warnings
- Use slices instead of pointer arguments - Replace len() > 0 with !is_empty() - Swapped out expect(format!(...)) with unwrap_or_else(|| panic!(...)) - Remove self parameter and replace it with Self::find_fn_range(...)
1 parent 74c314c commit 2ad6a94

4 files changed

Lines changed: 16 additions & 17 deletions

File tree

src/call_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ pub struct Edge<'a> {
1414
}
1515

1616
impl<'a> CallGraph<'a> {
17-
pub fn new(sources: &'a mut Vec<CodeSource>) -> CallGraph<'a> {
17+
pub fn new(sources: &'a mut [CodeSource]) -> CallGraph<'a> {
1818
let edges = Self::find_edges(sources);
1919
CallGraph { edges }
2020
}
2121

22-
pub(crate) fn find_edges(sources: &'a mut Vec<CodeSource>) -> Vec<Edge<'a>> {
22+
pub(crate) fn find_edges(sources: &'a mut [CodeSource]) -> Vec<Edge<'a>> {
2323
let mut symbols = Vec::new();
2424
let edge_query = r#"
2525
(call_expression function: (identifier) @fn_name arguments: (arguments (_))*)

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub fn extract_variables<'a>(
129129
src_ref: &'a SourceRef,
130130
) -> HashMap<String, String> {
131131
let mut variables = HashMap::new();
132-
if src_ref.vars.len() > 0 {
132+
if !src_ref.vars.is_empty() {
133133
if let Some(captures) = src_ref.captures(&log_line) {
134134
for i in 0..captures.len() - 1 {
135135
variables.insert(
@@ -238,7 +238,7 @@ pub fn find_possible_paths<'a>(
238238
possible
239239
}
240240

241-
pub fn extract_logging<'a>(sources: &mut Vec<CodeSource>) -> Vec<SourceRef> {
241+
pub fn extract_logging(sources: &mut [CodeSource]) -> Vec<SourceRef> {
242242
let mut matched = Vec::new();
243243
for code in sources.iter_mut() {
244244
let src_query = SourceQuery::new(code);
@@ -326,7 +326,7 @@ fn nope(i: u32) {
326326
#[test]
327327
fn test_extract_logging() {
328328
let code = CodeSource::new(PathBuf::from("in-mem.rs"), Box::new(TEST_SOURCE.as_bytes()));
329-
let src_refs = extract_logging(&mut vec![code]);
329+
let src_refs = extract_logging(&mut [code]);
330330
assert_eq!(src_refs.len(), 2);
331331
let first = &src_refs[0];
332332
assert_eq!(first.line_no, 7);
@@ -349,7 +349,7 @@ fn nope(i: u32) {
349349
line: "[2024-02-15T03:46:44Z DEBUG stack] you're only as funky as your last cut",
350350
};
351351
let code = CodeSource::new(PathBuf::from("in-mem.rs"), Box::new(TEST_SOURCE.as_bytes()));
352-
let src_refs = extract_logging(&mut vec![code]);
352+
let src_refs = extract_logging(&mut [code]);
353353
assert_eq!(src_refs.len(), 2);
354354
let result = link_to_source(&log_ref, &src_refs);
355355
assert!(ptr::eq(result.unwrap(), &src_refs[0]));
@@ -362,10 +362,10 @@ fn nope(i: u32) {
362362
};
363363

364364
let code = CodeSource::new(PathBuf::from("in-mem.rs"), Box::new(TEST_SOURCE.as_bytes()));
365-
let src_refs = extract_logging(&mut vec![code]);
365+
let src_refs = extract_logging(&mut [code]);
366366
assert_eq!(src_refs.len(), 2);
367367
let result = link_to_source(&log_ref, &src_refs);
368-
assert_eq!(result.is_none(), true);
368+
assert!(result.is_none());
369369
}
370370

371371
#[test]
@@ -374,7 +374,7 @@ fn nope(i: u32) {
374374
line: "[2024-02-15T03:46:44Z DEBUG nope] this won't match i=1",
375375
};
376376
let code = CodeSource::new(PathBuf::from("in-mem.rs"), Box::new(TEST_SOURCE.as_bytes()));
377-
let src_refs = extract_logging(&mut vec![code]);
377+
let src_refs = extract_logging(&mut [code]);
378378
assert_eq!(src_refs.len(), 2);
379379
let vars = extract_variables(log_ref, &src_refs[1]);
380380
assert_eq!(vars.get("i").map(|val| val.as_str()), Some("1"));

src/log_format.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ impl LogFormat {
3232
}
3333

3434
pub fn captures<'a>(&self, log_ref: &LogRef<'a>) -> Captures<'a> {
35-
self.regex.captures(log_ref.line).expect(&format!(
36-
"Couldn't match `{}` with `{:?}`",
37-
log_ref.line, self.regex
38-
))
35+
self.regex
36+
.captures(log_ref.line)
37+
.unwrap_or_else(|| panic!("Couldn't match `{}` with `{:?}`", log_ref.line, self.regex))
3938
}
4039
}
4140

src/source_query.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a> SourceQuery<'a> {
2525
let language = code.ts_language();
2626
parser
2727
.set_language(&language)
28-
.expect(format!("Error loading {:?} grammar", language).as_str());
28+
.unwrap_or_else(|_| panic!("Error loading {:?} grammar", language));
2929
let source = code.buffer.as_str();
3030
let tree = parser.parse(source, None).expect("source is parsable");
3131
// println!("{:?}", tree.root_node().to_sexp());
@@ -50,7 +50,7 @@ impl<'a> SourceQuery<'a> {
5050
results.push(QueryResult {
5151
kind: String::from(capture.node.kind()),
5252
range: capture.node.range(),
53-
name_range: self.find_fn_range(capture.node),
53+
name_range: Self::find_fn_range(capture.node),
5454
});
5555
}
5656
}
@@ -59,7 +59,7 @@ impl<'a> SourceQuery<'a> {
5959
results
6060
}
6161

62-
fn find_fn_range(&self, node: Node) -> Range<usize> {
62+
fn find_fn_range(node: Node) -> Range<usize> {
6363
// println!("node.kind()={:?}", node.kind());
6464
match node.kind() {
6565
"function_item" => {
@@ -79,7 +79,7 @@ impl<'a> SourceQuery<'a> {
7979
range.start_byte..range.end_byte
8080
}
8181
_ => {
82-
let r = self.find_fn_range(node.parent().unwrap());
82+
let r = Self::find_fn_range(node.parent().unwrap());
8383
// println!("*****");
8484
r
8585
}

0 commit comments

Comments
 (0)