Closed
Description
I tried this code:
fn some_function(data: *const u8) {}
fn main() {
let string = "hello";
some_function(CString::new(string).unwrap().as_ptr());
}
I expected no clippy complains about this one.
Instead, clippy gave "that pointer will be invalid outside this expression". Yes, "that pointer will be invalid outside this expression", but I only need it valid inside this expression, so this is a false positive.
Meta
- Clippy 0.0.212 (2020-07-23 0820e54)
- Reproducible on Playground as of 2020-07-24
Backtrace
error: you are getting the inner pointer of a temporary `CString`
--> src/main.rs:7:19
|
7 | some_function(CString::new(string).unwrap().as_ptr());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[deny(clippy::temporary_cstring_as_ptr)]` on by default
= note: that pointer will be invalid outside this expression
help: assign the `CString` to a variable to extend its lifetime
--> src/main.rs:7:19
|
7 | some_function(CString::new(string).unwrap().as_ptr());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#temporary_cstring_as_ptr