Skip to content

Commit aaa72b7

Browse files
committed
instantly_dangling_pointer: change wording a bit
1 parent 7821554 commit aaa72b7

File tree

8 files changed

+25
-22
lines changed

8 files changed

+25
-22
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,10 @@ lint_incomplete_include =
405405
406406
lint_inner_macro_attribute_unstable = inner macro attributes are unstable
407407
408-
lint_instantly_dangling = this pointer will immediately dangle
409-
.ptr_label = this pointer will be invalid
410-
.temporary_label = this `{$ty}` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
411-
.note = pointers do not have a lifetime; when calling `{$method}` the `{$ty}` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
408+
lint_instantly_dangling = getting a pointer from a temporary `{$ty}` will result in a dangling pointer
409+
.label_ptr = this pointer will immediately be invalid
410+
.label_temporary = this `{$ty}` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
411+
.note = pointers do not have a lifetime; when calling `{$callee}` the `{$ty}` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
412412
.help = for more information, see https://doc.rust-lang.org/reference/destructors.html
413413
414414
lint_invalid_asm_label_binary = avoid using labels containing only the digits `0` and `1` in inline assembly

compiler/rustc_lint/src/dangling.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ declare_lint! {
3535
// FIXME: does not catch UnsafeCell::get
3636
// FIXME: does not catch getting a ref to a temporary and then converting it to a ptr
3737
declare_lint! {
38+
/// TODO
3839
pub INSTANTLY_DANGLING_POINTER,
3940
Warn,
4041
"detects getting a pointer that will immediately dangle"
4142
}
4243

43-
declare_lint_pass!(InstantlyDanglingPtr => [TEMPORARY_CSTRING_AS_PTR, INSTANTLY_DANGLING_POINTER]);
44+
declare_lint_pass!(DanglingPointers => [TEMPORARY_CSTRING_AS_PTR, INSTANTLY_DANGLING_POINTER]);
4445

45-
impl<'tcx> LateLintPass<'tcx> for InstantlyDanglingPtr {
46+
impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
4647
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4748
// We have a method call.
4849
let ExprKind::MethodCall(method, receiver, _args, _span) = expr.kind else {
@@ -66,9 +67,9 @@ impl<'tcx> LateLintPass<'tcx> for InstantlyDanglingPtr {
6667
return;
6768
};
6869

69-
let span = method.ident.span;
70+
let span = method_span;
7071
let decorator = InstantlyDangling {
71-
method: method_name,
72+
callee: method_name,
7273
ty: ty.to_string(),
7374
ptr_span: method_span,
7475
temporary_span: receiver.span,
@@ -85,7 +86,7 @@ impl<'tcx> LateLintPass<'tcx> for InstantlyDanglingPtr {
8586
fn is_temporary_rvalue(expr: &Expr<'_>) -> bool {
8687
match expr.kind {
8788
// We are not interested in these
88-
ExprKind::Cast(_, _) | ExprKind::Closure(_) | ExprKind::Tup(_) | ExprKind::Lit(_) => false,
89+
ExprKind::Cast(_, _) | ExprKind::Closure(_) | ExprKind::Tup(_) => false,
8990

9091
// Const is not temporary.
9192
ExprKind::ConstBlock(_) | ExprKind::Repeat(_, _) => false,
@@ -109,6 +110,8 @@ fn is_temporary_rvalue(expr: &Expr<'_>) -> bool {
109110
ExprKind::Field(parent, _) => is_temporary_rvalue(parent),
110111

111112
ExprKind::Struct(_, _, _) => true,
113+
// These are 'static
114+
ExprKind::Lit(_) => false,
112115
// False negatives are possible, but arrays get promoted to 'static way too often.
113116
ExprKind::Array(_) => false,
114117

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ late_lint_methods!(
223223
UngatedAsyncFnTrackCaller: UngatedAsyncFnTrackCaller,
224224
ShadowedIntoIter: ShadowedIntoIter,
225225
DropTraitConstraints: DropTraitConstraints,
226-
InstantlyDanglingPtr: InstantlyDanglingPtr,
226+
DanglingPointers: DanglingPointers,
227227
NonPanicFmt: NonPanicFmt,
228228
NoopMethodCall: NoopMethodCall,
229229
EnumIntrinsicsNonEnums: EnumIntrinsicsNonEnums,

compiler/rustc_lint/src/lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,11 @@ pub struct IgnoredUnlessCrateSpecified<'a> {
11301130
#[note]
11311131
#[help]
11321132
pub struct InstantlyDangling {
1133-
pub method: Symbol,
1133+
pub callee: Symbol,
11341134
pub ty: String,
1135-
#[label(lint_ptr_label)]
1135+
#[label(lint_label_ptr)]
11361136
pub ptr_span: Span,
1137-
#[label(lint_temporary_label)]
1137+
#[label(lint_label_temporary)]
11381138
pub temporary_span: Span,
11391139
}
11401140

tests/ui/lint/lint-temporary-cstring-as-param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn some_function(data: *const c_char) {}
77

88
fn main() {
99
some_function(CString::new("").unwrap().as_ptr());
10-
//~^ ERROR this pointer will immediately dangle
10+
//~^ ERROR getting a pointer from a temporary `CString` will result in a dangling pointer
1111
}

tests/ui/lint/lint-temporary-cstring-as-param.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: this pointer will immediately dangle
1+
error: getting a pointer from a temporary `CString` will result in a dangling pointer
22
--> $DIR/lint-temporary-cstring-as-param.rs:9:45
33
|
44
LL | some_function(CString::new("").unwrap().as_ptr());
5-
| ------------------------- ^^^^^^ this pointer will be invalid
5+
| ------------------------- ^^^^^^ this pointer will immediately be invalid
66
| |
77
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|

tests/ui/lint/lint-temporary-cstring-as-ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use std::ffi::CString;
66
macro_rules! mymacro {
77
() => {
88
let s = CString::new("some text").unwrap().as_ptr();
9-
//~^ ERROR this pointer will immediately dangle
9+
//~^ ERROR getting a pointer from a temporary `CString` will result in a dangling pointer
1010
}
1111
}
1212

1313
fn main() {
1414
let s = CString::new("some text").unwrap().as_ptr();
15-
//~^ ERROR this pointer will immediately dangle
15+
//~^ ERROR getting a pointer from a temporary `CString` will result in a dangling pointer
1616
mymacro!();
1717
}

tests/ui/lint/lint-temporary-cstring-as-ptr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: this pointer will immediately dangle
1+
error: getting a pointer from a temporary `CString` will result in a dangling pointer
22
--> $DIR/lint-temporary-cstring-as-ptr.rs:14:48
33
|
44
LL | let s = CString::new("some text").unwrap().as_ptr();
5-
| ---------------------------------- ^^^^^^ this pointer will be invalid
5+
| ---------------------------------- ^^^^^^ this pointer will immediately be invalid
66
| |
77
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
@@ -14,11 +14,11 @@ note: the lint level is defined here
1414
LL | #![deny(temporary_cstring_as_ptr)]
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^
1616

17-
error: this pointer will immediately dangle
17+
error: getting a pointer from a temporary `CString` will result in a dangling pointer
1818
--> $DIR/lint-temporary-cstring-as-ptr.rs:8:52
1919
|
2020
LL | let s = CString::new("some text").unwrap().as_ptr();
21-
| ---------------------------------- ^^^^^^ this pointer will be invalid
21+
| ---------------------------------- ^^^^^^ this pointer will immediately be invalid
2222
| |
2323
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
2424
...

0 commit comments

Comments
 (0)