|
20 | 20 | //! If you define a new `LateLintPass`, you will also need to add it to the |
21 | 21 | //! `late_lint_methods!` invocation in `lib.rs`. |
22 | 22 |
|
23 | | -use std::fmt::Write; |
24 | | - |
25 | 23 | use ast::token::TokenKind; |
26 | 24 | use rustc_ast::tokenstream::{TokenStream, TokenTree}; |
27 | 25 | use rustc_ast::visit::{FnCtxt, FnKind}; |
@@ -61,12 +59,12 @@ use crate::lints::{ |
61 | 59 | BuiltinEllipsisInclusiveRangePatternsLint, BuiltinExplicitOutlives, |
62 | 60 | BuiltinExplicitOutlivesSuggestion, BuiltinFeatureIssueNote, BuiltinIncompleteFeatures, |
63 | 61 | BuiltinIncompleteFeaturesHelp, BuiltinInternalFeatures, BuiltinKeywordIdents, |
64 | | - BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, BuiltinMissingDoc, BuiltinMutablesTransmutes, |
65 | | - BuiltinNoMangleGeneric, BuiltinNonShorthandFieldPatterns, BuiltinSpecialModuleNameUsed, |
66 | | - BuiltinTrivialBounds, BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller, |
67 | | - BuiltinUnpermittedTypeInit, BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, |
68 | | - BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub, |
69 | | - BuiltinWhileTrue, InvalidAsmLabel, |
| 62 | + BuiltinMissingCopyImpl, BuiltinMissingDebugImpl, BuiltinMissingDoc, BuiltinNoMangleGeneric, |
| 63 | + BuiltinNonShorthandFieldPatterns, BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, |
| 64 | + BuiltinTypeAliasBounds, BuiltinUngatedAsyncFnTrackCaller, BuiltinUnpermittedTypeInit, |
| 65 | + BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, BuiltinUnsafe, |
| 66 | + BuiltinUnsafeCellTransmutes, BuiltinUnstableFeatures, BuiltinUnusedDocComment, |
| 67 | + BuiltinUnusedDocCommentSub, BuiltinWhileTrue, InvalidAsmLabel, |
70 | 68 | }; |
71 | 69 | use crate::nonstandard_style::{method_context, MethodLateContext}; |
72 | 70 | use crate::{ |
@@ -1100,72 +1098,6 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems { |
1100 | 1098 | } |
1101 | 1099 | } |
1102 | 1100 |
|
1103 | | -declare_lint! { |
1104 | | - /// The `mutable_transmutes` lint catches transmuting from `&T` to `&mut |
1105 | | - /// T` because it is [undefined behavior]. |
1106 | | - /// |
1107 | | - /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html |
1108 | | - /// |
1109 | | - /// ### Example |
1110 | | - /// |
1111 | | - /// ```rust,compile_fail |
1112 | | - /// unsafe { |
1113 | | - /// let y = std::mem::transmute::<&i32, &mut i32>(&5); |
1114 | | - /// } |
1115 | | - /// ``` |
1116 | | - /// |
1117 | | - /// {{produces}} |
1118 | | - /// |
1119 | | - /// ### Explanation |
1120 | | - /// |
1121 | | - /// Certain assumptions are made about aliasing of data, and this transmute |
1122 | | - /// violates those assumptions. Consider using [`UnsafeCell`] instead. |
1123 | | - /// |
1124 | | - /// [`UnsafeCell`]: https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html |
1125 | | - MUTABLE_TRANSMUTES, |
1126 | | - Deny, |
1127 | | - "transmuting &T to &mut T is undefined behavior, even if the reference is unused" |
1128 | | -} |
1129 | | - |
1130 | | -declare_lint_pass!(MutableTransmutes => [MUTABLE_TRANSMUTES]); |
1131 | | - |
1132 | | -impl<'tcx> LateLintPass<'tcx> for MutableTransmutes { |
1133 | | - fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) { |
1134 | | - if let Some((&ty::Ref(_, _, from_mutbl), &ty::Ref(_, _, to_mutbl))) = |
1135 | | - get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind())) |
1136 | | - { |
1137 | | - if from_mutbl < to_mutbl { |
1138 | | - cx.emit_span_lint(MUTABLE_TRANSMUTES, expr.span, BuiltinMutablesTransmutes); |
1139 | | - } |
1140 | | - } |
1141 | | - |
1142 | | - fn get_transmute_from_to<'tcx>( |
1143 | | - cx: &LateContext<'tcx>, |
1144 | | - expr: &hir::Expr<'_>, |
1145 | | - ) -> Option<(Ty<'tcx>, Ty<'tcx>)> { |
1146 | | - let def = if let hir::ExprKind::Path(ref qpath) = expr.kind { |
1147 | | - cx.qpath_res(qpath, expr.hir_id) |
1148 | | - } else { |
1149 | | - return None; |
1150 | | - }; |
1151 | | - if let Res::Def(DefKind::Fn, did) = def { |
1152 | | - if !def_id_is_transmute(cx, did) { |
1153 | | - return None; |
1154 | | - } |
1155 | | - let sig = cx.typeck_results().node_type(expr.hir_id).fn_sig(cx.tcx); |
1156 | | - let from = sig.inputs().skip_binder()[0]; |
1157 | | - let to = sig.output().skip_binder(); |
1158 | | - return Some((from, to)); |
1159 | | - } |
1160 | | - None |
1161 | | - } |
1162 | | - |
1163 | | - fn def_id_is_transmute(cx: &LateContext<'_>, def_id: DefId) -> bool { |
1164 | | - cx.tcx.is_intrinsic(def_id, sym::transmute) |
1165 | | - } |
1166 | | - } |
1167 | | -} |
1168 | | - |
1169 | 1101 | declare_lint! { |
1170 | 1102 | /// The `unstable_features` lint detects uses of `#![feature]`. |
1171 | 1103 | /// |
@@ -1612,7 +1544,6 @@ declare_lint_pass!( |
1612 | 1544 | UNUSED_DOC_COMMENTS, |
1613 | 1545 | NO_MANGLE_CONST_ITEMS, |
1614 | 1546 | NO_MANGLE_GENERIC_ITEMS, |
1615 | | - MUTABLE_TRANSMUTES, |
1616 | 1547 | UNSTABLE_FEATURES, |
1617 | 1548 | UNREACHABLE_PUB, |
1618 | 1549 | TYPE_ALIAS_BOUNDS, |
|
0 commit comments