Skip to content

Commit 37b72bc

Browse files
committed
Write docs for lint / fix review nit
1 parent 79e61a0 commit 37b72bc

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

compiler/rustc_lint/src/methods.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ use rustc_middle::ty;
66
use rustc_span::{symbol::sym, ExpnKind, Span};
77

88
declare_lint! {
9+
/// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of
10+
/// a temporary `CString`.
11+
///
12+
/// ### Example
13+
///
14+
/// ```rust
15+
/// # #![allow(unused)]
16+
/// let c_str = CString::new("foo").unwrap().as_ptr();
17+
/// ```
18+
///
19+
/// {{produces}}
20+
///
21+
/// ### Explanation
22+
///
23+
/// The inner pointer of a `CString` lives only as long as the `CString` it
24+
/// points to. Getting the inner pointer of a *temporary* `CString` allows the `CString`
25+
/// to be dropped at the end of the statement, as it is not being referenced as far as the typesystem
26+
/// is concerned. This means outside of the statement the pointer will point to freed memory, which
27+
/// causes undefined behavior if the pointer is later dereferenced.
928
pub TEMPORARY_CSTRING_AS_PTR,
1029
Warn,
1130
"detects getting the inner pointer of a temporary `CString`"
@@ -75,8 +94,7 @@ fn lint_cstring_as_ptr(
7594
unwrap.span,
7695
"this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime",
7796
);
78-
diag.note("pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement...");
79-
diag.note("...because nothing is referencing it as far as the type system is concerned");
97+
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");
8098
diag.help("for more information, see https://doc.rust-lang.org/reference/destructors.html");
8199
diag.emit();
82100
});

src/test/ui/lint/lint-temporary-cstring-as-param.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ note: the lint level is defined here
1111
|
1212
LL | #![deny(temporary_cstring_as_ptr)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^
14-
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement...
15-
= note: ...because nothing is referencing it as far as the type system is concerned
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
1615
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1716

1817
error: aborting due to previous error

src/test/ui/lint/lint-temporary-cstring-as-ptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// ignore-tidy-linelength
2+
// this program is not technically incorrect, but is an obscure enough style to be worth linting
23
#![deny(temporary_cstring_as_ptr)]
34

45
use std::ffi::CString;

src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
error: getting the inner pointer of a temporary `CString`
2-
--> $DIR/lint-temporary-cstring-as-ptr.rs:7:48
2+
--> $DIR/lint-temporary-cstring-as-ptr.rs:8:48
33
|
44
LL | let s = CString::new("some text").unwrap().as_ptr();
55
| ---------------------------------- ^^^^^^ this pointer will be invalid
66
| |
77
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
99
note: the lint level is defined here
10-
--> $DIR/lint-temporary-cstring-as-ptr.rs:2:9
10+
--> $DIR/lint-temporary-cstring-as-ptr.rs:3:9
1111
|
1212
LL | #![deny(temporary_cstring_as_ptr)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^
14-
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement...
15-
= note: ...because nothing is referencing it as far as the type system is concerned
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
1615
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1716

1817
error: aborting due to previous error

0 commit comments

Comments
 (0)