From 8ac9dab01bf100a79155f2a9b19e87b244f3e43d Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 24 Mar 2024 11:55:00 +0100 Subject: [PATCH] refactor: minor tweaks found by Clippy (#639) --- src/file_utils.rs | 5 ++--- src/imports/ipynb.rs | 2 +- src/imports/shared.rs | 6 +++--- src/location.rs | 10 +++++----- src/visitor.rs | 6 +++--- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/file_utils.rs b/src/file_utils.rs index eb96bf53..af9e80fe 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -20,8 +20,7 @@ pub fn read_file(file_path: &str) -> PyResult { Ok(content) => Ok(content), Err(e) => match e.kind() { ErrorKind::NotFound => Err(PyFileNotFoundError::new_err(format!( - "File not found: '{}'", - file_path + "File not found: '{file_path}'", ))), ErrorKind::InvalidData => { let file = File::open(path).unwrap(); @@ -32,7 +31,7 @@ pub fn read_file(file_path: &str) -> PyResult { .unwrap_or_else(|| guess_encoding(&buffer)); read_with_encoding(&buffer, encoding) } - _ => Err(PyIOError::new_err(format!("An error occurred: '{}'", e))), + _ => Err(PyIOError::new_err(format!("An error occurred: '{e}'"))), }, } } diff --git a/src/imports/ipynb.rs b/src/imports/ipynb.rs index 25c4874d..596278bd 100644 --- a/src/imports/ipynb.rs +++ b/src/imports/ipynb.rs @@ -63,7 +63,7 @@ fn _extract_code_from_notebook_cells(cells: &[serde_json::Value]) -> String { let code_lines: Vec = cells .iter() .filter(|cell| cell["cell_type"] == "code") - .flat_map(|cell| cell["source"].as_array()) + .filter_map(|cell| cell["source"].as_array()) .flatten() .filter_map(|line| line.as_str()) .map(str::to_owned) diff --git a/src/imports/shared.rs b/src/imports/shared.rs index 6d79ffe0..1b048560 100644 --- a/src/imports/shared.rs +++ b/src/imports/shared.rs @@ -29,7 +29,7 @@ pub fn get_ast_from_file_content(file_content: &str) -> PyResult { } /// Iterates through an AST to identify and collect import statements, and returns them together with their -/// respective TextRange for each occurrence. +/// respective `TextRange` for each occurrence. pub fn extract_imports_from_ast(ast: Mod) -> HashMap> { let mut visitor = ImportVisitor::new(); @@ -62,7 +62,7 @@ pub fn convert_imports_with_textranges_to_location_objects( .column .get(); Location { - file: file_path.to_string(), + file: file_path.to_owned(), line: Some(start_line), column: Some(start_col), } @@ -73,7 +73,7 @@ pub fn convert_imports_with_textranges_to_location_objects( imports_with_locations } -/// Transforms a Rust HashMap containing import data into a Python dictionary suitable for Python-side consumption. +/// Transforms a Rust `HashMap` containing import data into a Python dictionary suitable for Python-side consumption. pub fn convert_to_python_dict( py: Python<'_>, imports_with_locations: FileToImportsMap, diff --git a/src/location.rs b/src/location.rs index c7e59d1b..489c5016 100644 --- a/src/location.rs +++ b/src/location.rs @@ -14,14 +14,14 @@ pub struct Location { #[pymethods] impl Location { #[new] - pub fn new(file: String, line: Option, column: Option) -> Self { - Location { file, line, column } + fn new(file: String, line: Option, column: Option) -> Self { + Self { file, line, column } } - fn __repr__(&self) -> PyResult { - Ok(format!( + fn __repr__(&self) -> String { + format!( "Location(file='{}', line={:?}, column={:?})", self.file, self.line, self.column - )) + ) } } diff --git a/src/visitor.rs b/src/visitor.rs index ca35ea5f..c3c0e429 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -10,7 +10,7 @@ pub struct ImportVisitor { impl ImportVisitor { pub fn new() -> Self { - ImportVisitor { + Self { imports: HashMap::new(), } } @@ -49,11 +49,11 @@ impl<'a> Visitor<'a> for ImportVisitor { } /// Extracts the top-level module name from a potentially nested module path. -/// e.g. when a module_name is `foo.bar`, this returns `foo`. +/// e.g. when a `module_name` is `foo.bar`, this returns `foo`. fn get_top_level_module_name(module_name: &str) -> String { module_name .split('.') .next() .unwrap_or(module_name) - .to_string() + .to_owned() }