[Clippy] Address most clippy issues in production code#1291
Conversation
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].
|
|
||
| #[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. |
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.
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
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.
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.
e45e67c to
3aedab1
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
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.