Skip to content

Commit 3e44ca9

Browse files
committed
remove some unused code and types
1 parent ee3fc9d commit 3e44ca9

File tree

2 files changed

+17
-69
lines changed

2 files changed

+17
-69
lines changed

compiler/rustc_middle/src/mir/query.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::mir::{Body, ConstantKind, Promoted};
44
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
5-
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_data_structures::fx::FxHashSet;
66
use rustc_data_structures::vec_map::VecMap;
77
use rustc_errors::ErrorGuaranteed;
88
use rustc_hir as hir;
@@ -115,21 +115,6 @@ pub enum UnusedUnsafe {
115115
/// `unsafe` block nested under another (used) `unsafe` block
116116
/// > ``… because it's nested under this `unsafe` block``
117117
InUnsafeBlock(hir::HirId),
118-
/// `unsafe` block nested under `unsafe fn`
119-
/// > ``… because it's nested under this `unsafe fn` ``
120-
///
121-
/// the second HirId here indicates the first usage of the `unsafe` block,
122-
/// which allows retrieval of the LintLevelSource for why that operation would
123-
/// have been permitted without the block
124-
InUnsafeFn(hir::HirId, hir::HirId),
125-
}
126-
127-
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
128-
pub enum UsedUnsafeBlockData {
129-
SomeDisallowedInUnsafeFn,
130-
// the HirId here indicates the first usage of the `unsafe` block
131-
// (i.e. the one that's first encountered in the MIR traversal of the unsafety check)
132-
AllAllowedInUnsafeFn(hir::HirId),
133118
}
134119

135120
#[derive(TyEncodable, TyDecodable, HashStable, Debug)]
@@ -138,10 +123,7 @@ pub struct UnsafetyCheckResult {
138123
pub violations: Vec<UnsafetyViolation>,
139124

140125
/// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint.
141-
///
142-
/// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
143-
/// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
144-
pub used_unsafe_blocks: FxHashMap<hir::HirId, UsedUnsafeBlockData>,
126+
pub used_unsafe_blocks: FxHashSet<hir::HirId>,
145127

146128
/// This is `Some` iff the item is not a closure.
147129
pub unused_unsafes: Option<Vec<(hir::HirId, UnusedUnsafe)>>,

compiler/rustc_mir_transform/src/check_unsafety.rs

+15-49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::fx::FxHashMap;
1+
use rustc_data_structures::fx::FxHashSet;
22
use rustc_errors::struct_span_err;
33
use rustc_hir as hir;
44
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -11,7 +11,6 @@ use rustc_middle::ty::{self, TyCtxt};
1111
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
1212
use rustc_session::lint::Level;
1313

14-
use std::collections::hash_map;
1514
use std::ops::Bound;
1615

1716
pub struct UnsafetyChecker<'a, 'tcx> {
@@ -26,7 +25,7 @@ pub struct UnsafetyChecker<'a, 'tcx> {
2625
///
2726
/// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
2827
/// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
29-
used_unsafe_blocks: FxHashMap<HirId, UsedUnsafeBlockData>,
28+
used_unsafe_blocks: FxHashSet<HirId>,
3029
}
3130

3231
impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
@@ -130,10 +129,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
130129
&AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => {
131130
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
132131
self.tcx.unsafety_check_result(def_id);
133-
self.register_violations(
134-
violations,
135-
used_unsafe_blocks.iter().map(|(&h, &d)| (h, d)),
136-
);
132+
self.register_violations(violations, used_unsafe_blocks.iter().copied());
137133
}
138134
},
139135
_ => {}
@@ -257,22 +253,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
257253
fn register_violations<'a>(
258254
&mut self,
259255
violations: impl IntoIterator<Item = &'a UnsafetyViolation>,
260-
new_used_unsafe_blocks: impl IntoIterator<Item = (HirId, UsedUnsafeBlockData)>,
256+
new_used_unsafe_blocks: impl IntoIterator<Item = HirId>,
261257
) {
262-
use UsedUnsafeBlockData::*;
263-
264-
let update_entry = |this: &mut Self, hir_id, new_usage| {
265-
match this.used_unsafe_blocks.entry(hir_id) {
266-
hash_map::Entry::Occupied(mut entry) => {
267-
if new_usage == SomeDisallowedInUnsafeFn {
268-
*entry.get_mut() = SomeDisallowedInUnsafeFn;
269-
}
270-
}
271-
hash_map::Entry::Vacant(entry) => {
272-
entry.insert(new_usage);
273-
}
274-
};
275-
};
276258
let safety = self.body.source_scopes[self.source_info.scope]
277259
.local_data
278260
.as_ref()
@@ -300,17 +282,13 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
300282
}),
301283
Safety::BuiltinUnsafe => {}
302284
Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| {
303-
update_entry(
304-
self,
305-
hir_id,
306-
SomeDisallowedInUnsafeFn,
307-
)
285+
self.used_unsafe_blocks.insert(hir_id);
308286
}),
309287
};
310288

