Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: disallow use of `CStr::as_ptr` and `CStr::from_ptr`

As kernel always use unsigned char and not the platform ABI's default, an
user should always use `as_char_ptr` provided via `CStrExt` instead.
Therefore configure `disallow-methods` feature of clippy to catch incorrect
usage.

Similarly, the dual `from_ptr` is also disallowed.

[ As an example, without the previous commit, we would get a warning like:

warning: use of a disallowed method `core::ffi::CStr::as_ptr`
--> rust/kernel/task.rs:422:54
|
422 | unsafe { crate::bindings::__might_sleep(file.as_ptr().cast(), loc.line() as i32) }
| ^^^^^^ help: kernel's `char` is always unsigned, use `as_char_ptr` instead: `kernel::prelude::CStrExt::as_char_ptr`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.94.0/index.html#disallowed_methods
= note: `-W clippy::disallowed-methods` implied by `-W clippy::all`
= help: to override `-W clippy::all` add `#[allow(clippy::disallowed_methods)]`

- Miguel ]

Signed-off-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Tamir Duberstein <tamird@kernel.org>
Link: https://patch.msgid.link/20260203130745.868762-2-gary@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Gary Guo and committed by
Miguel Ojeda
b3d161f2 1a933719

+13
+10
.clippy.toml
··· 9 9 # it here, see: https://github.com/rust-lang/rust-clippy/issues/11303. 10 10 { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool", allow-invalid = true }, 11 11 ] 12 + 13 + [[disallowed-methods]] 14 + path = "core::ffi::CStr::as_ptr" 15 + replacement = "kernel::prelude::CStrExt::as_char_ptr" 16 + reason = "kernel's `char` is always unsigned, use `as_char_ptr` instead" 17 + 18 + [[disallowed-methods]] 19 + path = "core::ffi::CStr::from_ptr" 20 + replacement = "kernel::prelude::CStrExt::from_char_ptr" 21 + reason = "kernel's `char` is always unsigned, use `from_char_ptr` instead"
+3
rust/kernel/str.rs
··· 189 189 // 190 190 // - error[E0379]: functions in trait impls cannot be declared const 191 191 #[inline] 192 + #[expect(clippy::disallowed_methods, reason = "internal implementation")] 192 193 pub const fn as_char_ptr_in_const_context(c_str: &CStr) -> *const c_char { 193 194 c_str.as_ptr().cast() 194 195 } ··· 320 319 321 320 impl CStrExt for CStr { 322 321 #[inline] 322 + #[expect(clippy::disallowed_methods, reason = "internal implementation")] 323 323 unsafe fn from_char_ptr<'a>(ptr: *const c_char) -> &'a Self { 324 324 // SAFETY: The safety preconditions are the same as for `CStr::from_ptr`. 325 325 unsafe { CStr::from_ptr(ptr.cast()) } ··· 336 334 } 337 335 338 336 #[inline] 337 + #[expect(clippy::disallowed_methods, reason = "internal implementation")] 339 338 fn as_char_ptr(&self) -> *const c_char { 340 339 self.as_ptr().cast() 341 340 }