Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "unnecessary unsafe block in unsafe fn" lint #69245

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit;
use rustc_hir::Node;
use rustc_session::lint::builtin::{SAFE_PACKED_BORROWS, UNUSED_UNSAFE};
use rustc_span::symbol::{sym, Symbol};

Expand Down Expand Up @@ -548,14 +547,6 @@ fn is_enclosed(
if parent_id != id {
if used_unsafe.contains(&parent_id) {
Some(("block".to_string(), parent_id))
} else if let Some(Node::Item(&hir::Item {
kind: hir::ItemKind::Fn(ref sig, _, _), ..
})) = tcx.hir().find(parent_id)
{
match sig.header.unsafety {
hir::Unsafety::Unsafe => Some(("fn".to_string(), parent_id)),
hir::Unsafety::Normal => None,
}
} else {
is_enclosed(tcx, used_unsafe, parent_id)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
BlockSafety::ExplicitUnsafe(hir_id) => {
assert_eq!(self.push_unsafe_count, 0);
match self.unpushed_unsafe {
Safety::Safe => {}
Safety::Safe | Safety::FnUnsafe => {}
_ => return,
}
self.unpushed_unsafe = Safety::ExplicitUnsafe(hir_id);
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/lint/issue-69173-unsafe-block-in-unsafe-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Regression test for #69173: do not warn for `unsafe` blocks in `unsafe` functions

// check-pass

#![allow(dead_code)]
#![deny(unused_unsafe)]

unsafe fn foo() {}
unsafe fn bar() { unsafe { foo(); } }

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/span/lint-unused-unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn bad1() { unsafe {} } //~ ERROR: unnecessary `unsafe` block
fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary `unsafe` block
unsafe fn bad3() { unsafe {} } //~ ERROR: unnecessary `unsafe` block
fn bad4() { unsafe { callback(||{}) } } //~ ERROR: unnecessary `unsafe` block
unsafe fn bad5() { unsafe { unsf() } } //~ ERROR: unnecessary `unsafe` block
unsafe fn bad5() { unsafe { unsf() } }
fn bad6() {
unsafe { // don't put the warning here
unsafe { //~ ERROR: unnecessary `unsafe` block
Expand All @@ -26,7 +26,7 @@ fn bad6() {
}
}
unsafe fn bad7() {
unsafe { //~ ERROR: unnecessary `unsafe` block
unsafe {
unsafe { //~ ERROR: unnecessary `unsafe` block
unsf()
}
Expand Down
25 changes: 3 additions & 22 deletions src/test/ui/span/lint-unused-unsafe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,14 @@ error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:18:20
|
LL | unsafe fn bad3() { unsafe {} }
| ---------------- ^^^^^^ unnecessary `unsafe` block
| |
| because it's nested under this `unsafe` fn
| ^^^^^^ unnecessary `unsafe` block

error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:19:13
|
LL | fn bad4() { unsafe { callback(||{}) } }
| ^^^^^^ unnecessary `unsafe` block

error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:20:20
|
LL | unsafe fn bad5() { unsafe { unsf() } }
| ---------------- ^^^^^^ unnecessary `unsafe` block
| |
| because it's nested under this `unsafe` fn

error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:23:9
|
Expand All @@ -46,22 +36,13 @@ LL | unsafe { // don't put the warning here
LL | unsafe {
| ^^^^^^ unnecessary `unsafe` block

error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:29:5
|
LL | unsafe fn bad7() {
| ---------------- because it's nested under this `unsafe` fn
LL | unsafe {
| ^^^^^^ unnecessary `unsafe` block

error: unnecessary `unsafe` block
--> $DIR/lint-unused-unsafe.rs:30:9
|
LL | unsafe fn bad7() {
| ---------------- because it's nested under this `unsafe` fn
LL | unsafe {
| ------ because it's nested under this `unsafe` block
LL | unsafe {
| ^^^^^^ unnecessary `unsafe` block

error: aborting due to 8 previous errors
error: aborting due to 6 previous errors