diff options
| author | Nathan Whitaker <nathan.whitaker01@gmail.com> | 2020-09-22 11:20:06 -0400 |
|---|---|---|
| committer | Nathan Whitaker <nathan.whitaker01@gmail.com> | 2020-10-26 18:19:48 -0400 |
| commit | 576eb2a30cab62992e36f3acaf8aedbadf58ba75 (patch) | |
| tree | d9a739e58004ffba8d4c2e3be086f0352cc653fb /compiler/rustc_lint/src | |
| parent | 1bcd2452fe0abd1510b21cfa9aef19898a5c14fe (diff) | |
| download | rust-576eb2a30cab62992e36f3acaf8aedbadf58ba75.tar.gz rust-576eb2a30cab62992e36f3acaf8aedbadf58ba75.zip | |
Write docs for lint / fix review nit
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/methods.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/methods.rs b/compiler/rustc_lint/src/methods.rs index 3852cc4f298..2fad02bf883 100644 --- a/compiler/rustc_lint/src/methods.rs +++ b/compiler/rustc_lint/src/methods.rs @@ -6,6 +6,25 @@ use rustc_middle::ty; use rustc_span::{symbol::sym, ExpnKind, Span}; declare_lint! { + /// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of + /// a temporary `CString`. + /// + /// ### Example + /// + /// ```rust + /// # #![allow(unused)] + /// let c_str = CString::new("foo").unwrap().as_ptr(); + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// The inner pointer of a `CString` lives only as long as the `CString` it + /// points to. Getting the inner pointer of a *temporary* `CString` allows the `CString` + /// to be dropped at the end of the statement, as it is not being referenced as far as the typesystem + /// is concerned. This means outside of the statement the pointer will point to freed memory, which + /// causes undefined behavior if the pointer is later dereferenced. pub TEMPORARY_CSTRING_AS_PTR, Warn, "detects getting the inner pointer of a temporary `CString`" @@ -75,8 +94,7 @@ fn lint_cstring_as_ptr( unwrap.span, "this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime", ); - diag.note("pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement..."); - diag.note("...because nothing is referencing it as far as the type system is concerned"); + 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"); diag.help("for more information, see https://doc.rust-lang.org/reference/destructors.html"); diag.emit(); }); |
