Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5643a06

Browse files
committed
Tweak diagnostic
1 parent ce95122 commit 5643a06

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

compiler/rustc_lint/src/methods.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
4343
match first_method_call(expr) {
4444
Some((path, args)) if path.ident.name == sym::as_ptr => {
4545
let unwrap_arg = &args[0];
46+
let as_ptr_span = path.ident.span;
4647
match first_method_call(unwrap_arg) {
4748
Some((path, args))
4849
if path.ident.name == sym::unwrap || path.ident.name == sym::expect =>
4950
{
5051
let source_arg = &args[0];
51-
lint_cstring_as_ptr(cx, source_arg, unwrap_arg);
52+
lint_cstring_as_ptr(cx, as_ptr_span, source_arg, unwrap_arg);
5253
}
5354
_ => return,
5455
}
@@ -62,6 +63,7 @@ const CSTRING_PATH: [Symbol; 4] = [sym::std, sym::ffi, sym::c_str, sym::CString]
6263

6364
fn lint_cstring_as_ptr(
6465
cx: &LateContext<'_>,
66+
as_ptr_span: Span,
6567
source: &rustc_hir::Expr<'_>,
6668
unwrap: &rustc_hir::Expr<'_>,
6769
) {
@@ -70,11 +72,11 @@ fn lint_cstring_as_ptr(
7072
if cx.tcx.is_diagnostic_item(sym::result_type, def.did) {
7173
if let ty::Adt(adt, _) = substs.type_at(0).kind {
7274
if cx.match_def_path(adt.did, &CSTRING_PATH) {
73-
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, source.span, |diag| {
75+
cx.struct_span_lint(TEMPORARY_CSTRING_AS_PTR, as_ptr_span, |diag| {
7476
let mut diag = diag
7577
.build("getting the inner pointer of a temporary `CString`");
76-
diag.span_label(source.span, "this pointer will be invalid");
77-
diag.span_help(
78+
diag.span_label(as_ptr_span, "this pointer will be invalid");
79+
diag.span_label(
7880
unwrap.span,
7981
"this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime",
8082
);

src/test/ui/lint/lint-temporary-cstring-as-ptr.stderr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
error: getting the inner pointer of a temporary `CString`
2-
--> $DIR/lint-temporary-cstring-as-ptr.rs:6:13
2+
--> $DIR/lint-temporary-cstring-as-ptr.rs:6:48
33
|
44
LL | let s = CString::new("some text").unwrap().as_ptr();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ this pointer will be invalid
5+
| ---------------------------------- ^^^^^^ this pointer will be invalid
6+
| |
7+
| this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime
68
|
79
= note: `#[deny(temporary_cstring_as_ptr)]` on by default
8-
help: this `CString` is deallocated at the end of the expression, bind it to a variable to extend its lifetime
9-
--> $DIR/lint-temporary-cstring-as-ptr.rs:6:13
10-
|
11-
LL | let s = CString::new("some text").unwrap().as_ptr();
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1310
= 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
1411

1512
error: aborting due to previous error

0 commit comments

Comments
 (0)