1
Fork 0

remove some unused code and types

This commit is contained in:
Ralf Jung 2022-08-02 17:04:52 -04:00
parent ee3fc9dff8
commit 3e44ca95dd
2 changed files with 17 additions and 69 deletions

View file

@ -2,7 +2,7 @@
use crate::mir::{Body, ConstantKind, Promoted}; use crate::mir::{Body, ConstantKind, Promoted};
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt}; use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::vec_map::VecMap; use rustc_data_structures::vec_map::VecMap;
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir; use rustc_hir as hir;
@ -115,21 +115,6 @@ pub enum UnusedUnsafe {
/// `unsafe` block nested under another (used) `unsafe` block /// `unsafe` block nested under another (used) `unsafe` block
/// > ``… because it's nested under this `unsafe` block`` /// > ``… because it's nested under this `unsafe` block``
InUnsafeBlock(hir::HirId), InUnsafeBlock(hir::HirId),
/// `unsafe` block nested under `unsafe fn`
/// > ``… because it's nested under this `unsafe fn` ``
///
/// the second HirId here indicates the first usage of the `unsafe` block,
/// which allows retrieval of the LintLevelSource for why that operation would
/// have been permitted without the block
InUnsafeFn(hir::HirId, hir::HirId),
}
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
pub enum UsedUnsafeBlockData {
SomeDisallowedInUnsafeFn,
// the HirId here indicates the first usage of the `unsafe` block
// (i.e. the one that's first encountered in the MIR traversal of the unsafety check)
AllAllowedInUnsafeFn(hir::HirId),
} }
#[derive(TyEncodable, TyDecodable, HashStable, Debug)] #[derive(TyEncodable, TyDecodable, HashStable, Debug)]
@ -138,10 +123,7 @@ pub struct UnsafetyCheckResult {
pub violations: Vec<UnsafetyViolation>, pub violations: Vec<UnsafetyViolation>,
/// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint. /// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint.
/// pub used_unsafe_blocks: FxHashSet<hir::HirId>,
/// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
/// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
pub used_unsafe_blocks: FxHashMap<hir::HirId, UsedUnsafeBlockData>,
/// This is `Some` iff the item is not a closure. /// This is `Some` iff the item is not a closure.
pub unused_unsafes: Option<Vec<(hir::HirId, UnusedUnsafe)>>, pub unused_unsafes: Option<Vec<(hir::HirId, UnusedUnsafe)>>,

View file

@ -1,4 +1,4 @@
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err; use rustc_errors::struct_span_err;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
@ -11,7 +11,6 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
use rustc_session::lint::Level; use rustc_session::lint::Level;
use std::collections::hash_map;
use std::ops::Bound; use std::ops::Bound;
pub struct UnsafetyChecker<'a, 'tcx> { pub struct UnsafetyChecker<'a, 'tcx> {
@ -26,7 +25,7 @@ pub struct UnsafetyChecker<'a, 'tcx> {
/// ///
/// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
/// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
used_unsafe_blocks: FxHashMap<HirId, UsedUnsafeBlockData>, used_unsafe_blocks: FxHashSet<HirId>,
} }
impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> { impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
@ -130,10 +129,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
&AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => { &AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => {
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } = let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
self.tcx.unsafety_check_result(def_id); self.tcx.unsafety_check_result(def_id);
self.register_violations( self.register_violations(violations, used_unsafe_blocks.iter().copied());
violations,
used_unsafe_blocks.iter().map(|(&h, &d)| (h, d)),
);
} }
}, },
_ => {} _ => {}
@ -257,22 +253,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
fn register_violations<'a>( fn register_violations<'a>(
&mut self, &mut self,
violations: impl IntoIterator<Item = &'a UnsafetyViolation>, violations: impl IntoIterator<Item = &'a UnsafetyViolation>,
new_used_unsafe_blocks: impl IntoIterator<Item = (HirId, UsedUnsafeBlockData)>, new_used_unsafe_blocks: impl IntoIterator<Item = HirId>,
) { ) {
use UsedUnsafeBlockData::*;
let update_entry = |this: &mut Self, hir_id, new_usage| {
match this.used_unsafe_blocks.entry(hir_id) {
hash_map::Entry::Occupied(mut entry) => {
if new_usage == SomeDisallowedInUnsafeFn {
*entry.get_mut() = SomeDisallowedInUnsafeFn;
}
}
hash_map::Entry::Vacant(entry) => {
entry.insert(new_usage);
}
};
};
let safety = self.body.source_scopes[self.source_info.scope] let safety = self.body.source_scopes[self.source_info.scope]
.local_data .local_data
.as_ref() .as_ref()
@ -300,17 +282,13 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
}), }),
Safety::BuiltinUnsafe => {} Safety::BuiltinUnsafe => {}
Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| { Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| {
update_entry( self.used_unsafe_blocks.insert(hir_id);
self,
hir_id,
SomeDisallowedInUnsafeFn,
)
}), }),
}; };
new_used_unsafe_blocks new_used_unsafe_blocks.into_iter().for_each(|hir_id| {
.into_iter() self.used_unsafe_blocks.insert(hir_id);
.for_each(|(hir_id, usage_data)| update_entry(self, hir_id, usage_data)); });
} }
fn check_mut_borrowing_layout_constrained_field( fn check_mut_borrowing_layout_constrained_field(
&mut self, &mut self,
@ -407,34 +385,28 @@ enum Context {
struct UnusedUnsafeVisitor<'a, 'tcx> { struct UnusedUnsafeVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
used_unsafe_blocks: &'a FxHashMap<HirId, UsedUnsafeBlockData>, used_unsafe_blocks: &'a FxHashSet<HirId>,
context: Context, context: Context,
unused_unsafes: &'a mut Vec<(HirId, UnusedUnsafe)>, unused_unsafes: &'a mut Vec<(HirId, UnusedUnsafe)>,
} }
impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> { impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) { fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) {
use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn};
if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules { if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules {
let used = match self.tcx.lint_level_at_node(UNUSED_UNSAFE, block.hir_id) { let used = match self.tcx.lint_level_at_node(UNUSED_UNSAFE, block.hir_id) {
(Level::Allow, _) => Some(SomeDisallowedInUnsafeFn), (Level::Allow, _) => true,
_ => self.used_unsafe_blocks.get(&block.hir_id).copied(), _ => self.used_unsafe_blocks.contains(&block.hir_id),
}; };
let unused_unsafe = match (self.context, used) { let unused_unsafe = match (self.context, used) {
(_, None) => UnusedUnsafe::Unused, (_, false) => UnusedUnsafe::Unused,
(Context::Safe, Some(_)) (Context::Safe, true) | (Context::UnsafeFn(_), true) => {
| (Context::UnsafeFn(_), Some(SomeDisallowedInUnsafeFn)) => {
let previous_context = self.context; let previous_context = self.context;
self.context = Context::UnsafeBlock(block.hir_id); self.context = Context::UnsafeBlock(block.hir_id);
intravisit::walk_block(self, block); intravisit::walk_block(self, block);
self.context = previous_context; self.context = previous_context;
return; return;
} }
(Context::UnsafeFn(hir_id), Some(AllAllowedInUnsafeFn(lint_root))) => { (Context::UnsafeBlock(hir_id), true) => UnusedUnsafe::InUnsafeBlock(hir_id),
UnusedUnsafe::InUnsafeFn(hir_id, lint_root)
}
(Context::UnsafeBlock(hir_id), Some(_)) => UnusedUnsafe::InUnsafeBlock(hir_id),
}; };
self.unused_unsafes.push((block.hir_id, unused_unsafe)); self.unused_unsafes.push((block.hir_id, unused_unsafe));
} }
@ -458,7 +430,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
fn check_unused_unsafe( fn check_unused_unsafe(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
def_id: LocalDefId, def_id: LocalDefId,
used_unsafe_blocks: &FxHashMap<HirId, UsedUnsafeBlockData>, used_unsafe_blocks: &FxHashSet<HirId>,
) -> Vec<(HirId, UnusedUnsafe)> { ) -> Vec<(HirId, UnusedUnsafe)> {
let body_id = tcx.hir().maybe_body_owned_by(def_id); let body_id = tcx.hir().maybe_body_owned_by(def_id);
@ -518,11 +490,6 @@ fn unsafety_check_result<'tcx>(
} }
fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
if matches!(kind, UnusedUnsafe::InUnsafeFn(..)) {
// We do *not* warn here, these unsafe blocks are actually required when
// `unsafe_op_in_unsafe_fn` is warn or higher.
return;
}
let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id)); let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id));
tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| { tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
let msg = "unnecessary `unsafe` block"; let msg = "unnecessary `unsafe` block";
@ -536,7 +503,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
"because it's nested under this `unsafe` block", "because it's nested under this `unsafe` block",
); );
} }
UnusedUnsafe::InUnsafeFn(_id, _usage_lint_root) => unreachable!(),
} }
db.emit(); db.emit();