From 524add843d15f9a706b95fdcc2a9822609c8f2d3 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 18 Apr 2021 00:08:35 +0200 Subject: [PATCH] more lints (#648) --- Cargo.lock | 7 +++++++ asyncgit/Cargo.toml | 1 + asyncgit/src/error.rs | 3 +++ asyncgit/src/lib.rs | 8 +------- asyncgit/src/progress.rs | 7 ++++--- asyncgit/src/status.rs | 6 +++--- asyncgit/src/sync/diff.rs | 8 ++++++-- asyncgit/src/sync/staging/stage_tracked.rs | 3 ++- 8 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 053dd3f7..8822a95b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,7 @@ name = "asyncgit" version = "0.14.0" dependencies = [ "crossbeam-channel", + "easy-cast", "git2", "invalidstring", "log", @@ -294,6 +295,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "easy-cast" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd102ee8c418348759919b83b81cdbdc933ffe29740b903df448b4bafaa348e" + [[package]] name = "either" version = "1.6.1" diff --git a/asyncgit/Cargo.toml b/asyncgit/Cargo.toml index d66e6346..6d59fd66 100644 --- a/asyncgit/Cargo.toml +++ b/asyncgit/Cargo.toml @@ -20,6 +20,7 @@ log = "0.4" thiserror = "1.0" url = "2.2" unicode-truncate = "0.2.0" +easy-cast = "0.4" [dev-dependencies] tempfile = "3.2" diff --git a/asyncgit/src/error.rs b/asyncgit/src/error.rs index e83bed75..ff1580f8 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -32,6 +32,9 @@ pub enum Error { #[error("TryFromInt error:{0}")] IntConversion(#[from] TryFromIntError), + + #[error("EasyCast error:{0}")] + EasyCast(#[from] easy_cast::Error), } pub type Result = std::result::Result; diff --git a/asyncgit/src/lib.rs b/asyncgit/src/lib.rs index be2fb9bd..b56ae405 100644 --- a/asyncgit/src/lib.rs +++ b/asyncgit/src/lib.rs @@ -16,15 +16,9 @@ #![deny(clippy::needless_update)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::must_use_candidate)] +#![allow(clippy::missing_errors_doc)] //TODO: get this in someday since expect still leads us to crashes sometimes // #![deny(clippy::expect_used)] -//TODO: -#![allow(clippy::missing_errors_doc)] -#![allow(clippy::too_many_lines)] -#![allow(clippy::cast_possible_wrap)] -#![allow(clippy::cast_sign_loss)] -#![allow(clippy::cast_possible_truncation)] -#![allow(clippy::cast_precision_loss)] pub mod cached; mod commit_files; diff --git a/asyncgit/src/progress.rs b/asyncgit/src/progress.rs index c96b4c71..2474c47b 100644 --- a/asyncgit/src/progress.rs +++ b/asyncgit/src/progress.rs @@ -1,5 +1,6 @@ //! +use easy_cast::{Conv, ConvFloat}; use std::cmp; /// @@ -12,9 +13,9 @@ pub struct ProgressPercent { impl ProgressPercent { /// pub fn new(current: usize, total: usize) -> Self { - let total = cmp::max(current, total) as f32; - let progress = current as f32 / total * 100.0; - let progress = progress as u8; + let total = f64::conv(cmp::max(current, total)); + let progress = f64::conv(current) / total * 100.0; + let progress = u8::conv_nearest(progress); Self { progress } } /// diff --git a/asyncgit/src/status.rs b/asyncgit/src/status.rs index d7fc58b6..c5441d4d 100644 --- a/asyncgit/src/status.rs +++ b/asyncgit/src/status.rs @@ -14,11 +14,11 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -fn current_tick() -> u64 { +fn current_tick() -> u128 { SystemTime::now() .duration_since(UNIX_EPOCH) .expect("time before unix epoch!") - .as_millis() as u64 + .as_millis() } #[derive(Default, Hash, Clone)] @@ -29,7 +29,7 @@ pub struct Status { /// #[derive(Default, Hash, Copy, Clone, PartialEq)] pub struct StatusParams { - tick: u64, + tick: u128, status_type: StatusType, include_untracked: bool, } diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index 950de8ef..35b0c2f3 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -6,6 +6,7 @@ use super::{ CommitId, }; use crate::{error::Error, error::Result, hash}; +use easy_cast::Conv; use git2::{ Delta, Diff, DiffDelta, DiffFormat, DiffHunk, DiffOptions, Patch, Repository, @@ -186,6 +187,8 @@ pub fn get_diff_commit( } /// +//TODO: refactor into helper type with the inline closures as dedicated functions +#[allow(clippy::too_many_lines)] fn raw_diff_to_file_diff<'a>( diff: &'a Diff, work_dir: &Path, @@ -216,8 +219,9 @@ fn raw_diff_to_file_diff<'a>( delta.old_file().size(), delta.new_file().size(), ); - res.size_delta = (res.sizes.1 as i64) - .saturating_sub(res.sizes.0 as i64); + //TODO: use try_conv + res.size_delta = (i64::conv(res.sizes.1)) + .saturating_sub(i64::conv(res.sizes.0)); } if let Some(hunk) = hunk { let hunk_header = HunkHeader::from(hunk); diff --git a/asyncgit/src/sync/staging/stage_tracked.rs b/asyncgit/src/sync/staging/stage_tracked.rs index c541ad46..efc348a0 100644 --- a/asyncgit/src/sync/staging/stage_tracked.rs +++ b/asyncgit/src/sync/staging/stage_tracked.rs @@ -6,6 +6,7 @@ use crate::{ patches::get_file_diff_patch_and_hunklines, utils::repo, }, }; +use easy_cast::Conv; use scopetime::scope_time; use std::path::Path; @@ -49,7 +50,7 @@ pub fn stage_lines( let blob_id = repo.blob(new_content.as_bytes())?; idx.id = blob_id; - idx.file_size = new_content.as_bytes().len() as u32; + idx.file_size = u32::try_conv(new_content.as_bytes().len())?; //TODO: can we simply use add_frombuffer? index.add(&idx)?;