Auto merge of #4362 - lzutao:expect-on-cstring_as_ptr, r=flip1995

Fix lint_cstring_as_ptr for expect

Closes #4312
changelog: none
This commit is contained in:
bors 2019-08-09 09:22:02 +00:00
commit c55d38ed7a
3 changed files with 18 additions and 2 deletions

View file

@ -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),

View file

@ -5,4 +5,5 @@ fn temporary_cstring() {
use std::ffi::CString;
CString::new("foo").unwrap().as_ptr();
CString::new("foo").expect("dummy").as_ptr();
}

View file

@ -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