You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #75671 - nathanwhit:cstring-temp-lint, r=oli-obk
Uplift `temporary-cstring-as-ptr` lint from `clippy` into rustc
The general consensus seems to be that this lint covers a common enough mistake to warrant inclusion in rustc.
The diagnostic message might need some tweaking, as I'm not sure the use of second-person perspective matches the rest of rustc, but I'd like to hear others' thoughts on that.
(cc #53224).
r? `@oli-obk`
.build("getting the inner pointer of a temporary `CString`");
93
+
diag.span_label(as_ptr_span,"this pointer will be invalid");
94
+
diag.span_label(
95
+
unwrap.span,
96
+
"this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime",
97
+
);
98
+
diag.note("pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned");
99
+
diag.help("for more information, see https://doc.rust-lang.org/reference/destructors.html");
| ------------------------- ^^^^^^ this pointer will be invalid
6
+
| |
7
+
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
8
+
|
9
+
note: the lint level is defined here
10
+
--> $DIR/lint-temporary-cstring-as-param.rs:1:9
11
+
|
12
+
LL | #![deny(temporary_cstring_as_ptr)]
13
+
| ^^^^^^^^^^^^^^^^^^^^^^^^
14
+
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
15
+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
error: getting the inner pointer of a temporary `CString`
2
+
--> $DIR/lint-temporary-cstring-as-ptr.rs:7:48
3
+
|
4
+
LL | let s = CString::new("some text").unwrap().as_ptr();
5
+
| ---------------------------------- ^^^^^^ this pointer will be invalid
6
+
| |
7
+
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
8
+
|
9
+
note: the lint level is defined here
10
+
--> $DIR/lint-temporary-cstring-as-ptr.rs:2:9
11
+
|
12
+
LL | #![deny(temporary_cstring_as_ptr)]
13
+
| ^^^^^^^^^^^^^^^^^^^^^^^^
14
+
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
15
+
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
0 commit comments