rust-clippy/tests/ui/strlen_on_c_strings.rs

33 lines
950 B
Rust
Raw Normal View History

2021-05-18 13:19:56 +00:00
#![warn(clippy::strlen_on_c_strings)]
2023-12-03 22:41:01 +00:00
#![allow(dead_code, clippy::manual_c_str_literals)]
2021-05-18 13:19:56 +00:00
#![feature(rustc_private)]
extern crate libc;
#[allow(unused)]
use libc::strlen;
2021-05-18 13:19:56 +00:00
use std::ffi::{CStr, CString};
fn main() {
// CString
let cstring = CString::new("foo").expect("CString::new failed");
let _ = unsafe { libc::strlen(cstring.as_ptr()) };
2021-05-18 13:19:56 +00:00
// 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()) };
2021-05-18 13:19:56 +00:00
}