[Clippy] Address most clippy issues in production code#1291
[Clippy] Address most clippy issues in production code#1291DanielEScherzer wants to merge 75 commits into
Conversation
Add safety documentation for `ApplyOptions::raw()` regarding using the raw pointer.
Add an implementation of the `Default` trait for `ApplyOptions` that delegates to `ApplyOptions::new()`.
Simplify `BlameHunk::summary()` to use the tuple variant `Some` rather than using a closure.
In the `std::io::Write` trait implementation for `BlobWriter`, use the helper function `std::io::Error::other()` for constructing an `std::io::Error` of kind `std::io::ErrorKind::other`.
Add the missing `# Safety` heading to the existing safety documentation for `Branches::from_raw()` and expand that documentation.
Remove the explicit dereferencing in `Buf::as_str()`.
Mark the `CloneLocal` enum as `#[non_exhaustive]` and remove the hidden `__Nonexhaustive` variant. Based on the libgit2 documentation of the `git_clone_local_t` type the `__Nonexhaustive` was not something that the upstream library used as an actual value.
Add the missing `# Safety` heading to the existing safety documentation for `CheckoutBuilder::configure()` and expand that documentation.
Remove needless lifetimes from the implementation of the `Convert` trait for - converting references (`&T`) to constant pointers (`*const T`) - converting mutable references (`&mut T`) to mutable pointers (`*mut T`)
Add an implementation of the `Default` trait for `CherrypickOptions` that delegates to `CherrypickOptions::new()`.
Simplify a few methods to use the tuple variant `Some` rather than using a closure: - `Commit::message_encoding()` - `Commit::summary()` - `Commit::body()`
Because the method with the confusing name (`ConfigEntries::next()`) is part of the public API of the crate, simply move the lint handling to that method with an `#[expect]` attribute. Doing so avoids breaking changes while also removing the `#![allow]` from the overall module, ensuring that new functions do not have confusing names.
Simplify `CredentialHelper::add_command()` using `str::strip_prefix()`.
Simplify `CredentialHelper::config_use_http_path()` by using if-let expressions with `Results` rather than calling `Result::ok()` and matching against the resulting `Option`.
Add safety documentation for `Cred::unwrap()`.
Simplify `CredentialHelper::url_key()` to avoid needless borrows.
…red.rs Simplify `CredentialHelper::execute_cmd()` to avoid a needless borrow.
Because the method with the confusing name (`Cred::default()`) is part of the public API of the crate, simply move the lint handling to that method with an `#[expect]` attribute. Doing so avoids breaking changes while also removing the `#![allow]` from the overall module, ensuring that new functions do not have confusing names.
Add actual documentation to the `Addition`, `Deletion`, and `HunkHeader` variants of the `DiffLineType` enum.
Add the missing `# Safety` heading to the existing safety documentation for `DiffOptions::raw()`. Add safety documentation for `DiffFindOptions::raw()`.
Add an implementation of the `Default` trait for `DiffPatchidOptions` that delegates to `DiffPatchidOptions::new()`.
Because the method that accepts too many arguments (`Email::from_diff()`) is part of the public API of the crate, simply move the lint handling to that method with an `#[expect]` attribute. Doing so avoids breaking changes while also removing the `#![allow]` from the overall module, ensuring that new functions do not have too many arguments.
Because the method with the confusing name (`Error::from_str()`) is part of the public API of the crate, simply move the lint handling to that method with an `#[expect]` attribute. Doing so avoids breaking changes while also removing the `#![allow]` from the overall module, ensuring that new functions do not have confusing names.
In the `std::io::Write` trait implementation for `Indexer`, use the helper function `std::io::Error::other()` for constructing an `std::io::Error` of kind `std::io::ErrorKind::other`.
Remove needless lifetimes from the `opt_bytes()` function.
Because the method with the confusing name (`ObjectType::from_str()`) is part of the public API of the crate, simply move the lint handling to that method with an `#[expect]` attribute. Doing so avoids breaking changes while also removing the `#![allow]` from the overall module, ensuring that new functions do not have confusing names. Since lints that are `#![allow]`ed in src/lib.rs are also allowed in other files, the removal of the `#![allow]` surfaces warnings in src/oid.rs. For now, allow the current violation at a module level, to be addressed shortly.
Add safety documentation for `MergeOptions::raw()`.
Simplify `MergeFileResult::path()` to use the tuple variant `Some` rather than using a closure.
Add new public methods `MessageTrailersStrs::is_empty()` and `MessageTrailersBytes::is_empty()` for checking if a message's trailers are empty. Internally, introduce `MessageTrailers::is_empty()` to handle the logic.
Add an implementation of the `Default` trait for `RepositoryInitOptions` that delegates to `RepositoryInitOptions::new()`.
Simplify `Repository::namespace()` to use the tuple variant `Some` rather than using a closure.
Because the method that accepts too many arguments (`Repository::diff_blobs()`) is part of the public API of the crate, simply move the lint handling to that method with an `#[expect]` attribute. Doing so avoids breaking changes while also removing the `#![allow]` from the overall module, ensuring that new functions do not have too many arguments.
In `Repository::revert()` use `std::ptr::null()` for the revert options pointer passed to `git_revert()` if no options are provided by the caller.
Add an implementation of the `Default` trait for `RevertOptions` that delegates to `RevertOptions::new()`.
Add the missing `# Safety` heading to the existing safety documentation for `StashSaveOptions::raw()`.
Add the missing `# Safety` heading to the existing safety documentation for `StatusOptions::raw()`.
Simplify `StringArray::get()` to use the tuple variant `Some` rather than using a closure.
…dule.rs Add an empty comment line (`///`) between the paragraphs of the documentation for `Submodule::repo_init()`.
Remove the unneeded assignment in `Submodule::raw()`.
Simplify a few methods to use the tuple variant `Some` rather than using a closure: - `Submodule::branch()` - `Submodule::url()`
Remove the `#![allow]` attribute, apparently something has changed since I initially added it and clippy no longer complains.
Simplify `Tag::message()` to use the tuple variant `Some` rather than using a closure.
Add the missing `# Safety` heading to the existing safety documentation for `register()`.
…port.rs Add the missing annotations to: - `subtransport_action()` - `subtransport_free()` - `stream_free()`
Convert the implementations of conversions from `TreeWalkResult` to `i32` and `raw::git_treewalk_mode` from the `Into` trait to the `From` trait.
Simplify `treewalk_cb()` to use `Option::unwrap_or()`.
Add safety documentation for `Binding::from_raw()` and `Binding::from_raw_opt()`.
Remove needless lifetimes from the `IntoCString` trait implementations for - references to clonable types implementing `IntoCString` (`&T`) - references to string slices - references to `std::path::Path` objects - references to `std::ffi::OsStr` objects - references to `u8` slices
fc89d15 to
88c7f78
Compare
Simplify the `fixup_windows_path()` function to iterate by mutable reference to the path bytes rather than using a range and reading and writing bytes by index.
88c7f78 to
b8a4250
Compare
Remove unneeded `return`s in `path_to_repo_path()`.
Add an implementation of the `Default` trait for `WorktreeAddOptions` that delegates to `WorktreeAddOptions::new()`. Add an implementation of the `Default` trait for `WorktreePruneOptions` that delegates to `WorktreePruneOptions::new()`.
Simplify `Worktree::name()` to use the tuple variant `Some` rather than using a closure.
b8a4250 to
e45e67c
Compare
|
For the claim about from implementations for TreeWalkResult: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=14aafe81106484b31590080fdd2386ae |
| } | ||
| } | ||
|
|
||
| impl<'cb> Default for ApplyOptions<'cb> { |
There was a problem hiding this comment.
I am actually not fond of this. This may not be useful if we don't really need it. And that was why rust-lang/cargo only turn on a subset of lint group https://github.com/rust-lang/cargo/blob/371a35405026fc16dd9968589f13cd746b96e024/Cargo.toml#L139-L145.
Some of them are too pedantic.
| } | ||
|
|
||
| #[test] | ||
| #[allow(clippy::explicit_auto_deref)] |
There was a problem hiding this comment.
I forgot in the last PR. we should use #[expect] instead for all #[allow].
| /// This method is unsafe as there is no guarantee that this structure will | ||
| /// outlive the provided checkout options. | ||
| /// outlive the provided checkout options. The caller must ensure that | ||
| /// `opts` does not outlive the `CheckoutBuilder`, and also that for any |
There was a problem hiding this comment.
Intra doc link would be nice:
[`CheckoutBuilder`]
|
|
||
| #[cfg(test)] | ||
| #[cfg(feature = "cred")] | ||
| #[allow(clippy::needless_borrows_for_generic_args)] |
| @@ -1,3 +1,5 @@ | |||
| #![allow(clippy::should_implement_trait)] | |||
There was a problem hiding this comment.
this gets removed later in the same PR
| match writepack.free { | ||
| Some(free) => free(self.raw), | ||
| None => (), | ||
| if let Some(free) = writepack.free { |
There was a problem hiding this comment.
Like, this one is pretty annoying. It generally doesn't really matter.
| @@ -1,3 +1,5 @@ | |||
| #![allow(clippy::should_implement_trait)] | |||
I didn't use expect because as I found out both here and on #1238 some lints might fire for one OS but not another
Should I remove the |
I am fine keeping them anyway. Just share with you my preference :)
This might be better, if not too many churns. |
Commits are split by file and lint for ease of review. In most cases, the lints are addressed by fixing the reported issue as suggested.
API changes
Notably, the following public API changes were made:
New
DefaultimplementationsThe following types now implement the
Defaulttrait:ApplyOptionsCherrypickOptionsDiffPatchidOptionsRepositoryInitOptionsRevertOptionsWorktreeAddOptionsWorktreePruneOptionsNew
is_empty()methodsThe following types have a new
is_empty()method to go along with the existinglen()method:MessageTrailersStrsMessageTrailersBytesOdbObjectRebaseOther
CloneLocalenum is marked as#[non_exhaustive], and the__Nonexhaustivevariant was removedIntotrait for convertingTreeWalkResulttoi32and toraw::git_treewalk_modewere replaced with implementations of theFromtraitLints expected
In a few places, addressing the clippy issues would have required breaking changes to the existing API. Accordingly, instead of addressing the issues, the module-level
#[allow]attributes were replaced with method-level#[expect]attributes, ensuring that new functions do not have the same issues while maintaining backwards compatibility. The follwing methods are expected to trigger clippy lints:clippy::should_implement_traitConfigEntries::next()Cred::default()Error::from_str()ObjectType::from_str()Oid::from_str()clippy::too_many_argumentsEmail::from_diff()Repository::diff_blobs()Remaining
#[allow]attributesThis PR does not address code for tests (code in
src/test.rs, code inmod testsinline modules, or code in thetestsdirectory). It also does not address code for examples (theexamples) directory. When a file-wide#[allow]turned out to also be needed for the inline test modules, it was added to the tests module.The library-wide
clippy::unnecessary_castallowing specified insrc/lib.rsis also not addressed. Given that some of the casts are needed on unix but not windows, and others are needed on windows but not unix, that lint requires further attention. A dedicated PR already exists at #1238 reduce the unnecessary casts.