about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNathan Whitaker <nathan.whitaker01@gmail.com>2020-08-18 12:09:33 -0400
committerNathan Whitaker <nathan.whitaker01@gmail.com>2020-10-26 18:19:46 -0400
commita2f4afe0f6e9ce451e2aca0a91100c6335be9181 (patch)
tree96f00e11588b06cd75b59a0714e048d7285f2d29
parent8cf1b0e1adda1313771dbce65e9be7b40fa17490 (diff)
Add basic test
-rw-r--r--src/test/ui/lint/lint-temporary-cstring-as-ptr.rs8
-rw-r--r--src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr16
2 files changed, 24 insertions, 0 deletions
diff --git a/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs b/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs
new file mode 100644
index 00000000000..3ca6ee5439c
--- /dev/null
+++ b/src/test/ui/lint/lint-temporary-cstring-as-ptr.rs
@@ -0,0 +1,8 @@
+// check-fail
+// ignore-tidy-linelength
+
+use std::ffi::CString;
+
+fn main() {
+    let s = CString::new("some text").unwrap().as_ptr(); //~ ERROR you are getting the inner pointer of a temporary `CString`
+}
diff --git a/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr b/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr
new file mode 100644
index 00000000000..7cbb14a8f4e
--- /dev/null
+++ b/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr
@@ -0,0 +1,16 @@
+error: you are getting the inner pointer of a temporary `CString`
+  --> $DIR/lint-temporary-cstring-as-ptr.rs:7:13
+   |
+LL |     let s = CString::new("some text").unwrap().as_ptr();
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: `#[deny(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
+  --> $DIR/lint-temporary-cstring-as-ptr.rs:7:13
+   |
+LL |     let s = CString::new("some text").unwrap().as_ptr();
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to previous error
+