Skip to content

Commit aa0db0b

Browse files
authored
Rollup merge of #71197 - ljedrz:unsafe_unused, r=ecstatic-morse
Don't use the HirId to NodeId map in MIR Another step towards not having to build a `HirId` to `NodeId` map other than for doc and RLS purposes. We are currently sorting `unsafe` blocks by `NodeId` in `check_unsafety`; change it to sorting by `Span` instead; this passes the tests, but better ideas are welcome. In addition, simplify the split between the used and unused `unsafe` blocks for readability and less sorting. cc #50928
2 parents c68c71e + 66575c9 commit aa0db0b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/librustc_mir/transform/check_unsafety.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,19 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) {
641641
}
642642
}
643643

644-
let mut unsafe_blocks: Vec<_> = unsafe_blocks.iter().collect();
645-
unsafe_blocks.sort_by_cached_key(|(hir_id, _)| tcx.hir().hir_id_to_node_id(*hir_id));
646-
let used_unsafe: FxHashSet<_> =
647-
unsafe_blocks.iter().flat_map(|&&(id, used)| used.then_some(id)).collect();
648-
for &(block_id, is_used) in unsafe_blocks {
649-
if !is_used {
650-
report_unused_unsafe(tcx, &used_unsafe, block_id);
644+
let (mut unsafe_used, mut unsafe_unused): (FxHashSet<_>, Vec<_>) = Default::default();
645+
for &(block_id, is_used) in unsafe_blocks.iter() {
646+
if is_used {
647+
unsafe_used.insert(block_id);
648+
} else {
649+
unsafe_unused.push(block_id);
651650
}
652651
}
652+
// The unused unsafe blocks might not be in source order; sort them so that the unused unsafe
653+
// error messages are properly aligned and the issue-45107 and lint-unused-unsafe tests pass.
654+
unsafe_unused.sort_by_cached_key(|hir_id| tcx.hir().span(*hir_id));
655+
656+
for &block_id in &unsafe_unused {
657+
report_unused_unsafe(tcx, &unsafe_used, block_id);
658+
}
653659
}

0 commit comments

Comments
 (0)