Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore(clippy): Apply clippy lints #530

Merged
merged 5 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ const BUILD_ENV_VARS: &[&str] = &["CC", "CXX", "LD_LIBRARY_PATH", "PATH"];
fn codegen_language_map<T: ToString + Display>(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
}

Expand Down Expand Up @@ -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(())
}

Expand Down
30 changes: 22 additions & 8 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FromType, IntoType>(range: Range<FromType>) -> Range<IntoType>
where
FromType: TryInto<IntoType>,
<FromType as TryInto<IntoType>>::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<T: PartialEq>(
Expand All @@ -54,17 +66,19 @@ fn common_suffix_len<T: PartialEq>(
b: &[T],
b_range: Range<usize>,
) -> usize {
let mut l = 1;

let mut l: isize = 1;
let a_range: Range<isize> = convert_range(a_range);
let b_range: Range<isize> = 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::<usize>((a_range.end - l).try_into().unwrap())
== b.get_unchecked::<usize>((b_range.end - l).try_into().unwrap())
{
l += 1;
}
}
l - 1
(l - 1).try_into().unwrap()
}

/// The edit information representing a line
Expand Down Expand Up @@ -740,12 +754,12 @@ mod tests {
use test_case::test_case;

/// A convenience function to invoke the a Myers diff
fn myers_diff<'a, T>(a: &'a Vec<T>, b: &'a Vec<T>) -> Vec<EditType<&'a T>>
fn myers_diff<'a, T>(a: &'a [T], b: &'a [T]) -> Vec<EditType<&'a T>>
where
T: 'a + Eq + Debug,
{
let myers = Myers::default();
myers.diff(&a[..], &b[..])
myers.diff(a, b)
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/input_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}

Expand Down Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions src/neg_idx_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ impl<T> NegIdxVec<T> {
/// 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
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ mod tests {
}
}

assert!(failures.is_empty(), "{:#?}", failures);
assert!(failures.is_empty(), "{failures:#?}");
}

#[cfg(feature = "dynamic-grammar-libs")]
Expand Down
12 changes: 6 additions & 6 deletions src/render/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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());
Expand All @@ -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(())
}

Expand Down