- Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
Description
Summary
The lint clippy::inherent_to_string
still applies even if the function is marked unsafe
, which obviously cannot then be impl ToString
.
The example provides a motivating reason for why the unsafe
is important: the struct is used across an FFI boundary.
Lint Name
inherent_to_string
Reproducer
Example src/lib.rs
:
use core::marker::PhantomData; use libc::{c_char, size_t}; #[repr(C)] pub struct StringView<'a> { ptr: *const c_char, len: size_t, _marker: PhantomData<&'a [c_char]>, } impl<'a> StringView<'a> { /// # Safety /// The view must follow all requirements of [std::slice::from_raw_parts]. pub unsafe fn to_string(self) -> String { let slice = std::slice::from_raw_parts(self.ptr as *const u8, self.len); String::from_utf8_lossy(slice).to_string() } }
And Cargo.toml
:
[package] name = "clippy_inherent_to_string" version = "0.1.0" edition = "2021" [dependencies] libc = "0.2.147"
I saw this happen:
warning: implementation of inherent method `to_string(&self) -> String` for type `StringView<'a>` --> src/lib.rs:14:5 | 14 | / pub unsafe fn to_string(self) -> String { 15 | | let slice = std::slice::from_raw_parts(self.ptr as *const u8, self.len); 16 | | String::from_utf8_lossy(slice).to_string() 17 | | } | |_____^ | = help: implement trait `Display` for type `StringView<'a>` instead = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string = note: `#[warn(clippy::inherent_to_string)]` on by default warning: `clippy_inherent_to_string` (lib) generated 1 warning Finished dev [unoptimized + debuginfo] target(s) in 0.00s
I expected no error, because unsafe
means this signature is not applicable for the trait ToString
.
Version
rustc 1.71.0 (8ede3aae2 2023-07-12) binary: rustc commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225 commit-date: 2023-07-12 host: aarch64-apple-darwin release: 1.71.0 LLVM version: 16.0.5
Additional Labels
No response
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have