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

Explicit warning on nested unsafe blocks #39144

Closed
Wilfred opened this issue Jan 17, 2017 · 3 comments
Closed

Explicit warning on nested unsafe blocks #39144

Wilfred opened this issue Jan 17, 2017 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Jan 17, 2017

When refactoring code, it's easy to accidentally end up with nested unsafe blocks. This can lead to confusing rustc errors.

unsafe fn very_dangerous() -> u64 {
    1
}

fn main() {
    unsafe {
        // lots of code...
        
        unsafe {
            very_dangerous();
        }
    }

The compiler says:

rustc 1.16.0-nightly (ff591b6dc 2017-01-15)
warning: unnecessary `unsafe` block, #[warn(unused_unsafe)] on by default
  --> <anon>:9:9
   |
9  |           unsafe {
   |  _________^ starting here...
10 | |             very_dangerous();
11 | |         }
   | |_________^ ...ending here

This is surprising at first glance, since very_dangerous requires an unsafe block but the message implies otherwise. Could we warn about nested unsafe blocks?

@brson
Copy link
Contributor

brson commented Jan 18, 2017

A general improvement that would accomplish the same goal might be to have a 'note' pointing to the outer unsafe declaration. The text can be specialized to say e.g. "because of this unsafe block" or "unsafe function".

@brson brson added A-diagnostics Area: Messages for errors, warnings, and lints E-help-wanted Call for participation: Help is requested to fix this issue. I-papercut labels Jan 18, 2017
@brson
Copy link
Contributor

brson commented Jan 18, 2017

Enrich this error with span information about the reason the unsafe block is unneeded. Should be relatively simple to follow existing examples where errors have 'notes' attached. The tricky bits will be making sure it looks good and reads well, and finding the span of the original unsafe block.

@estebank
Copy link
Contributor

@brson, I have PR #39202 for this, and I arrived to these two possibilities:

frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 6, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
bors added a commit that referenced this issue Mar 7, 2017
Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes #39144.
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 9, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 9, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
alexcrichton added a commit to alexcrichton/rust that referenced this issue Mar 10, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
alexcrichton added a commit to alexcrichton/rust that referenced this issue Mar 10, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
arielb1 pushed a commit to arielb1/rust that referenced this issue Mar 10, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
arielb1 pushed a commit to arielb1/rust that referenced this issue Mar 10, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
arielb1 pushed a commit to arielb1/rust that referenced this issue Mar 10, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
alexcrichton added a commit to alexcrichton/rust that referenced this issue Mar 10, 2017
…turner

Point to enclosing block/fn on nested unsafe

When declaring nested unsafe blocks (`unsafe {unsafe {}}`) that trigger
the "unnecessary `unsafe` block" error, point out the enclosing `unsafe
block` or `unsafe fn` that makes it unnecessary.

<img width="621" alt="" src="https://cloud.githubusercontent.com/assets/1606434/22139922/26ad468a-de9e-11e6-8884-2945be882ea8.png">

Fixes rust-lang#39144.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants