diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index f40171aeed3..59a32e285e3 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