Skip to content

[Clippy] Address most clippy issues in production code#1291

Open
DanielEScherzer wants to merge 75 commits into
rust-lang:mainfrom
DanielEScherzer:clippy-fixes
Open

[Clippy] Address most clippy issues in production code#1291
DanielEScherzer wants to merge 75 commits into
rust-lang:mainfrom
DanielEScherzer:clippy-fixes

Conversation

@DanielEScherzer

@DanielEScherzer DanielEScherzer commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

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 Default implementations

The following types now implement the Default trait:

  • ApplyOptions
  • CherrypickOptions
  • DiffPatchidOptions
  • RepositoryInitOptions
  • RevertOptions
  • WorktreeAddOptions
  • WorktreePruneOptions

New is_empty() methods

The following types have a new is_empty() method to go along with the existing len() method:

  • MessageTrailersStrs
  • MessageTrailersBytes
  • OdbObject
  • Rebase

Other

  • the CloneLocal enum is marked as #[non_exhaustive], and the __Nonexhaustive variant was removed
  • the implementations of the Into trait for converting TreeWalkResult to i32 and to raw::git_treewalk_mode were replaced with implementations of the From trait

Lints 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_trait

  • ConfigEntries::next()
  • Cred::default()
  • Error::from_str()
  • ObjectType::from_str()
  • Oid::from_str()

clippy::too_many_arguments

  • Email::from_diff()
  • Repository::diff_blobs()

Remaining #[allow] attributes

This PR does not address code for tests (code in src/test.rs, code in mod tests inline modules, or code in the tests directory). It also does not address code for examples (the examples) 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_cast allowing specified in src/lib.rs is 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.

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.
@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Jul 15, 2026
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
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.
@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

For the claim about from implementations for TreeWalkResult: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=14aafe81106484b31590080fdd2386ae

@weihanglo weihanglo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This a huge work!

BTW, is it possible to convert all #[allow] to #[expect]?

View changes since this review

Comment thread src/apply.rs
}
}

impl<'cb> Default for ApplyOptions<'cb> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/buf.rs
}

#[test]
#[allow(clippy::explicit_auto_deref)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot in the last PR. we should use #[expect] instead for all #[allow].

Comment thread src/build.rs
/// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intra doc link would be nice:

[`CheckoutBuilder`]

Comment thread src/cred.rs

#[cfg(test)]
#[cfg(feature = "cred")]
#[allow(clippy::needless_borrows_for_generic_args)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[expect]

Comment thread src/oid.rs Outdated
@@ -1,3 +1,5 @@
#![allow(clippy::should_implement_trait)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[expect]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this gets removed later in the same PR

Comment thread src/odb.rs
match writepack.free {
Some(free) => free(self.raw),
None => (),
if let Some(free) = writepack.free {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like, this one is pretty annoying. It generally doesn't really matter.

Comment thread src/oid.rs Outdated
@@ -1,3 +1,5 @@
#![allow(clippy::should_implement_trait)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[expect]

@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

Thanks. This a huge work!

BTW, is it possible to convert all #[allow] to #[expect]?

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
Plus the new allows that are added here are either

  • moved from the file to the tests, and I will deal with soon
  • temporarily added to src/oid.rs and I deal with it already in this PR

Should I remove the Default implementations? Disable the lint entirely?

@weihanglo

Copy link
Copy Markdown
Member

Should I remove the Default implementations? Disable the lint entirely?

I am fine keeping them anyway. Just share with you my preference :)

moved from the file to the tests, and I will deal with soon

This might be better, if not too many churns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting on review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants