mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-13 15:33:53 +00:00
lint: port CString
ptr diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
4f35c7993b
commit
c29e05e745
@ -54,3 +54,9 @@ lint-diag-out-of-impl =
|
|||||||
diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
|
diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
|
||||||
|
|
||||||
lint-untranslatable-diag = diagnostics should be created using translatable messages
|
lint-untranslatable-diag = diagnostics should be created using translatable messages
|
||||||
|
|
||||||
|
lint-cstring-ptr = getting the inner pointer of a temporary `CString`
|
||||||
|
.as-ptr-label = this pointer will be invalid
|
||||||
|
.unwrap-label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
||||||
|
.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
|
||||||
|
.help = for more information, see https://doc.rust-lang.org/reference/destructors.html
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use crate::LateContext;
|
use crate::LateContext;
|
||||||
use crate::LateLintPass;
|
use crate::LateLintPass;
|
||||||
use crate::LintContext;
|
use crate::LintContext;
|
||||||
|
use rustc_errors::fluent;
|
||||||
use rustc_hir::{Expr, ExprKind, PathSegment};
|
use rustc_hir::{Expr, ExprKind, PathSegment};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_span::{symbol::sym, ExpnKind, Span};
|
use rustc_span::{symbol::sym, ExpnKind, Span};
|
||||||
@ -88,16 +89,12 @@ fn lint_cstring_as_ptr(
|
|||||||
if let ty::Adt(adt, _) = substs.type_at(0).kind() {
|
if let ty::Adt(adt, _) = substs.type_at(0).kind() {
|
||||||
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) {
|
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) {
|
||||||
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, as_ptr_span, |diag| {
|
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, as_ptr_span, |diag| {
|
||||||
let mut diag = diag
|
diag.build(fluent::lint::cstring_ptr)
|
||||||
.build("getting the inner pointer of a temporary `CString`");
|
.span_label(as_ptr_span, fluent::lint::as_ptr_label)
|
||||||
diag.span_label(as_ptr_span, "this pointer will be invalid");
|
.span_label(unwrap.span, fluent::lint::unwrap_label)
|
||||||
diag.span_label(
|
.note(fluent::lint::note)
|
||||||
unwrap.span,
|
.help(fluent::lint::help)
|
||||||
"this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime",
|
.emit();
|
||||||
);
|
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user