]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/strlen_on_c_strings.rs
Rollup merge of #102072 - scottmcm:ptr-alignment-type, r=thomcc
[rust.git] / src / tools / clippy / tests / ui / strlen_on_c_strings.rs
1 // run-rustfix
2
3 #![warn(clippy::strlen_on_c_strings)]
4 #![allow(dead_code)]
5 #![feature(rustc_private)]
6 extern crate libc;
7
8 #[allow(unused)]
9 use libc::strlen;
10 use std::ffi::{CStr, CString};
11
12 fn main() {
13     // CString
14     let cstring = CString::new("foo").expect("CString::new failed");
15     let _ = unsafe { libc::strlen(cstring.as_ptr()) };
16
17     // CStr
18     let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
19     let _ = unsafe { libc::strlen(cstr.as_ptr()) };
20
21     let _ = unsafe { strlen(cstr.as_ptr()) };
22
23     let pcstr: *const &CStr = &cstr;
24     let _ = unsafe { strlen((*pcstr).as_ptr()) };
25
26     unsafe fn unsafe_identity<T>(x: T) -> T {
27         x
28     }
29     let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
30     let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
31
32     let f: unsafe fn(_) -> _ = unsafe_identity;
33     let _ = unsafe { strlen(f(cstr).as_ptr()) };
34 }