Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a135347

Browse files
committed
Run rustfix for strlen_on_c_strings tests
1 parent 0d1f1ce commit a135347

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

tests/ui/strlen_on_c_strings.fixed

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 _ = cstring.as_bytes().len();
16+
17+
// CStr
18+
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
19+
let _ = cstr.to_bytes().len();
20+
21+
let _ = cstr.to_bytes().len();
22+
23+
let pcstr: *const &CStr = &cstr;
24+
let _ = unsafe { (*pcstr).to_bytes().len() };
25+
26+
unsafe fn unsafe_identity<T>(x: T) -> T {
27+
x
28+
}
29+
let _ = unsafe { unsafe_identity(cstr).to_bytes().len() };
30+
let _ = unsafe { unsafe_identity(cstr) }.to_bytes().len();
31+
32+
let f: unsafe fn(_) -> _ = unsafe_identity;
33+
let _ = unsafe { f(cstr).to_bytes().len() };
34+
}

tests/ui/strlen_on_c_strings.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1+
// run-rustfix
2+
13
#![warn(clippy::strlen_on_c_strings)]
24
#![allow(dead_code)]
35
#![feature(rustc_private)]
46
extern crate libc;
57

8+
#[allow(unused)]
69
use libc::strlen;
710
use std::ffi::{CStr, CString};
811

912
fn main() {
1013
// CString
1114
let cstring = CString::new("foo").expect("CString::new failed");
12-
let len = unsafe { libc::strlen(cstring.as_ptr()) };
15+
let _ = unsafe { libc::strlen(cstring.as_ptr()) };
1316

1417
// CStr
1518
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
16-
let len = unsafe { libc::strlen(cstr.as_ptr()) };
19+
let _ = unsafe { libc::strlen(cstr.as_ptr()) };
1720

18-
let len = unsafe { strlen(cstr.as_ptr()) };
21+
let _ = unsafe { strlen(cstr.as_ptr()) };
1922

2023
let pcstr: *const &CStr = &cstr;
21-
let len = unsafe { strlen((*pcstr).as_ptr()) };
24+
let _ = unsafe { strlen((*pcstr).as_ptr()) };
2225

2326
unsafe fn unsafe_identity<T>(x: T) -> T {
2427
x
2528
}
26-
let len = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
27-
let len = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
29+
let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
30+
let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
2831

2932
let f: unsafe fn(_) -> _ = unsafe_identity;
30-
let len = unsafe { strlen(f(cstr).as_ptr()) };
33+
let _ = unsafe { strlen(f(cstr).as_ptr()) };
3134
}

tests/ui/strlen_on_c_strings.stderr

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
error: using `libc::strlen` on a `CString` or `CStr` value
2-
--> $DIR/strlen_on_c_strings.rs:12:15
2+
--> $DIR/strlen_on_c_strings.rs:15:13
33
|
4-
LL | let len = unsafe { libc::strlen(cstring.as_ptr()) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`
4+
LL | let _ = unsafe { libc::strlen(cstring.as_ptr()) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`
66
|
77
= note: `-D clippy::strlen-on-c-strings` implied by `-D warnings`
88

99
error: using `libc::strlen` on a `CString` or `CStr` value
10-
--> $DIR/strlen_on_c_strings.rs:16:15
10+
--> $DIR/strlen_on_c_strings.rs:19:13
1111
|
12-
LL | let len = unsafe { libc::strlen(cstr.as_ptr()) };
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
12+
LL | let _ = unsafe { libc::strlen(cstr.as_ptr()) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
1414

1515
error: using `libc::strlen` on a `CString` or `CStr` value
16-
--> $DIR/strlen_on_c_strings.rs:18:15
16+
--> $DIR/strlen_on_c_strings.rs:21:13
1717
|
18-
LL | let len = unsafe { strlen(cstr.as_ptr()) };
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
18+
LL | let _ = unsafe { strlen(cstr.as_ptr()) };
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
2020

2121
error: using `libc::strlen` on a `CString` or `CStr` value
22-
--> $DIR/strlen_on_c_strings.rs:21:24
22+
--> $DIR/strlen_on_c_strings.rs:24:22
2323
|
24-
LL | let len = unsafe { strlen((*pcstr).as_ptr()) };
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(*pcstr).to_bytes().len()`
24+
LL | let _ = unsafe { strlen((*pcstr).as_ptr()) };
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(*pcstr).to_bytes().len()`
2626

2727
error: using `libc::strlen` on a `CString` or `CStr` value
28-
--> $DIR/strlen_on_c_strings.rs:26:24
28+
--> $DIR/strlen_on_c_strings.rs:29:22
2929
|
30-
LL | let len = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe_identity(cstr).to_bytes().len()`
30+
LL | let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe_identity(cstr).to_bytes().len()`
3232

3333
error: using `libc::strlen` on a `CString` or `CStr` value
34-
--> $DIR/strlen_on_c_strings.rs:27:15
34+
--> $DIR/strlen_on_c_strings.rs:30:13
3535
|
36-
LL | let len = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe { unsafe_identity(cstr) }.to_bytes().len()`
36+
LL | let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unsafe { unsafe_identity(cstr) }.to_bytes().len()`
3838

3939
error: using `libc::strlen` on a `CString` or `CStr` value
40-
--> $DIR/strlen_on_c_strings.rs:30:24
40+
--> $DIR/strlen_on_c_strings.rs:33:22
4141
|
42-
LL | let len = unsafe { strlen(f(cstr).as_ptr()) };
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `f(cstr).to_bytes().len()`
42+
LL | let _ = unsafe { strlen(f(cstr).as_ptr()) };
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `f(cstr).to_bytes().len()`
4444

4545
error: aborting due to 7 previous errors
4646

0 commit comments

Comments
 (0)