-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a special case for CStr/CString in the improper_ctypes lint
Instead of saying to "consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct", we now tell users to "Use `*const ffi::c_char` instead, and pass the value from `CStr::as_ptr()`" when the type involved is a `CStr` or a `CString`.
- Loading branch information
1 parent
1828461
commit e607e99
Showing
6 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![crate_type = "lib"] | ||
#![deny(improper_ctypes)] | ||
|
||
use std::ffi::{CStr, CString}; | ||
|
||
extern "C" { | ||
fn take_cstr(s: CStr); | ||
//~^ ERROR `extern` block uses type `CStr`, which is not FFI-safe | ||
//~| HELP consider using `*const std::ffi::c_char` instead, and pass the value from `CStr::as_ptr()` | ||
fn take_cstring(s: CString); | ||
//~^ ERROR `extern` block uses type `CString`, which is not FFI-safe | ||
//~| HELP consider using `*const std::ffi::c_char` instead, and pass the value from `CStr::as_ptr()` | ||
fn take_cstring_ref(s: &CString); | ||
//~^ ERROR `extern` block uses type `CString`, which is not FFI-safe | ||
//~| HELP consider using `*const std::ffi::c_char` instead, and pass the value from `CStr::as_ptr()` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
error: `extern` block uses type `CStr`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:7:21 | ||
| | ||
LL | fn take_cstr(s: CStr); | ||
| ^^^^ not FFI-safe | ||
| | ||
= help: consider using `*const std::ffi::c_char` instead, and pass the value from `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
note: the lint level is defined here | ||
--> $DIR/lint-ctypes-cstr.rs:2:9 | ||
| | ||
LL | #![deny(improper_ctypes)] | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: `extern` block uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:10:24 | ||
| | ||
LL | fn take_cstring(s: CString); | ||
| ^^^^^^^ not FFI-safe | ||
| | ||
= help: consider using `*const std::ffi::c_char` instead, and pass the value from `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
|
||
error: `extern` block uses type `CString`, which is not FFI-safe | ||
--> $DIR/lint-ctypes-cstr.rs:13:28 | ||
| | ||
LL | fn take_cstring_ref(s: &CString); | ||
| ^^^^^^^^ not FFI-safe | ||
| | ||
= help: consider using `*const std::ffi::c_char` instead, and pass the value from `CStr::as_ptr()` | ||
= note: `CStr`/`CString` do not have a guaranteed layout | ||
|
||
error: aborting due to 3 previous errors | ||
|