From f04978fd3911cf46e91d30f9ac9075e62f10f62f Mon Sep 17 00:00:00 2001 From: Afnan Enayet Date: Sat, 7 Jan 2023 16:01:10 -0600 Subject: [PATCH 1/5] chore(clippy): Apply clippy lints This was done with: ```sh cargo clippy --fix -- -W clippy::pedantic ``` --- build.rs | 6 +++--- src/input_processing.rs | 2 +- src/main.rs | 8 ++++---- src/parse.rs | 2 +- src/render/unified.rs | 12 ++++++------ 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/build.rs b/build.rs index 93ba424f..1e0a0a6e 100644 --- a/build.rs +++ b/build.rs @@ -82,10 +82,10 @@ const BUILD_ENV_VARS: &[&str] = &["CC", "CXX", "LD_LIBRARY_PATH", "PATH"]; fn codegen_language_map(languages: &[T]) -> String { let body: String = languages .iter() - .map(|lang| format!("\"{}\" => tree_sitter_{},\n", lang, lang)) + .map(|lang| format!("\"{lang}\" => tree_sitter_{lang},\n")) .collect(); let map_decl = format!( - "\nstatic LANGUAGES: phf::Map<&'static str, unsafe extern \"C\" fn() -> Language> = phf_map! {{\n {}\n }};\n", body); + "\nstatic LANGUAGES: phf::Map<&'static str, unsafe extern \"C\" fn() -> Language> = phf_map! {{\n {body}\n }};\n"); map_decl } @@ -394,7 +394,7 @@ use phf::phf_map; // Write the generated code to a file in the resulting build directory let codegen_out_dir = env::var_os("OUT_DIR").unwrap(); let codegen_path = Path::new(&codegen_out_dir).join("generated_grammar.rs"); - fs::write(&codegen_path, codegen)?; + fs::write(codegen_path, codegen)?; Ok(()) } diff --git a/src/input_processing.rs b/src/input_processing.rs index 44ece3e5..0260beaf 100644 --- a/src/input_processing.rs +++ b/src/input_processing.rs @@ -71,7 +71,7 @@ pub struct VectorLeaf<'a> { pub text: &'a str, } -/// A proxy for (Point)[tree_sitter::Point] for [serde]. +/// A proxy for (Point)[`tree_sitter::Point`] for [serde]. /// /// This is a copy of an external struct that we use with serde so we can create json objects with /// serde. diff --git a/src/main.rs b/src/main.rs index ad61d953..dc2c3b2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -211,7 +211,7 @@ pub fn list_supported_languages() { println!("This program was compiled with support for:"); for language in languages { - println!("- {}", language); + println!("- {language}"); } } @@ -294,9 +294,9 @@ mod tests { /// Get paths to input files for tests fn get_test_paths(test_type: &str, test_name: &str, ext: &str) -> (PathBuf, PathBuf) { - let test_data_root = PathBuf::from(format!("./test_data/{}/{}", test_type, test_name)); - let path_a = test_data_root.join(format!("a.{}", ext)); - let path_b = test_data_root.join(format!("b.{}", ext)); + let test_data_root = PathBuf::from(format!("./test_data/{test_type}/{test_name}")); + let path_a = test_data_root.join(format!("a.{ext}")); + let path_b = test_data_root.join(format!("b.{ext}")); assert!( path_a.exists(), "test data path {} does not exist", diff --git a/src/parse.rs b/src/parse.rs index 15c84a1c..2fde17cd 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -367,7 +367,7 @@ mod tests { } } - assert!(failures.is_empty(), "{:#?}", failures); + assert!(failures.is_empty(), "{failures:#?}"); } #[cfg(feature = "dynamic-grammar-libs")] diff --git a/src/render/unified.rs b/src/render/unified.rs index 620c9d49..4db0d7d4 100644 --- a/src/render/unified.rs +++ b/src/render/unified.rs @@ -167,7 +167,7 @@ impl Unified { // We construct the fully horizontal title string. If wider than the terminal, then we // format another title string that's vertically stacked - let title_len = format!("{}{}{}", old_fname, divider, new_fname).len(); + let title_len = format!("{old_fname}{divider}{new_fname}").len(); // We only display the horizontal title format if we know we have enough horizontal space // to display it. If we can't determine the terminal width, play it safe and default to @@ -211,8 +211,8 @@ impl Unified { (styled_title_str, title_sep) } }; - writeln!(term, "{}", styled_title_str)?; - writeln!(term, "{}", title_sep)?; + writeln!(term, "{styled_title_str}")?; + writeln!(term, "{title_sep}")?; Ok(()) } @@ -260,9 +260,9 @@ impl Unified { // We don't need to display a range `x - x:` since `x:` is terser and clearer let title_str = if last_line - first_line == 0 { - format!("\n{}:", first_line) + format!("\n{first_line}:") } else { - format!("\n{} - {}:", first_line, last_line) + format!("\n{first_line} - {last_line}:") }; debug!("Title string has length of {}", title_str.len()); @@ -271,7 +271,7 @@ impl Unified { // length of the string, which is why we call `trim()` let separator = HUNK_TITLE_SEPARATOR.repeat(title_str.trim().len()); writeln!(term, "{}", fmt.regular.0.apply_to(title_str))?; - writeln!(term, "{}", separator)?; + writeln!(term, "{separator}")?; Ok(()) } From c608114919629f2abce6cf66589ba08e1b11f6e0 Mon Sep 17 00:00:00 2001 From: Afnan Enayet Date: Sat, 7 Jan 2023 16:15:44 -0600 Subject: [PATCH 2/5] fix(diff): Safe numeric type conversions Implements safe type conversions to address clippy lints. --- src/diff.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 0b46f74f..281c0717 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -46,6 +46,18 @@ where pub new: T, } +/// Convert a range with into another numeric type. +/// +/// This will panic if the values cannot be converted to the target type. This is better than using +/// `as` because it will explicitly panic instead of silently wrapping around. +fn convert_range(range: Range) -> Range +where + FromType: TryInto, + >::Error: std::fmt::Debug, +{ + range.start.try_into().unwrap()..range.end.try_into().unwrap() +} + /// Find the length of the common suffix between the ranges specified for `a` and `b`. /// The ranges are assumed to be [inclusive, exclusive). fn common_suffix_len( @@ -54,17 +66,19 @@ fn common_suffix_len( b: &[T], b_range: Range, ) -> usize { - let mut l = 1; - + let mut l: isize = 1; + let a_range: Range = convert_range(a_range); + let b_range: Range = convert_range(b_range); unsafe { - while (a_range.end as isize) - (l as isize) >= a_range.start as isize - && (b_range.end as isize) - (l as isize) >= b_range.start as isize - && a.get_unchecked(a_range.end - l) == b.get_unchecked(b_range.end - l) + while a_range.end - l >= a_range.start + && b_range.end - l >= b_range.start + && a.get_unchecked::((a_range.end - l).try_into().unwrap()) + == b.get_unchecked::((b_range.end - l).try_into().unwrap()) { l += 1; } } - l - 1 + (l - 1).try_into().unwrap() } /// The edit information representing a line From 4607ece6fb3ccb19f01d19e25e7fba37c4f12b6e Mon Sep 17 00:00:00 2001 From: Afnan Enayet Date: Sat, 7 Jan 2023 16:18:24 -0600 Subject: [PATCH 3/5] chore(tests): Use slice type It's more generic. Addresses a clippy lint. --- src/diff.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff.rs b/src/diff.rs index 281c0717..d5f184d1 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -754,7 +754,7 @@ mod tests { use test_case::test_case; /// A convenience function to invoke the a Myers diff - fn myers_diff<'a, T>(a: &'a Vec, b: &'a Vec) -> Vec> + fn myers_diff<'a, T>(a: &'a [T], b: &'a [T]) -> Vec> where T: 'a + Eq + Debug, { From 6f169d98af8c7b330396b757a77602b9e352ff67 Mon Sep 17 00:00:00 2001 From: Afnan Enayet Date: Sat, 7 Jan 2023 16:22:53 -0600 Subject: [PATCH 4/5] chore: Fix numeric type conversions Will panic instead of silently failing or wrapping --- src/neg_idx_vec.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/neg_idx_vec.rs b/src/neg_idx_vec.rs index d7c347f9..08c311b4 100644 --- a/src/neg_idx_vec.rs +++ b/src/neg_idx_vec.rs @@ -51,16 +51,16 @@ impl NegIdxVec { /// If the index is less zero then the index will be transformed by adding `idx` to the offset /// so negative indices are relative to the end of the vector. fn idx_helper(&self, idx: i32) -> usize { - let len = self.len; + let len: i32 = self.len.try_into().unwrap(); let final_index = if idx >= 0 { - idx as usize + idx.try_into().unwrap() } else { - let offset_idx = (len as i32) + idx; + let offset_idx = len + idx; debug_assert!(offset_idx >= 0); - offset_idx as usize + offset_idx.try_into().unwrap() }; - debug_assert!(final_index < len); + debug_assert!(final_index < len.try_into().unwrap()); final_index } From f83550c71a14efac54c7c92b63b3cfdaf700a84b Mon Sep 17 00:00:00 2001 From: Afnan Enayet Date: Sat, 7 Jan 2023 16:24:18 -0600 Subject: [PATCH 5/5] chore(clippy): Use simpler invocation Done with clippy automatically --- src/diff.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff.rs b/src/diff.rs index d5f184d1..ff4bf926 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -759,7 +759,7 @@ mod tests { T: 'a + Eq + Debug, { let myers = Myers::default(); - myers.diff(&a[..], &b[..]) + myers.diff(a, b) } #[test]