2023-04-20 14:37:15 +00:00
|
|
|
//@run-rustfix
|
2021-11-24 21:06:13 +00:00
|
|
|
|
2021-05-18 13:19:56 +00:00
|
|
|
#![warn(clippy::strlen_on_c_strings)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![feature(rustc_private)]
|
|
|
|
extern crate libc;
|
|
|
|
|
2021-11-24 21:06:13 +00:00
|
|
|
#[allow(unused)]
|
2021-11-20 01:02:49 +00:00
|
|
|
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");
|
2021-11-24 21:06:13 +00:00
|
|
|
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");
|
2021-11-24 21:06:13 +00:00
|
|
|
let _ = unsafe { libc::strlen(cstr.as_ptr()) };
|
2021-11-20 01:02:49 +00:00
|
|
|
|
2021-11-24 21:06:13 +00:00
|
|
|
let _ = unsafe { strlen(cstr.as_ptr()) };
|
2021-11-20 01:51:20 +00:00
|
|
|
|
|
|
|
let pcstr: *const &CStr = &cstr;
|
2021-11-24 21:06:13 +00:00
|
|
|
let _ = unsafe { strlen((*pcstr).as_ptr()) };
|
2021-11-20 01:51:20 +00:00
|
|
|
|
|
|
|
unsafe fn unsafe_identity<T>(x: T) -> T {
|
|
|
|
x
|
|
|
|
}
|
2021-11-24 21:06:13 +00:00
|
|
|
let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
|
|
|
|
let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
|
2021-11-20 01:51:20 +00:00
|
|
|
|
|
|
|
let f: unsafe fn(_) -> _ = unsafe_identity;
|
2021-11-24 21:06:13 +00:00
|
|
|
let _ = unsafe { strlen(f(cstr).as_ptr()) };
|
2021-05-18 13:19:56 +00:00
|
|
|
}
|