initial round of pedantic clippy lint fixes

This commit is contained in:
Josh Junon 2024-03-04 12:58:41 +01:00
parent 79a857bb84
commit 5215317a7d
7 changed files with 31 additions and 13 deletions

View File

@ -43,6 +43,7 @@
//! [GitButler organization](https://github.com/gitbutlerapp)
//! or the [GitButler website](https://gitbutler.com).
#![deny(missing_docs)]
#![allow(clippy::doc_markdown, clippy::missing_errors_doc)]
#![feature(impl_trait_in_assoc_type, iter_map_windows, slice_as_chunks)]
mod linefile;

View File

@ -1,3 +1,5 @@
#![allow(clippy::module_name_repetitions)]
use crate::{CrlfBehavior, LineFile, LineSpan};
/// A [`LineFile`] stored in memory.
@ -7,12 +9,14 @@ pub struct MemoryLineFile {
impl MemoryLineFile {
/// Creates a new [`MemoryLineFile`] from the given lines.
#[must_use]
pub fn new(lines: Vec<String>) -> Self {
Self { lines }
}
/// Creates a new [`MemoryLineFile`] from the given text,
/// with the given CRLF behavior.
#[must_use]
pub fn from_str(text: &str, crlf_behavior: CrlfBehavior) -> Self {
MemoryLineFile {
lines: text
@ -34,6 +38,7 @@ impl<'a> LineFile<'a> for MemoryLineFile {
self.lines.len()
}
#[must_use]
fn extract(&'a self, span: LineSpan) -> Self::LineIterator {
self.lines[span.start()..=span.end()]
.iter()

View File

@ -1,3 +1,5 @@
#![allow(clippy::module_name_repetitions)]
use crate::{CrlfBehavior, LineFile};
use mmap_rs::{Error, Mmap};
@ -52,11 +54,7 @@ impl MmapLineFile {
if *c == b'\n' {
lines.push((
start,
i - if cr && crlf_behavior == CrlfBehavior::Trim {
1
} else {
0
},
i - usize::from(cr && crlf_behavior == CrlfBehavior::Trim),
));
(i + 1, false)
} else {

View File

@ -44,6 +44,7 @@ pub struct Signature([u8; TOTAL_BYTES]);
impl Signature {
/// Creates a new signature from a byte array.
#[inline]
#[must_use]
pub fn new(bytes: [u8; TOTAL_BYTES]) -> Self {
Self(bytes)
}
@ -54,6 +55,7 @@ impl Signature {
/// or assume anything about its contents. It is an
/// implementation detail and may change at any time.
#[inline]
#[must_use]
pub fn as_bytes(&self) -> &[u8; TOTAL_BYTES] {
&self.0
}
@ -76,10 +78,9 @@ impl Signature {
/// about the signature or the original file contents.
///
/// Do not use for any security-related purposes.
#[must_use]
pub fn score_str<S: AsRef<str>>(&self, other: S) -> f64 {
if self.0[0] != 0 {
panic!("unsupported signature version");
}
assert!(self.0[0] == 0, "unsupported signature version");
let original_length = u32::from_le_bytes(self.0[1..5].try_into().expect("invalid length"));
if original_length < 2 {
@ -108,7 +109,10 @@ impl Signature {
}
}
(2 * matching_bigrams) as f64 / (original_length as usize + other.len() - 2) as f64
#[allow(clippy::cast_precision_loss)]
{
(2 * matching_bigrams) as f64 / (original_length as usize + other.len() - 2) as f64
}
}
fn bucket_iter(&self) -> impl Iterator<Item = SigBucket> + '_ {

View File

@ -1,3 +1,5 @@
#![allow(clippy::module_name_repetitions)]
/// A line-based span of text.
///
/// All line spans are at least one line long.
@ -16,33 +18,35 @@ impl LineSpan {
/// # Panics
///
/// Panics if the start line is greater than the end line.
#[must_use]
pub fn new(start: usize, end: usize) -> Self {
if start > end {
panic!("start line cannot be greater than end line");
}
assert!(start <= end, "start line cannot be greater than end line");
Self { start, end }
}
/// The starting line of the span. Zero-based.
#[inline]
#[must_use]
pub fn start(&self) -> usize {
self.start
}
/// The ending line of the span. Zero-based, inclusive.
#[inline]
#[must_use]
pub fn end(&self) -> usize {
self.end
}
/// Gets the line count from the span
#[must_use]
pub fn line_count(&self) -> usize {
debug_assert!(self.end >= self.start);
self.end - self.start + 1
}
/// Returns true if the given span intersects with this span.
#[must_use]
pub fn intersects(&self, other: &Self) -> bool {
debug_assert!(self.end >= self.start);
debug_assert!(other.end >= other.start);
@ -57,6 +61,10 @@ impl LineSpan {
/// The final line ending (if any) is not included.
///
/// Also returns the character offsets (inclusive).
///
/// # Panics
/// Panics if the span's start > end.
#[must_use]
pub fn extract<'a>(&self, text: &'a str) -> Option<(&'a str, usize, usize)> {
debug_assert!(self.end >= self.start);

View File

@ -32,6 +32,7 @@ impl<T> Ord for Id<T> {
}
impl<T> Id<T> {
#[must_use]
pub fn generate() -> Self {
Id(Uuid::new_v4(), PhantomData)
}

View File

@ -7,5 +7,6 @@
//! For more information, see the
//! [GitButler organization](https://github.com/gitbutlerapp)
//! or the [GitButler website](https://gitbutler.com).
#![allow(clippy::doc_markdown)]
pub mod id;