Tweak diagnostic
This commit is contained in:
parent
ce95122e95
commit
5643a0662a
@ -43,12 +43,13 @@ impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
|
||||
match first_method_call(expr) {
|
||||
Some((path, args)) if path.ident.name == sym::as_ptr => {
|
||||
let unwrap_arg = &args[0];
|
||||
let as_ptr_span = path.ident.span;
|
||||
match first_method_call(unwrap_arg) {
|
||||
Some((path, args))
|
||||
if path.ident.name == sym::unwrap || path.ident.name == sym::expect =>
|
||||
{
|
||||
let source_arg = &args[0];
|
||||
lint_cstring_as_ptr(cx, source_arg, unwrap_arg);
|
||||
lint_cstring_as_ptr(cx, as_ptr_span, source_arg, unwrap_arg);
|
||||
}
|
||||
_ => return,
|
||||
}
|
||||
@ -62,6 +63,7 @@ const CSTRING_PATH: [Symbol; 4] = [sym::std, sym::ffi, sym::c_str, sym::CString]
|
||||
|
||||
fn lint_cstring_as_ptr(
|
||||
cx: &LateContext<'_>,
|
||||
as_ptr_span: Span,
|
||||
source: &rustc_hir::Expr<'_>,
|
||||
unwrap: &rustc_hir::Expr<'_>,
|
||||
) {
|
||||
@ -70,11 +72,11 @@ fn lint_cstring_as_ptr(
|
||||
if cx.tcx.is_diagnostic_item(sym::result_type, def.did) {
|
||||
if let ty::Adt(adt, _) = substs.type_at(0).kind {
|
||||
if cx.match_def_path(adt.did, &CSTRING_PATH) {
|
||||
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, source.span, |diag| {
|
||||
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, as_ptr_span, |diag| {
|
||||
let mut diag = diag
|
||||
.build("getting the inner pointer of a temporary `CString`");
|
||||
diag.span_label(source.span, "this pointer will be invalid");
|
||||
diag.span_help(
|
||||
diag.span_label(as_ptr_span, "this pointer will be invalid");
|
||||
diag.span_label(
|
||||
unwrap.span,
|
||||
"this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime",
|
||||
);
|
||||
|
@ -1,15 +1,12 @@
|
||||
error: getting the inner pointer of a temporary `CString`
|
||||
--> $DIR/lint-temporary-cstring-as-ptr.rs:6:13
|
||||
--> $DIR/lint-temporary-cstring-as-ptr.rs:6:48
|
||||
|
|
||||
LL | let s = CString::new("some text").unwrap().as_ptr();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ this pointer will be invalid
|
||||
| ---------------------------------- ^^^^^^ this pointer will be invalid
|
||||
| |
|
||||
| this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime
|
||||
|
|
||||
= note: `#[deny(temporary_cstring_as_ptr)]` on by default
|
||||
help: this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime
|
||||
--> $DIR/lint-temporary-cstring-as-ptr.rs:6:13
|
||||
|
|
||||
LL | let s = CString::new("some text").unwrap().as_ptr();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` is deallocated because nothing is referencing it as far as the type system is concerned
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in New Issue
Block a user