-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4425 - mikerite:4375, r=flip1995
Fix `temporary_cstring_as_ptr` false negative Fixes #4375. Changes the check to test when `.unwrap().as_ptr()` is called on any `Result<CString, _>` as suggested by @flip1995 (#4375 (comment)). changelog: Fix `temporary_cstring_as_ptr` false negative
- Loading branch information
Showing
4 changed files
with
45 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
#![deny(clippy::temporary_cstring_as_ptr)] | ||
|
||
fn main() {} | ||
|
||
#[allow(clippy::result_unwrap_used)] | ||
fn temporary_cstring() { | ||
use std::ffi::CString; | ||
|
||
CString::new("foo").unwrap().as_ptr(); | ||
CString::new("foo").expect("dummy").as_ptr(); | ||
} | ||
|
||
mod issue4375 { | ||
use std::ffi::CString; | ||
use std::os::raw::c_char; | ||
|
||
extern "C" { | ||
fn foo(data: *const c_char); | ||
} | ||
|
||
pub fn bar(v: &[u8]) { | ||
let cstr = CString::new(v); | ||
unsafe { foo(cstr.unwrap().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 |
---|---|---|
@@ -1,29 +1,46 @@ | ||
error: you are getting the inner pointer of a temporary `CString` | ||
--> $DIR/cstring.rs:7:5 | ||
--> $DIR/cstring.rs:8:5 | ||
| | ||
LL | CString::new("foo").unwrap().as_ptr(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[deny(clippy::temporary_cstring_as_ptr)]` on by default | ||
note: lint level defined here | ||
--> $DIR/cstring.rs:1:9 | ||
| | ||
LL | #![deny(clippy::temporary_cstring_as_ptr)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: that pointer will be invalid outside this expression | ||
help: assign the `CString` to a variable to extend its lifetime | ||
--> $DIR/cstring.rs:7:5 | ||
--> $DIR/cstring.rs:8:5 | ||
| | ||
LL | CString::new("foo").unwrap().as_ptr(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: you are getting the inner pointer of a temporary `CString` | ||
--> $DIR/cstring.rs:8:5 | ||
--> $DIR/cstring.rs:9:5 | ||
| | ||
LL | CString::new("foo").expect("dummy").as_ptr(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: that pointer will be invalid outside this expression | ||
help: assign the `CString` to a variable to extend its lifetime | ||
--> $DIR/cstring.rs:8:5 | ||
--> $DIR/cstring.rs:9:5 | ||
| | ||
LL | CString::new("foo").expect("dummy").as_ptr(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
error: you are getting the inner pointer of a temporary `CString` | ||
--> $DIR/cstring.rs:22:22 | ||
| | ||
LL | unsafe { foo(cstr.unwrap().as_ptr()) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: that pointer will be invalid outside this expression | ||
help: assign the `CString` to a variable to extend its lifetime | ||
--> $DIR/cstring.rs:22:22 | ||
| | ||
LL | unsafe { foo(cstr.unwrap().as_ptr()) } | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|