diff options
| author | Nathan Whitaker <nathan.whitaker01@gmail.com> | 2020-08-18 19:37:50 -0400 |
|---|---|---|
| committer | Nathan Whitaker <nathan.whitaker01@gmail.com> | 2020-10-26 18:19:47 -0400 |
| commit | 5643a0662af337c1096975400040c4442da439ca (patch) | |
| tree | 4dccc35967c4099ac1d44ff5dde77688292c5f22 /compiler/rustc_lint/src/methods.rs | |
| parent | ce95122e95b5cb4698fa2a07e747823b64729f59 (diff) | |
| download | rust-5643a0662af337c1096975400040c4442da439ca.tar.gz rust-5643a0662af337c1096975400040c4442da439ca.zip | |
Tweak diagnostic
Diffstat (limited to 'compiler/rustc_lint/src/methods.rs')
| -rw-r--r-- | compiler/rustc_lint/src/methods.rs | 10 |
1 files changed, 6 insertions, 4 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", ); |
