From 5643a0662af337c1096975400040c4442da439ca Mon Sep 17 00:00:00 2001 From: Nathan Whitaker Date: Tue, 18 Aug 2020 19:37:50 -0400 Subject: [PATCH] Tweak diagnostic --- compiler/rustc_lint/src/methods.rs | 10 ++++++---- src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr | 11 ++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_lint/src/methods.rs b/compiler/rustc_lint/src/methods.rs index 7b595dd18ff..1e67e9dd7de 100644 --- a/compiler/rustc_lint/src/methods.rs +++ b/compiler/rustc_lint/src/methods.rs @@ -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", ); diff --git a/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr b/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr index 34e42a47805..9ceb71ba8d9 100644 --- a/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr +++ b/src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr @@ -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