Run rustfix for strlen_on_c_strings tests

This commit is contained in:
Jason Newcomb 2021-11-24 16:06:13 -05:00
parent 0d1f1cec44
commit a135347f5d
3 changed files with 65 additions and 28 deletions

View File

@ -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 _ = cstring.as_bytes().len();
// CStr
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
let _ = cstr.to_bytes().len();
let _ = cstr.to_bytes().len();
let pcstr: *const &CStr = &cstr;
let _ = unsafe { (*pcstr).to_bytes().len() };
unsafe fn unsafe_identity<T>(x: T) -> T {
x
}
let _ = unsafe { unsafe_identity(cstr).to_bytes().len() };
let _ = unsafe { unsafe_identity(cstr) }.to_bytes().len();
let f: unsafe fn(_) -> _ = unsafe_identity;
let _ = unsafe { f(cstr).to_bytes().len() };
}

View File

@ -1,31 +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 len = unsafe { libc::strlen(cstring.as_ptr()) };
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 len = unsafe { libc::strlen(cstr.as_ptr()) };
let _ = unsafe { libc::strlen(cstr.as_ptr()) };
let len = unsafe { strlen(cstr.as_ptr()) };
let _ = unsafe { strlen(cstr.as_ptr()) };
let pcstr: *const &CStr = &cstr;
let len = unsafe { strlen((*pcstr).as_ptr()) };
let _ = unsafe { strlen((*pcstr).as_ptr()) };
unsafe fn unsafe_identity<T>(x: T) -> T {
x
}
let len = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
let len = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
let f: unsafe fn(_) -> _ = unsafe_identity;
let len = unsafe { strlen(f(cstr).as_ptr()) };
let _ = unsafe { strlen(f(cstr).as_ptr()) };
}

View File

@ -1,46 +1,46 @@
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:12:15
--> $DIR/strlen_on_c_strings.rs:15:13
|
LL | let len = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`
LL | let _ = unsafe { libc::strlen(cstring.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`
|
= note: `-D clippy::strlen-on-c-strings` implied by `-D warnings`
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:16:15
--> $DIR/strlen_on_c_strings.rs:19:13
|
LL | let len = unsafe { libc::strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
LL | let _ = unsafe { libc::strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:18:15
--> $DIR/strlen_on_c_strings.rs:21:13
|
LL | let len = unsafe { strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
LL | let _ = unsafe { strlen(cstr.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:21:24
--> $DIR/strlen_on_c_strings.rs:24:22
|
LL | let len = unsafe { strlen((*pcstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(*pcstr).to_bytes().len()`
LL | let _ = unsafe { strlen((*pcstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(*pcstr).to_bytes().len()`
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:26:24
--> $DIR/strlen_on_c_strings.rs:29:22
|
LL | let len = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe_identity(cstr).to_bytes().len()`
LL | let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe_identity(cstr).to_bytes().len()`
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:27:15
--> $DIR/strlen_on_c_strings.rs:30:13
|
LL | let len = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe { unsafe_identity(cstr) }.to_bytes().len()`
LL | let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe { unsafe_identity(cstr) }.to_bytes().len()`
error: using `libc::strlen` on a `CString` or `CStr` value
--> $DIR/strlen_on_c_strings.rs:30:24
--> $DIR/strlen_on_c_strings.rs:33:22
|
LL | let len = unsafe { strlen(f(cstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `f(cstr).to_bytes().len()`
LL | let _ = unsafe { strlen(f(cstr).as_ptr()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `f(cstr).to_bytes().len()`
error: aborting due to 7 previous errors