|
1 | 1 | use rustc_ast::Mutability; |
2 | | -use rustc_data_structures::fx::FxHashMap; |
3 | | -use rustc_hir::{def::Res, Expr, ExprKind, HirId, Local, QPath, StmtKind, UnOp}; |
| 2 | +use rustc_hir::{Expr, ExprKind, UnOp}; |
4 | 3 | use rustc_middle::ty::{self, TypeAndMut}; |
5 | | -use rustc_span::{sym, Span}; |
| 4 | +use rustc_span::sym; |
6 | 5 |
|
7 | 6 | use crate::{lints::InvalidReferenceCastingDiag, LateContext, LateLintPass, LintContext}; |
8 | 7 |
|
@@ -34,38 +33,18 @@ declare_lint! { |
34 | 33 | "casts of `&T` to `&mut T` without interior mutability" |
35 | 34 | } |
36 | 35 |
|
37 | | -#[derive(Default)] |
38 | | -pub struct InvalidReferenceCasting { |
39 | | - casted: FxHashMap<HirId, Span>, |
40 | | -} |
41 | | - |
42 | | -impl_lint_pass!(InvalidReferenceCasting => [INVALID_REFERENCE_CASTING]); |
| 36 | +declare_lint_pass!(InvalidReferenceCasting => [INVALID_REFERENCE_CASTING]); |
43 | 37 |
|
44 | 38 | impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting { |
45 | | - fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx rustc_hir::Stmt<'tcx>) { |
46 | | - let StmtKind::Local(local) = stmt.kind else { |
47 | | - return; |
48 | | - }; |
49 | | - let Local { init: Some(init), els: None, .. } = local else { |
50 | | - return; |
51 | | - }; |
52 | | - |
53 | | - if is_cast_from_const_to_mut(cx, init) { |
54 | | - self.casted.insert(local.pat.hir_id, init.span); |
55 | | - } |
56 | | - } |
57 | | - |
58 | 39 | fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
59 | 40 | let Some((is_assignment, e)) = is_operation_we_care_about(cx, expr) else { |
60 | 41 | return; |
61 | 42 | }; |
62 | 43 |
|
63 | | - let orig_cast = if is_cast_from_const_to_mut(cx, e) { |
64 | | - None |
65 | | - } else if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind |
66 | | - && let Res::Local(hir_id) = &path.res |
67 | | - && let Some(orig_cast) = self.casted.get(hir_id) { |
68 | | - Some(*orig_cast) |
| 44 | + let init = cx.expr_or_init(e); |
| 45 | + |
| 46 | + let orig_cast = if is_cast_from_const_to_mut(cx, init) { |
| 47 | + if init.span != e.span { Some(init.span) } else { None } |
69 | 48 | } else { |
70 | 49 | return; |
71 | 50 | }; |
@@ -125,99 +104,51 @@ fn is_operation_we_care_about<'tcx>( |
125 | 104 | deref_assign_or_addr_of(e).or_else(|| ptr_write(cx, e)) |
126 | 105 | } |
127 | 106 |
|
128 | | -fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool { |
129 | | - let e = e.peel_blocks(); |
| 107 | +fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, orig_expr: &'tcx Expr<'tcx>) -> bool { |
| 108 | + let mut need_check_freeze = false; |
| 109 | + let mut e = orig_expr; |
130 | 110 |
|
131 | | - fn from_casts<'tcx>( |
132 | | - cx: &LateContext<'tcx>, |
133 | | - e: &'tcx Expr<'tcx>, |
134 | | - need_check_freeze: &mut bool, |
135 | | - ) -> Option<&'tcx Expr<'tcx>> { |
136 | | - // <expr> as *mut ... |
137 | | - let mut e = if let ExprKind::Cast(e, t) = e.kind |
138 | | - && let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() { |
139 | | - e |
140 | | - // <expr>.cast_mut() |
| 111 | + let end_ty = cx.typeck_results().node_type(orig_expr.hir_id); |
| 112 | + |
| 113 | + // Bail out early if the end type is **not** a mutable pointer. |
| 114 | + if !matches!(end_ty.kind(), ty::RawPtr(TypeAndMut { ty: _, mutbl: Mutability::Mut })) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + loop { |
| 119 | + e = e.peel_blocks(); |
| 120 | + // <expr> as ... |
| 121 | + e = if let ExprKind::Cast(expr, _) = e.kind { |
| 122 | + expr |
| 123 | + // <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const() |
141 | 124 | } else if let ExprKind::MethodCall(_, expr, [], _) = e.kind |
142 | 125 | && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) |
143 | | - && cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) { |
| 126 | + && matches!( |
| 127 | + cx.tcx.get_diagnostic_name(def_id), |
| 128 | + Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const) |
| 129 | + ) |
| 130 | + { |
144 | 131 | expr |
145 | | - // UnsafeCell::raw_get(<expr>) |
| 132 | + // ptr::from_ref(<expr>), UnsafeCell::raw_get(<expr>) or mem::transmute<_, _>(<expr>) |
146 | 133 | } else if let ExprKind::Call(path, [arg]) = e.kind |
147 | 134 | && let ExprKind::Path(ref qpath) = path.kind |
148 | 135 | && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() |
149 | | - && cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id) |
| 136 | + && matches!( |
| 137 | + cx.tcx.get_diagnostic_name(def_id), |
| 138 | + Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get | sym::transmute) |
| 139 | + ) |
150 | 140 | { |
151 | | - *need_check_freeze = true; |
| 141 | + if cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id) { |
| 142 | + need_check_freeze = true; |
| 143 | + } |
152 | 144 | arg |
153 | 145 | } else { |
154 | | - return None; |
| 146 | + break; |
155 | 147 | }; |
156 | | - |
157 | | - let mut had_at_least_one_cast = false; |
158 | | - loop { |
159 | | - e = e.peel_blocks(); |
160 | | - // <expr> as *mut/const ... or <expr> as <uint> |
161 | | - e = if let ExprKind::Cast(expr, t) = e.kind |
162 | | - && matches!(cx.typeck_results().node_type(t.hir_id).kind(), ty::RawPtr(_) | ty::Uint(_)) { |
163 | | - had_at_least_one_cast = true; |
164 | | - expr |
165 | | - // <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const() |
166 | | - } else if let ExprKind::MethodCall(_, expr, [], _) = e.kind |
167 | | - && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) |
168 | | - && matches!( |
169 | | - cx.tcx.get_diagnostic_name(def_id), |
170 | | - Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const) |
171 | | - ) |
172 | | - { |
173 | | - had_at_least_one_cast = true; |
174 | | - expr |
175 | | - // ptr::from_ref(<expr>) or UnsafeCell::raw_get(<expr>) |
176 | | - } else if let ExprKind::Call(path, [arg]) = e.kind |
177 | | - && let ExprKind::Path(ref qpath) = path.kind |
178 | | - && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() |
179 | | - && matches!( |
180 | | - cx.tcx.get_diagnostic_name(def_id), |
181 | | - Some(sym::ptr_from_ref | sym::unsafe_cell_raw_get) |
182 | | - ) |
183 | | - { |
184 | | - if cx.tcx.is_diagnostic_item(sym::unsafe_cell_raw_get, def_id) { |
185 | | - *need_check_freeze = true; |
186 | | - } |
187 | | - return Some(arg); |
188 | | - } else if had_at_least_one_cast { |
189 | | - return Some(e); |
190 | | - } else { |
191 | | - return None; |
192 | | - }; |
193 | | - } |
194 | | - } |
195 | | - |
196 | | - fn from_transmute<'tcx>( |
197 | | - cx: &LateContext<'tcx>, |
198 | | - e: &'tcx Expr<'tcx>, |
199 | | - ) -> Option<&'tcx Expr<'tcx>> { |
200 | | - // mem::transmute::<_, *mut _>(<expr>) |
201 | | - if let ExprKind::Call(path, [arg]) = e.kind |
202 | | - && let ExprKind::Path(ref qpath) = path.kind |
203 | | - && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() |
204 | | - && cx.tcx.is_diagnostic_item(sym::transmute, def_id) |
205 | | - && let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(e.hir_id).kind() { |
206 | | - Some(arg) |
207 | | - } else { |
208 | | - None |
209 | | - } |
210 | 148 | } |
211 | 149 |
|
212 | | - let mut need_check_freeze = false; |
213 | | - let Some(e) = from_casts(cx, e, &mut need_check_freeze).or_else(|| from_transmute(cx, e)) |
214 | | - else { |
215 | | - return false; |
216 | | - }; |
217 | | - |
218 | | - let e = e.peel_blocks(); |
219 | | - let node_type = cx.typeck_results().node_type(e.hir_id); |
220 | | - if let ty::Ref(_, inner_ty, Mutability::Not) = node_type.kind() { |
| 150 | + let start_ty = cx.typeck_results().node_type(e.hir_id); |
| 151 | + if let ty::Ref(_, inner_ty, Mutability::Not) = start_ty.kind() { |
221 | 152 | // If an UnsafeCell method is involved we need to additionaly check the |
222 | 153 | // inner type for the presence of the Freeze trait (ie does NOT contain |
223 | 154 | // an UnsafeCell), since in that case we would incorrectly lint on valid casts. |
|
0 commit comments