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

unused_unsafe gives wrong reason #48131

Closed
main-- opened this issue Feb 10, 2018 · 2 comments · Fixed by #48317
Closed

unused_unsafe gives wrong reason #48131

main-- opened this issue Feb 10, 2018 · 2 comments · Fixed by #48317
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@main--
Copy link
Contributor

main-- commented Feb 10, 2018

pub unsafe fn outer() {
    fn inner() {
        unsafe { /* unnecessary */ }
    }
    
    inner()
}
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

The unsafe block is clearly unnecessary, but it being inside a safe function which is nested in an unsafe function is clearly not the reason. Playpen

@sfackler sfackler added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Feb 10, 2018
@TimNN TimNN added the C-bug Category: This is a bug. label Feb 13, 2018
@ExpHP
Copy link
Contributor

ExpHP commented Feb 18, 2018

fix looks trivial, I think I got this

@ExpHP
Copy link
Contributor

ExpHP commented Feb 18, 2018

More specifically, I believe librustc_mir::transform::check_unsafety::is_enclosed should return None as soon as it encounters a safe fn.

Manishearth added a commit to Manishearth/rust that referenced this issue Feb 24, 2018
…ebank

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
```
Manishearth added a commit to Manishearth/rust that referenced this issue Feb 24, 2018
…ebank

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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants