Skip to content

Commit c29e05e

Browse files
committed
lint: port CString ptr diagnostics
Signed-off-by: David Wood <[email protected]>
1 parent 4f35c79 commit c29e05e

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ lint-diag-out-of-impl =
5454
diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
5555
5656
lint-untranslatable-diag = diagnostics should be created using translatable messages
57+
58+
lint-cstring-ptr = getting the inner pointer of a temporary `CString`
59+
.as-ptr-label = this pointer will be invalid
60+
.unwrap-label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
61+
.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
62+
.help = for more information, see https://doc.rust-lang.org/reference/destructors.html

compiler/rustc_lint/src/methods.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::LateContext;
22
use crate::LateLintPass;
33
use crate::LintContext;
4+
use rustc_errors::fluent;
45
use rustc_hir::{Expr, ExprKind, PathSegment};
56
use rustc_middle::ty;
67
use rustc_span::{symbol::sym, ExpnKind, Span};
@@ -88,16 +89,12 @@ fn lint_cstring_as_ptr(
8889
if let ty::Adt(adt, _) = substs.type_at(0).kind() {
8990
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) {
9091
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, as_ptr_span, |diag| {
91-
let mut diag = diag
92-
.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");
100-
diag.emit();
92+
diag.build(fluent::lint::cstring_ptr)
93+
.span_label(as_ptr_span, fluent::lint::as_ptr_label)
94+
.span_label(unwrap.span, fluent::lint::unwrap_label)
95+
.note(fluent::lint::note)
96+
.help(fluent::lint::help)
97+
.emit();
10198
});
10299
}
103100
}

0 commit comments

Comments
 (0)