diff --git a/tests/ui/strlen_on_c_strings.fixed b/tests/ui/strlen_on_c_strings.fixed new file mode 100644 index 00000000000..947a59bcc02 --- /dev/null +++ b/tests/ui/strlen_on_c_strings.fixed @@ -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(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() }; +} diff --git a/tests/ui/strlen_on_c_strings.rs b/tests/ui/strlen_on_c_strings.rs index a3451d992a0..1237f1ab03a 100644 --- a/tests/ui/strlen_on_c_strings.rs +++ b/tests/ui/strlen_on_c_strings.rs @@ -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(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()) }; } diff --git a/tests/ui/strlen_on_c_strings.stderr b/tests/ui/strlen_on_c_strings.stderr index 0dbecda62e3..296268a5f1d 100644 --- a/tests/ui/strlen_on_c_strings.stderr +++ b/tests/ui/strlen_on_c_strings.stderr @@ -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