Skip to content

Commit e2f6c80

Browse files
committed
instantly_dangling_pointer: fix ICE
Turns out you cannot format Ty's you won't emit in unsuppressed diagnostics
1 parent 5aaaf53 commit e2f6c80

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

compiler/rustc_lint/src/dangling.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
4949
&& as_ptr_path.ident.name == sym::as_ptr
5050
&& let ExprKind::MethodCall(unwrap_path, unwrap_receiver, ..) = as_ptr_receiver.kind
5151
&& (unwrap_path.ident.name == sym::unwrap || unwrap_path.ident.name == sym::expect)
52-
&& lint_cstring_as_ptr(cx, unwrap_receiver)
52+
&& let source_type = cx.typeck_results().expr_ty(unwrap_receiver)
53+
&& let ty::Adt(def, args) = source_type.kind()
54+
&& cx.tcx.is_diagnostic_item(sym::Result, def.did())
55+
&& let ty = args.type_at(0)
56+
&& let ty::Adt(adt, _) = ty.kind()
57+
&& cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did())
5358
{
5459
cx.emit_span_lint(
5560
TEMPORARY_CSTRING_AS_PTR,
5661
as_ptr_path.ident.span,
5762
InstantlyDangling {
5863
callee: as_ptr_path.ident.name,
59-
ty: "CString".into(),
64+
ty,
6065
ptr_span: as_ptr_path.ident.span,
6166
temporary_span: as_ptr_receiver.span,
6267
},
@@ -75,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
7580
method.ident.span,
7681
InstantlyDangling {
7782
callee: method.ident.name,
78-
ty: ty.to_string(),
83+
ty,
7984
ptr_span: method.ident.span,
8085
temporary_span: receiver.span,
8186
},
@@ -84,20 +89,6 @@ impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
8489
}
8590
}
8691

87-
fn lint_cstring_as_ptr(cx: &LateContext<'_>, source: &rustc_hir::Expr<'_>) -> bool {
88-
let source_type = cx.typeck_results().expr_ty(source);
89-
if let ty::Adt(def, args) = source_type.kind() {
90-
if cx.tcx.is_diagnostic_item(sym::Result, def.did()) {
91-
if let ty::Adt(adt, _) = args.type_at(0).kind() {
92-
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) {
93-
return true;
94-
}
95-
}
96-
}
97-
}
98-
false
99-
}
100-
10192
fn is_temporary_rvalue(expr: &Expr<'_>) -> bool {
10293
match expr.kind {
10394
// Const is not temporary.

compiler/rustc_lint/src/lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1130,9 +1130,9 @@ pub struct IgnoredUnlessCrateSpecified<'a> {
11301130
#[note]
11311131
#[help]
11321132
// FIXME: use #[primary_span]
1133-
pub struct InstantlyDangling {
1133+
pub struct InstantlyDangling<'tcx> {
11341134
pub callee: Symbol,
1135-
pub ty: String,
1135+
pub ty: Ty<'tcx>,
11361136
#[label(lint_label_ptr)]
11371137
pub ptr_span: Span,
11381138
#[label(lint_label_temporary)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ check-pass
2+
3+
// This should not ICE.
4+
5+
#![allow(instantly_dangling_pointer)]
6+
7+
fn main() {
8+
dbg!(String::new().as_ptr());
9+
}

0 commit comments

Comments
 (0)