diff --git a/tests/ui/cstring.rs b/tests/ui/cstring.rs new file mode 100644 index 00000000000..8b7b0b66bc6 --- /dev/null +++ b/tests/ui/cstring.rs @@ -0,0 +1,8 @@ +fn main() {} + +#[allow(result_unwrap_used)] +fn temporary_cstring() { + use std::ffi::CString; + + CString::new("foo").unwrap().as_ptr(); +} diff --git a/tests/ui/cstring.stderr b/tests/ui/cstring.stderr new file mode 100644 index 00000000000..ddb74ce9cac --- /dev/null +++ b/tests/ui/cstring.stderr @@ -0,0 +1,22 @@ +error: function is never used: `temporary_cstring` + --> $DIR/cstring.rs:4:1 + | +4 | fn temporary_cstring() { + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D dead-code` implied by `-D warnings` + +error: you are getting the inner pointer of a temporary `CString` + --> $DIR/cstring.rs:7:5 + | +7 | CString::new("foo").unwrap().as_ptr(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D temporary-cstring-as-ptr` implied by `-D warnings` + = 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 + | +7 | CString::new("foo").unwrap().as_ptr(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + diff --git a/tests/ui/methods.rs b/tests/ui/methods.rs index 4eb7846d3cc..a422cffafab 100644 --- a/tests/ui/methods.rs +++ b/tests/ui/methods.rs @@ -453,10 +453,3 @@ fn str_extend_chars() { let f = HasChars; s.extend(f.chars()); } - -#[allow(result_unwrap_used)] -fn temporary_cstring() { - use std::ffi::CString; - - CString::new("foo").unwrap().as_ptr(); -} diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index 0bf7dc321c9..d80ff30ec71 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -567,17 +567,3 @@ error: calling `.extend(_.chars())` 447 | s.extend(def.chars()); | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.push_str(&def)` -error: you are getting the inner pointer of a temporary `CString` - --> $DIR/methods.rs:461:5 - | -461 | CString::new("foo").unwrap().as_ptr(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `-D temporary-cstring-as-ptr` implied by `-D warnings` - = note: that pointer will be invalid outside this expression -help: assign the `CString` to a variable to extend its lifetime - --> $DIR/methods.rs:461:5 - | -461 | CString::new("foo").unwrap().as_ptr(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -