Skip to content

Clippy: allow or fix clippy::unnecessary_cast lint in src/#1238

Open
DanielEScherzer wants to merge 2 commits intorust-lang:masterfrom
DanielEScherzer:clippy-unnecessary_cast
Open

Clippy: allow or fix clippy::unnecessary_cast lint in src/#1238
DanielEScherzer wants to merge 2 commits intorust-lang:masterfrom
DanielEScherzer:clippy-unnecessary_cast

Conversation

@DanielEScherzer
Copy link
Copy Markdown
Contributor

In a lot of places, the lint is triggered when casting a constant from the libgit2 bindings that is created (in libgit2-sys) via git_enum!. That macro will use i32 when #[cfg(target_env = "msvc")], and u32 otherwise, meaning that a number of the casts that clippy flags as "unnecessary" are indeed necessary. In those cases, use #[allow] with a reason.

Similarly, clippy also lints against casting of c_int and c_uint to i32 and u32 - while those are usually the same types, the libc documentation makes it clear that they are not always the same. In those cases too, use #[allow] with a reason.

The only libc-provided type with a consistent rust type is that size_t is always the same as usize - fix the places that clippy identified where that conversion is unneeded. Other fixes include casts when the underlying type is explicitly set in the libgit2 bindings to be a specific rust type, or when converting mutable pointers to the same type they already are.

After these changes, running the command
cargo clippy -- -A clippy::all -W clippy::unnecessary_cast no longer reports any hits.

In a lot of places, the lint is triggered when casting a constant from the
libgit2 bindings that is created (in libgit2-sys) via `git_enum!`. That macro
will use i32 when `#[cfg(target_env = "msvc")]`, and u32 otherwise, meaning
that a number of the casts that clippy flags as "unnecessary" are indeed
necessary. In those cases, use `#[allow]` with a reason.

Similarly, clippy also lints against casting of `c_int` and `c_uint` to `i32`
and `u32` - while those are usually the same types, the libc documentation
makes it clear that they are not always the same. In those cases too, use
`#[allow]` with a reason.

The only libc-provided type with a consistent rust type is that `size_t` is
always the same as `usize` - fix the places that clippy identified where that
conversion is unneeded. Other fixes include casts when the underlying type is
explicitly set in the libgit2 bindings to be a specific rust type, or when
converting mutable pointers to the same type they already are.

After these changes, running the command
`cargo clippy -- -A clippy::all -W clippy::unnecessary_cast` no longer reports
any hits.
@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Apr 22, 2026
@DanielEScherzer
Copy link
Copy Markdown
Contributor Author

This is the first of a few PRs to get clippy passing, and then I'll send a PR to add it to CI

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.

2 participants