Skip to content

Commit c55d38e

Browse files
committed
Auto merge of #4362 - lzutao:expect-on-cstring_as_ptr, r=flip1995
Fix lint_cstring_as_ptr for expect Closes #4312 changelog: none
2 parents 26a1e53 + c23a5c5 commit c55d38e

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

clippy_lints/src/methods/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
937937
["is_some", "position"] => lint_search_is_some(cx, expr, "position", arg_lists[1], arg_lists[0]),
938938
["is_some", "rposition"] => lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0]),
939939
["extend", ..] => lint_extend(cx, expr, arg_lists[0]),
940-
["as_ptr", "unwrap"] => lint_cstring_as_ptr(cx, expr, &arg_lists[1][0], &arg_lists[0][0]),
940+
["as_ptr", "unwrap"] | ["as_ptr", "expect"] => {
941+
lint_cstring_as_ptr(cx, expr, &arg_lists[1][0], &arg_lists[0][0])
942+
},
941943
["nth", "iter"] => lint_iter_nth(cx, expr, arg_lists[1], false),
942944
["nth", "iter_mut"] => lint_iter_nth(cx, expr, arg_lists[1], true),
943945
["next", "skip"] => lint_iter_skip_next(cx, expr),

tests/ui/cstring.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ fn temporary_cstring() {
55
use std::ffi::CString;
66

77
CString::new("foo").unwrap().as_ptr();
8+
CString::new("foo").expect("dummy").as_ptr();
89
}

tests/ui/cstring.stderr

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,18 @@ help: assign the `CString` to a variable to extend its lifetime
1212
LL | CString::new("foo").unwrap().as_ptr();
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
error: aborting due to previous error
15+
error: you are getting the inner pointer of a temporary `CString`
16+
--> $DIR/cstring.rs:8:5
17+
|
18+
LL | CString::new("foo").expect("dummy").as_ptr();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= note: that pointer will be invalid outside this expression
22+
help: assign the `CString` to a variable to extend its lifetime
23+
--> $DIR/cstring.rs:8:5
24+
|
25+
LL | CString::new("foo").expect("dummy").as_ptr();
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error: aborting due to 2 previous errors
1629

0 commit comments

Comments
 (0)