From d167408cb4efff3c0a5e404dbe5f097b87fa54fa Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 16 Apr 2020 14:24:52 +0200 Subject: [PATCH 1/3] simplify unused unsafe block handling --- src/librustc_mir/transform/check_unsafety.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 3ce9b875e16cc..595b0432015b3 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -641,13 +641,17 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) { } } - let mut unsafe_blocks: Vec<_> = unsafe_blocks.iter().collect(); - unsafe_blocks.sort_by_cached_key(|(hir_id, _)| tcx.hir().hir_id_to_node_id(*hir_id)); - let used_unsafe: FxHashSet<_> = - unsafe_blocks.iter().flat_map(|&&(id, used)| used.then_some(id)).collect(); - for &(block_id, is_used) in unsafe_blocks { - if !is_used { - report_unused_unsafe(tcx, &used_unsafe, block_id); + let (mut unsafe_used, mut unsafe_unused): (FxHashSet<_>, Vec<_>) = Default::default(); + for &(block_id, is_used) in unsafe_blocks.iter() { + if is_used { + unsafe_used.insert(block_id); + } else { + unsafe_unused.push(block_id); } } + unsafe_unused.sort_by_cached_key(|hir_id| tcx.hir().hir_id_to_node_id(*hir_id)); + + for &block_id in &unsafe_unused { + report_unused_unsafe(tcx, &unsafe_used, block_id); + } } From 672b7682b0e836d87ad4e06e0fed2daaa6c065e6 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 16 Apr 2020 14:53:09 +0200 Subject: [PATCH 2/3] sort unused unsafe blocks by Span instead of NodeId --- src/librustc_mir/transform/check_unsafety.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 595b0432015b3..567fa42c83ae1 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -649,7 +649,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) { unsafe_unused.push(block_id); } } - unsafe_unused.sort_by_cached_key(|hir_id| tcx.hir().hir_id_to_node_id(*hir_id)); + unsafe_unused.sort_by_cached_key(|hir_id| tcx.hir().span(*hir_id)); for &block_id in &unsafe_unused { report_unused_unsafe(tcx, &unsafe_used, block_id); From 66575c9962188aa2f16df04fa1fef1786f0d216f Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 16 Apr 2020 20:00:54 +0200 Subject: [PATCH 3/3] comment on the sorting of unused unsafe blocks --- src/librustc_mir/transform/check_unsafety.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 567fa42c83ae1..9f6b1963ce794 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -649,6 +649,8 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) { unsafe_unused.push(block_id); } } + // The unused unsafe blocks might not be in source order; sort them so that the unused unsafe + // error messages are properly aligned and the issue-45107 and lint-unused-unsafe tests pass. unsafe_unused.sort_by_cached_key(|hir_id| tcx.hir().span(*hir_id)); for &block_id in &unsafe_unused {