Skip to content

Commit 83025b7

Browse files
committed
Implement new unused_unsafe-checking algorithm
1 parent 50f7e5b commit 83025b7

File tree

3 files changed

+162
-119
lines changed

3 files changed

+162
-119
lines changed

compiler/rustc_middle/src/mir/query.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::mir::{Body, Promoted};
44
use crate::ty::{self, Ty, TyCtxt};
5-
use rustc_data_structures::sync::Lrc;
5+
use rustc_data_structures::stable_map::FxHashMap;
66
use rustc_data_structures::vec_map::VecMap;
77
use rustc_errors::ErrorReported;
88
use rustc_hir as hir;
@@ -114,13 +114,32 @@ pub struct UnsafetyViolation {
114114
pub details: UnsafetyViolationDetails,
115115
}
116116

117-
#[derive(Clone, TyEncodable, TyDecodable, HashStable, Debug)]
117+
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
118+
pub enum UnusedUnsafe {
119+
/// `unsafe` block contains no unsafe operations
120+
// > ``unnecessary `unsafe` block``
121+
Unused,
122+
// `unsafe` block nested under another (used) `unsafe` block
123+
// ``… because it's nested under this `unsafe` block``
124+
InUnsafeBlock(hir::HirId),
125+
// `unsafe` block nested under `unsafe` fn
126+
// ``… because it's nested under this `unsafe` fn``
127+
InUnsafeFn(hir::HirId),
128+
}
129+
130+
#[derive(TyEncodable, TyDecodable, HashStable, Debug)]
118131
pub struct UnsafetyCheckResult {
119132
/// Violations that are propagated *upwards* from this function.
120-
pub violations: Lrc<[UnsafetyViolation]>,
121-
/// `unsafe` blocks in this function, along with whether they are used. This is
122-
/// used for the "unused_unsafe" lint.
123-
pub unsafe_blocks: Lrc<[(hir::HirId, bool)]>,
133+
pub violations: Vec<UnsafetyViolation>,
134+
135+
/// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint.
136+
///
137+
/// The keys are the used `unsafe` blocks, the boolean flag indicates whether
138+
/// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
139+
pub used_unsafe_blocks: FxHashMap<hir::HirId, bool>,
140+
141+
/// This is `Some` iff the item is not a closure.
142+
pub unused_unsafe: Option<Vec<(hir::HirId, UnusedUnsafe)>>,
124143
}
125144

126145
rustc_index::newtype_index! {

compiler/rustc_mir_build/src/build/block.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::build::ForGuard::OutsideGuard;
33
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
44
use rustc_middle::mir::*;
55
use rustc_middle::thir::*;
6-
use rustc_session::lint::builtin::UNSAFE_OP_IN_UNSAFE_FN;
7-
use rustc_session::lint::Level;
86
use rustc_span::Span;
97

108
impl<'a, 'tcx> Builder<'a, 'tcx> {

0 commit comments

Comments
 (0)