Skip to content

Commit c900d4a

Browse files
committed
update_lint non_reentrant_functions
Signed-off-by: mojave2 <0109chenchen@gmail.com>
1 parent 9845b3a commit c900d4a

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

clippy_lints/src/non_reentrant_functions.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ impl EarlyLintPass for NonReentrantFunctions {
4646
fn is_reentrant_fn(func: &Expr) -> bool {
4747
match &func.kind {
4848
ExprKind::Path(_, Path { segments, .. }) => {
49-
let seg = segments.iter().last().unwrap();
50-
let ident = format!("{:?}", seg.ident);
49+
if segments.len() != 2 || !format!("{:?}", segments[0].ident).starts_with("libc#") {
50+
return false;
51+
}
52+
let ident = format!("{:?}", segments[1].ident);
5153
check_reentrant_by_fn_name(&ident)
5254
},
5355
_ => false,

tests/ui/non_reentrant_functions.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
#![warn(clippy::non_reentrant_functions)]
2+
#![allow(unused)]
23
#![feature(rustc_private)]
34
extern crate libc;
45

5-
#[allow(unused)]
6-
use libc::{c_char, localtime, strtok, time_t};
76
use std::ffi::{CStr, CString};
87

9-
fn main() {
8+
fn test_libc_localtime() {
109
// test code goes here
1110
unsafe {
12-
let _tm = localtime(&0i64 as *const time_t);
11+
let _tm = libc::localtime(&0i64 as *const libc::time_t);
1312
}
13+
}
1414

15+
fn test_libc_strtok() {
1516
let string = CString::new("welcome-to-rust").unwrap();
16-
let string = string.as_ptr() as *mut c_char;
17+
let string = string.as_ptr() as *mut libc::c_char;
1718
let delim = CString::new(" - ").unwrap();
1819
let delim = delim.as_ptr();
1920

20-
let mut token = unsafe { strtok(string, delim) };
21+
let mut token = unsafe { libc::strtok(string, delim) };
2122
while !token.is_null() {
2223
println!("{:?}", unsafe { CStr::from_ptr(token) });
23-
token = unsafe { strtok(std::ptr::null_mut(), delim) };
24+
token = unsafe { libc::strtok(std::ptr::null_mut(), delim) };
2425
}
2526
}
27+
28+
fn test_locatime() {
29+
fn localtime() {}
30+
localtime();
31+
}
32+
33+
fn test_strtok() {
34+
fn strtok() {}
35+
strtok();
36+
}
37+
38+
fn main() {}
+9-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
error: consider using the reentrant version of the function
2-
--> $DIR/non_reentrant_functions.rs:12:19
2+
--> $DIR/non_reentrant_functions.rs:11:19
33
|
4-
LL | let _tm = localtime(&0i64 as *const time_t);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | let _tm = libc::localtime(&0i64 as *const libc::time_t);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::non-reentrant-functions` implied by `-D warnings`
88

99
error: consider using the reentrant version of the function
10-
--> $DIR/non_reentrant_functions.rs:20:30
10+
--> $DIR/non_reentrant_functions.rs:21:30
1111
|
12-
LL | let mut token = unsafe { strtok(string, delim) };
13-
| ^^^^^^^^^^^^^^^^^^^^^
12+
LL | let mut token = unsafe { libc::strtok(string, delim) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: consider using the reentrant version of the function
16-
--> $DIR/non_reentrant_functions.rs:23:26
16+
--> $DIR/non_reentrant_functions.rs:24:26
1717
|
18-
LL | token = unsafe { strtok(std::ptr::null_mut(), delim) };
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
LL | token = unsafe { libc::strtok(std::ptr::null_mut(), delim) };
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: aborting due to 3 previous errors
2222

0 commit comments

Comments
 (0)