about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-08-09 09:22:02 +0000
committerbors <bors@rust-lang.org>2019-08-09 09:22:02 +0000
commitc55d38ed7ace06e4a3a5939b3c7c01045017bca4 (patch)
tree2599e59b874ae14ac626c3d943e1a2ac10ac2255
parent26a1e532e6ce2067b5c157af0157f3b18697781a (diff)
parentc23a5c586f521c320033712e7245a37a0a728999 (diff)
downloadrust-c55d38ed7ace06e4a3a5939b3c7c01045017bca4.tar.gz
rust-c55d38ed7ace06e4a3a5939b3c7c01045017bca4.zip
Auto merge of #4362 - lzutao:expect-on-cstring_as_ptr, r=flip1995
Fix lint_cstring_as_ptr for expect

Closes #4312
changelog: none
-rw-r--r--clippy_lints/src/methods/mod.rs4
-rw-r--r--tests/ui/cstring.rs1
-rw-r--r--tests/ui/cstring.stderr15
3 files changed, 18 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 19151a414c7..4d57d76b3dc 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -937,7 +937,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
             ["is_some", "position"] => lint_search_is_some(cx, expr, "position", arg_lists[1], arg_lists[0]),
             ["is_some", "rposition"] => lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0]),
             ["extend", ..] => lint_extend(cx, expr, arg_lists[0]),
-            ["as_ptr", "unwrap"] => lint_cstring_as_ptr(cx, expr, &arg_lists[1][0], &arg_lists[0][0]),
+            ["as_ptr", "unwrap"] | ["as_ptr", "expect"] => {
+                lint_cstring_as_ptr(cx, expr, &arg_lists[1][0], &arg_lists[0][0])
+            },
             ["nth", "iter"] => lint_iter_nth(cx, expr, arg_lists[1], false),
             ["nth", "iter_mut"] => lint_iter_nth(cx, expr, arg_lists[1], true),
             ["next", "skip"] => lint_iter_skip_next(cx, expr),
diff --git a/tests/ui/cstring.rs b/tests/ui/cstring.rs
index 6cc36518e27..0d775d4067a 100644
--- a/tests/ui/cstring.rs
+++ b/tests/ui/cstring.rs
@@ -5,4 +5,5 @@ fn temporary_cstring() {
     use std::ffi::CString;
 
     CString::new("foo").unwrap().as_ptr();
+    CString::new("foo").expect("dummy").as_ptr();
 }
diff --git a/tests/ui/cstring.stderr b/tests/ui/cstring.stderr
index 8c86249dc84..83f64ff3b02 100644
--- a/tests/ui/cstring.stderr
+++ b/tests/ui/cstring.stderr
@@ -12,5 +12,18 @@ help: assign the `CString` to a variable to extend its lifetime
 LL |     CString::new("foo").unwrap().as_ptr();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to previous error
+error: you are getting the inner pointer of a temporary `CString`
+  --> $DIR/cstring.rs:8: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
+   |
+LL |     CString::new("foo").expect("dummy").as_ptr();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors