Skip to content

Commit 2136a5c

Browse files
Fix unused_unsafe label with `unsafe_block_in_unsafe_fn
1 parent 635ccfe commit 2136a5c

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

Diff for: compiler/rustc_mir/src/transform/check_unsafety.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -580,24 +580,23 @@ fn is_enclosed(
580580
tcx: TyCtxt<'_>,
581581
used_unsafe: &FxHashSet<hir::HirId>,
582582
id: hir::HirId,
583-
) -> Option<(String, hir::HirId)> {
583+
unsafe_op_in_unsafe_fn_allowed: bool,
584+
) -> Option<(&'static str, hir::HirId)> {
584585
let parent_id = tcx.hir().get_parent_node(id);
585586
if parent_id != id {
586587
if used_unsafe.contains(&parent_id) {
587-
Some(("block".to_string(), parent_id))
588+
Some(("block", parent_id))
588589
} else if let Some(Node::Item(&hir::Item {
589590
kind: hir::ItemKind::Fn(ref sig, _, _), ..
590591
})) = tcx.hir().find(parent_id)
591592
{
592-
if sig.header.unsafety == hir::Unsafety::Unsafe
593-
&& !tcx.features().unsafe_block_in_unsafe_fn
594-
{
595-
Some(("fn".to_string(), parent_id))
593+
if sig.header.unsafety == hir::Unsafety::Unsafe && unsafe_op_in_unsafe_fn_allowed {
594+
Some(("fn", parent_id))
596595
} else {
597596
None
598597
}
599598
} else {
600-
is_enclosed(tcx, used_unsafe, parent_id)
599+
is_enclosed(tcx, used_unsafe, parent_id, unsafe_op_in_unsafe_fn_allowed)
601600
}
602601
} else {
603602
None
@@ -610,7 +609,9 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, used_unsafe: &FxHashSet<hir::HirId>, id
610609
let msg = "unnecessary `unsafe` block";
611610
let mut db = lint.build(msg);
612611
db.span_label(span, msg);
613-
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {
612+
if let Some((kind, id)) =
613+
is_enclosed(tcx, used_unsafe, id, unsafe_op_in_unsafe_fn_allowed(tcx, id))
614+
{
614615
db.span_label(
615616
tcx.sess.source_map().guess_head_span(tcx.hir().span(id)),
616617
format!("because it's nested under this `unsafe` {}", kind),

Diff for: src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ unsafe fn deny_level() {
1313
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
1414
VOID = ();
1515
//~^ ERROR use of mutable static is unsafe and requires unsafe block
16+
17+
unsafe {}
18+
//~^ ERROR unnecessary `unsafe` block
1619
}
1720

1821
// Check that `unsafe_op_in_unsafe_fn` works starting from the `warn` level.
@@ -25,6 +28,8 @@ unsafe fn warning_level() {
2528
//~^ ERROR dereference of raw pointer is unsafe and requires unsafe block
2629
VOID = ();
2730
//~^ ERROR use of mutable static is unsafe and requires unsafe block
31+
unsafe {}
32+
//~^ ERROR unnecessary `unsafe` block
2833
}
2934

3035
unsafe fn explicit_block() {

Diff for: src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.stderr

+34-16
Original file line numberDiff line numberDiff line change
@@ -27,78 +27,96 @@ LL | VOID = ();
2727
|
2828
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
2929

30+
error: unnecessary `unsafe` block
31+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:17:5
32+
|
33+
LL | unsafe {}
34+
| ^^^^^^ unnecessary `unsafe` block
35+
|
36+
note: the lint level is defined here
37+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:3:9
38+
|
39+
LL | #![deny(unused_unsafe)]
40+
| ^^^^^^^^^^^^^
41+
3042
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
31-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:22:5
43+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:25:5
3244
|
3345
LL | unsf();
3446
| ^^^^^^ call to unsafe function
3547
|
3648
note: the lint level is defined here
37-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:20:8
49+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:23:8
3850
|
3951
LL | #[deny(warnings)]
4052
| ^^^^^^^^
4153
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`
4254
= note: consult the function's documentation for information on how to avoid undefined behavior
4355

4456
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
45-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:24:5
57+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:27:5
4658
|
4759
LL | *PTR;
4860
| ^^^^ dereference of raw pointer
4961
|
5062
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
5163

5264
error: use of mutable static is unsafe and requires unsafe block (error E0133)
53-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:26:5
65+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:29:5
5466
|
5567
LL | VOID = ();
5668
| ^^^^^^^^^ use of mutable static
5769
|
5870
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
5971

6072
error: unnecessary `unsafe` block
61-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:40:14
73+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5
74+
|
75+
LL | unsafe {}
76+
| ^^^^^^ unnecessary `unsafe` block
77+
78+
error: unnecessary `unsafe` block
79+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:45:14
6280
|
6381
LL | unsafe { unsafe { unsf() } }
6482
| ------ ^^^^^^ unnecessary `unsafe` block
6583
| |
6684
| because it's nested under this `unsafe` block
67-
|
68-
note: the lint level is defined here
69-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:3:9
70-
|
71-
LL | #![deny(unused_unsafe)]
72-
| ^^^^^^^^^^^^^
7385

7486
error: unnecessary `unsafe` block
75-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:51:5
87+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:56:5
7688
|
89+
LL | unsafe fn allow_level() {
90+
| ----------------------- because it's nested under this `unsafe` fn
91+
...
7792
LL | unsafe { unsf() }
7893
| ^^^^^^ unnecessary `unsafe` block
7994

8095
error: unnecessary `unsafe` block
81-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:63:9
96+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:68:9
8297
|
98+
LL | unsafe fn nested_allow_level() {
99+
| ------------------------------ because it's nested under this `unsafe` fn
100+
...
83101
LL | unsafe { unsf() }
84102
| ^^^^^^ unnecessary `unsafe` block
85103

86104
error[E0133]: call to unsafe function is unsafe and requires unsafe block
87-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:69:5
105+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:74:5
88106
|
89107
LL | unsf();
90108
| ^^^^^^ call to unsafe function
91109
|
92110
= note: consult the function's documentation for information on how to avoid undefined behavior
93111

94112
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
95-
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:73:9
113+
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:78:9
96114
|
97115
LL | unsf();
98116
| ^^^^^^ call to unsafe function
99117
|
100118
= note: consult the function's documentation for information on how to avoid undefined behavior
101119

102-
error: aborting due to 11 previous errors
120+
error: aborting due to 13 previous errors
103121

104122
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)