311-
new_used_unsafe_blocks
312-
.into_iter()
313-
.for_each(|(hir_id, usage_data)| update_entry(self, hir_id, usage_data));
289+
new_used_unsafe_blocks.into_iter().for_each(|hir_id| {
290+
self.used_unsafe_blocks.insert(hir_id);
291+
});
314292
}
315293
fn check_mut_borrowing_layout_constrained_field(
316294
&mut self,
@@ -407,34 +385,28 @@ enum Context {
407385

408386
struct UnusedUnsafeVisitor<'a, 'tcx> {
409387
tcx: TyCtxt<'tcx>,
410-
used_unsafe_blocks: &'a FxHashMap<HirId, UsedUnsafeBlockData>,
388+
used_unsafe_blocks: &'a FxHashSet<HirId>,
411389
context: Context,
412390
unused_unsafes: &'a mut Vec<(HirId, UnusedUnsafe)>,
413391
}
414392

415393
impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
416394
fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) {
417-
use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn};
418-
419395
if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules {
420396
let used = match self.tcx.lint_level_at_node(UNUSED_UNSAFE, block.hir_id) {
421-
(Level::Allow, _) => Some(SomeDisallowedInUnsafeFn),
422-
_ => self.used_unsafe_blocks.get(&block.hir_id).copied(),
397+
(Level::Allow, _) => true,
398+
_ => self.used_unsafe_blocks.contains(&block.hir_id),
423399
};
424400
let unused_unsafe = match (self.context, used) {
425-
(_, None) => UnusedUnsafe::Unused,
426-
(Context::Safe, Some(_))
427-
| (Context::UnsafeFn(_), Some(SomeDisallowedInUnsafeFn)) => {
401+
(_, false) => UnusedUnsafe::Unused,
402+
(Context::Safe, true) | (Context::UnsafeFn(_), true) => {
428403
let previous_context = self.context;
429404
self.context = Context::UnsafeBlock(block.hir_id);
430405
intravisit::walk_block(self, block);
431406
self.context = previous_context;
432407
return;
433408
}
434-
(Context::UnsafeFn(hir_id), Some(AllAllowedInUnsafeFn(lint_root))) => {
435-
UnusedUnsafe::InUnsafeFn(hir_id, lint_root)
436-
}
437-
(Context::UnsafeBlock(hir_id), Some(_)) => UnusedUnsafe::InUnsafeBlock(hir_id),
409+
(Context::UnsafeBlock(hir_id), true) => UnusedUnsafe::InUnsafeBlock(hir_id),
438410
};
439411
self.unused_unsafes.push((block.hir_id, unused_unsafe));
440412
}
@@ -458,7 +430,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
458430
fn check_unused_unsafe(
459431
tcx: TyCtxt<'_>,
460432
def_id: LocalDefId,
461-
used_unsafe_blocks: &FxHashMap<HirId, UsedUnsafeBlockData>,
433+
used_unsafe_blocks: &FxHashSet<HirId>,
462434
) -> Vec<(HirId, UnusedUnsafe)> {
463435
let body_id = tcx.hir().maybe_body_owned_by(def_id);
464436

@@ -518,11 +490,6 @@ fn unsafety_check_result<'tcx>(
518490
}
519491

520492
fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
521-
if matches!(kind, UnusedUnsafe::InUnsafeFn(..)) {
522-
// We do *not* warn here, these unsafe blocks are actually required when
523-
// `unsafe_op_in_unsafe_fn` is warn or higher.
524-
return;
525-
}
526493
let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id));
527494
tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
528495
let msg = "unnecessary `unsafe` block";
@@ -536,7 +503,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
536503
"because it's nested under this `unsafe` block",
537504
);
538505
}
539-
UnusedUnsafe::InUnsafeFn(_id, _usage_lint_root) => unreachable!(),
540506
}
541507

542508
db.emit();

0 commit comments

Comments
 (0)