diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/strlen_on_c_strings.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/tools/clippy/tests/ui/strlen_on_c_strings.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/strlen_on_c_strings.rs b/src/tools/clippy/tests/ui/strlen_on_c_strings.rs new file mode 100644 index 000000000..1237f1ab0 --- /dev/null +++ b/src/tools/clippy/tests/ui/strlen_on_c_strings.rs @@ -0,0 +1,34 @@ +// run-rustfix + +#![warn(clippy::strlen_on_c_strings)] +#![allow(dead_code)] +#![feature(rustc_private)] +extern crate libc; + +#[allow(unused)] +use libc::strlen; +use std::ffi::{CStr, CString}; + +fn main() { + // CString + let cstring = CString::new("foo").expect("CString::new failed"); + let _ = unsafe { libc::strlen(cstring.as_ptr()) }; + + // CStr + let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"); + let _ = unsafe { libc::strlen(cstr.as_ptr()) }; + + let _ = unsafe { strlen(cstr.as_ptr()) }; + + let pcstr: *const &CStr = &cstr; + let _ = unsafe { strlen((*pcstr).as_ptr()) }; + + unsafe fn unsafe_identity<T>(x: T) -> T { + x + } + let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) }; + let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) }; + + let f: unsafe fn(_) -> _ = unsafe_identity; + let _ = unsafe { strlen(f(cstr).as_ptr()) }; +} |