|
1 | 1 | use rustc_hir::{Expr, ExprKind, LangItem};
|
2 |
| -use rustc_middle::ty::{self, Ty, TyCtxt}; |
| 2 | +use rustc_middle::ty::{Ty, TyCtxt}; |
3 | 3 | use rustc_session::{declare_lint, declare_lint_pass};
|
4 | 4 | use rustc_span::symbol::sym;
|
5 | 5 |
|
6 | 6 | use crate::lints::InstantlyDangling;
|
7 | 7 | use crate::{LateContext, LateLintPass, LintContext};
|
8 | 8 |
|
9 |
| -declare_lint! { |
10 |
| - /// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of |
11 |
| - /// a temporary `CString`. |
12 |
| - /// |
13 |
| - /// ### Example |
14 |
| - /// |
15 |
| - /// ```rust |
16 |
| - /// # #![allow(unused)] |
17 |
| - /// # use std::ffi::CString; |
18 |
| - /// let c_str = CString::new("foo").unwrap().as_ptr(); |
19 |
| - /// ``` |
20 |
| - /// |
21 |
| - /// {{produces}} |
22 |
| - /// |
23 |
| - /// ### Explanation |
24 |
| - /// |
25 |
| - /// The inner pointer of a `CString` lives only as long as the `CString` it |
26 |
| - /// points to. Getting the inner pointer of a *temporary* `CString` allows the `CString` |
27 |
| - /// to be dropped at the end of the statement, as it is not being referenced as far as the |
28 |
| - /// typesystem is concerned. This means outside of the statement the pointer will point to |
29 |
| - /// freed memory, which causes undefined behavior if the pointer is later dereferenced. |
30 |
| - pub TEMPORARY_CSTRING_AS_PTR, |
31 |
| - Warn, |
32 |
| - "detects getting the inner pointer of a temporary `CString`" |
33 |
| -} |
34 |
| - |
35 | 9 | // FIXME: does not catch UnsafeCell::get
|
36 | 10 | // FIXME: does not catch getting a ref to a temporary and then converting it to a ptr
|
37 | 11 | declare_lint! {
|
@@ -68,34 +42,10 @@ declare_lint! {
|
68 | 42 | "detects getting a pointer from a temporary"
|
69 | 43 | }
|
70 | 44 |
|
71 |
| -declare_lint_pass!(DanglingPointers => [TEMPORARY_CSTRING_AS_PTR, DANGLING_POINTERS_FROM_TEMPORARIES]); |
| 45 | +declare_lint_pass!(DanglingPointers => [DANGLING_POINTERS_FROM_TEMPORARIES]); |
72 | 46 |
|
73 | 47 | impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
|
74 | 48 | fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
75 |
| - if let ExprKind::MethodCall(as_ptr_path, as_ptr_receiver, ..) = expr.kind |
76 |
| - && as_ptr_path.ident.name == sym::as_ptr |
77 |
| - && let ExprKind::MethodCall(unwrap_path, unwrap_receiver, ..) = as_ptr_receiver.kind |
78 |
| - && (unwrap_path.ident.name == sym::unwrap || unwrap_path.ident.name == sym::expect) |
79 |
| - && let source_type = cx.typeck_results().expr_ty(unwrap_receiver) |
80 |
| - && let ty::Adt(def, args) = source_type.kind() |
81 |
| - && cx.tcx.is_diagnostic_item(sym::Result, def.did()) |
82 |
| - && let ty = args.type_at(0) |
83 |
| - && let ty::Adt(adt, _) = ty.kind() |
84 |
| - && cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) |
85 |
| - { |
86 |
| - cx.emit_span_lint( |
87 |
| - TEMPORARY_CSTRING_AS_PTR, |
88 |
| - as_ptr_path.ident.span, |
89 |
| - InstantlyDangling { |
90 |
| - callee: as_ptr_path.ident.name, |
91 |
| - ty, |
92 |
| - ptr_span: as_ptr_path.ident.span, |
93 |
| - temporary_span: as_ptr_receiver.span, |
94 |
| - }, |
95 |
| - ); |
96 |
| - return; // One lint is enough |
97 |
| - } |
98 |
| - |
99 | 49 | if let ExprKind::MethodCall(method, receiver, _args, _span) = expr.kind
|
100 | 50 | && matches!(method.ident.name, sym::as_ptr | sym::as_mut_ptr)
|
101 | 51 | && is_temporary_rvalue(receiver)
|
|
0 commit comments