Skip to content

False positive with temporary_cstring_as_ptr  #5838

Closed
@kangalio

Description

@kangalio

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions