Skip to content

Commit a1a1409

Browse files
committed
Rollup merge of rust-lang#48317 - ExpHP:unused-unsafe-is-no-fn, r=estebank
unused_unsafe: don't label irrelevant fns Fixes rust-lang#48131 Diagnostic bugfix to remove an errant note. Stops the search for an enclosing unsafe scope at the first safe fn encountered. ```rust pub unsafe fn outer() { fn inner() { unsafe { /* unnecessary */ } } inner() } ``` **Before:** ``` warning: unnecessary `unsafe` block --> src/main.rs:3:9 | 1 | pub unsafe fn outer() { | --------------------- because it's nested under this `unsafe` fn 2 | fn inner() { 3 | unsafe { /* unnecessary */ } | ^^^^^^ unnecessary `unsafe` block | = note: #[warn(unused_unsafe)] on by default ``` **After:** ``` warning: unnecessary `unsafe` block --> src/main.rs:3:9 | 3 | unsafe { /* unnecessary */ } | ^^^^^^ unnecessary `unsafe` block | = note: #[warn(unused_unsafe)] on by default ```
2 parents b3f85fd + 42df8c5 commit a1a1409

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/librustc_mir/transform/check_unsafety.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,13 @@ fn is_enclosed(tcx: TyCtxt,
386386
if used_unsafe.contains(&parent_id) {
387387
Some(("block".to_string(), parent_id))
388388
} else if let Some(hir::map::NodeItem(&hir::Item {
389-
node: hir::ItemFn(_, hir::Unsafety::Unsafe, _, _, _, _),
389+
node: hir::ItemFn(_, fn_unsafety, _, _, _, _),
390390
..
391391
})) = tcx.hir.find(parent_id) {
392-
Some(("fn".to_string(), parent_id))
392+
match fn_unsafety {
393+
hir::Unsafety::Unsafe => Some(("fn".to_string(), parent_id)),
394+
hir::Unsafety::Normal => None,
395+
}
393396
} else {
394397
is_enclosed(tcx, used_unsafe, parent_id)
395398
}

src/test/compile-fail/issue-48131.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This note is annotated because the purpose of the test
12+
// is to ensure that certain other notes are not generated.
13+
#![deny(unused_unsafe)] //~ NOTE
14+
15+
// (test that no note is generated on this unsafe fn)
16+
pub unsafe fn a() {
17+
fn inner() {
18+
unsafe { /* unnecessary */ } //~ ERROR unnecessary `unsafe`
19+
//~^ NOTE
20+
}
21+
22+
inner()
23+
}
24+
25+
pub fn b() {
26+
// (test that no note is generated on this unsafe block)
27+
unsafe {
28+
fn inner() {
29+
unsafe { /* unnecessary */ } //~ ERROR unnecessary `unsafe`
30+
//~^ NOTE
31+
}
32+
33+
let () = ::std::mem::uninitialized();
34+
35+
inner()
36+
}
37+
}
38+
39+
fn main() {}

0 commit comments

Comments
 (0)