public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: "Tamir Duberstein" <tamird@gmail.com>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nicolas Schier" <nicolas@fjasle.eu>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Brendan Higgins" <brendan.higgins@linux.dev>,
	"David Gow" <davidgow@google.com>, "Rae Moar" <rmoar@google.com>,
	"Bjorn Helgaas" <bhelgaas@google.com>
Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
	rust-for-linux@vger.kernel.org, linux-kselftest@vger.kernel.org,
	kunit-dev@googlegroups.com, linux-pci@vger.kernel.org
Subject: Re: [PATCH] rust: enable `clippy::ptr_as_ptr` lint
Date: Fri, 07 Mar 2025 17:04:20 +0000	[thread overview]
Message-ID: <D8A73PQELZV8.24M3P13CM0062@proton.me> (raw)
In-Reply-To: <D8A6YVP8B1UB.38AHIB0LMO85Y@proton.me>

On Fri Mar 7, 2025 at 5:58 PM CET, Benno Lossin wrote:
> On Fri Mar 7, 2025 at 5:41 PM CET, Tamir Duberstein wrote:
>> In Rust 1.51.0, Clippy introduced the `ignored_unit_patterns` lint [1]:
>
> You link to the `ptr_as_ptr` lint though, is this a typo?
>
>>> Though `as` casts between raw pointers are not terrible,
>>> `pointer::cast` is safer because it cannot accidentally change the
>>> pointer's mutability, nor cast the pointer to other types like `usize`.
>>
>> There are a few classes of changes required:
>> - Modules generated by bindgen are marked
>>   `#[allow(clippy::ptr_as_ptr)]`.
>> - Inferred casts (` as _`) are replaced with `.cast()`.
>> - Ascribed casts (` as *... T`) are replaced with `.cast::<T>()`.
>> - Multistep casts from references (` as *const _ as *const T`) are
>>   replaced with `let x: *const _ = &x;` and `.cast()` or `.cast::<T>()`
>>   according to the previous rules. The intermediate `let` binding is
>>   required because `(x as *const _).cast::<T>()` results in inference
>>   failure.
>> - Native literal C strings are replaced with `c_str!().as_char_ptr()`.
>
> These all seem very nice, thanks! I think it would also be a good idea
> to enable `ptr_cast_constness` [1], since those are the other kind of
> `as` usage that we should be doing via `cast_mut`/`cast_const`.

I didn't mean to make this sound like you *have* to do the work. If you
find the time and want to do it, then great, otherwise we can turn this
into a good-first-issue :)

---
Cheers,
Benno

> There are still some legitimate uses of `as` casts, when unsizing
> values. I don't know if these two lints can trigger on those, it would
> be nice if they don't. So those should continue to use `as`, but other
> than that, there shouldn't be any `as` in our code :)
>
> [1]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness
>
> (I will take a look at the patch itself a bit later)
>
> ---
> Cheers,
> Benno
>
>> Apply these changes and enable the lint -- no functional change
>> intended.
>>
>> Link: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr [1]
>> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
>> ---
>>  Makefile                               |  1 +
>>  rust/bindings/lib.rs                   |  1 +
>>  rust/kernel/alloc/allocator_test.rs    |  2 +-
>>  rust/kernel/alloc/kvec.rs              |  4 ++--
>>  rust/kernel/device.rs                  |  5 +++--
>>  rust/kernel/devres.rs                  |  2 +-
>>  rust/kernel/error.rs                   |  2 +-
>>  rust/kernel/fs/file.rs                 |  2 +-
>>  rust/kernel/kunit.rs                   | 15 +++++++--------
>>  rust/kernel/lib.rs                     |  4 ++--
>>  rust/kernel/list/impl_list_item_mod.rs |  2 +-
>>  rust/kernel/pci.rs                     |  2 +-
>>  rust/kernel/platform.rs                |  4 +++-
>>  rust/kernel/print.rs                   | 11 +++++------
>>  rust/kernel/seq_file.rs                |  3 ++-
>>  rust/kernel/str.rs                     |  2 +-
>>  rust/kernel/sync/poll.rs               |  2 +-
>>  rust/kernel/workqueue.rs               | 10 +++++-----
>>  rust/uapi/lib.rs                       |  1 +
>>  19 files changed, 40 insertions(+), 35 deletions(-)



  reply	other threads:[~2025-03-07 17:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-07 16:41 [PATCH] rust: enable `clippy::ptr_as_ptr` lint Tamir Duberstein
2025-03-07 16:58 ` Benno Lossin
2025-03-07 17:04   ` Benno Lossin [this message]
2025-03-07 17:07     ` Tamir Duberstein
2025-03-07 17:07   ` Tamir Duberstein
2025-03-07 18:27   ` Miguel Ojeda
2025-03-07 18:28     ` Tamir Duberstein
2025-03-07 21:31   ` Tamir Duberstein
2025-03-11 19:22 ` kernel test robot
2025-03-11 20:44   ` Tamir Duberstein
2025-03-11 20:49     ` Miguel Ojeda
2025-03-13  0:40       ` Philip Li
2025-03-14 15:22         ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=D8A73PQELZV8.24M3P13CM0062@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=brendan.higgins@linux.dev \
    --cc=dakr@kernel.org \
    --cc=davidgow@google.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=nathan@kernel.org \
    --cc=nicolas@fjasle.eu \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rmoar@google.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox