-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add MVP suggestion for unsafe_op_in_unsafe_fn
#112017
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
975152c
Add MVP suggestion for `unsafe_op_in_unsafe_fn`
LeSeulArtichaut aca61b2
Test that a couple more types of unsafe-ops get a wrapping unsafe blo…
Nemo157 62a712a
Hide suggestion to wrap function in unsafe block
Nemo157 8f3e876
Add note about unsafe functions body not being unsafe
Nemo157 802c1d5
Add test cases for suggestions with unsafe operations contained insid…
Nemo157 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub unsafe fn unsf() {} | ||
|
||
#[macro_export] | ||
macro_rules! unsafe_macro { () => ($crate::unsf()) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// run-rustfix | ||
// aux-build:external_unsafe_macro.rs | ||
|
||
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE | ||
|
||
extern crate external_unsafe_macro; | ||
|
||
unsafe fn unsf() {} | ||
|
||
pub unsafe fn foo() { unsafe { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
unsf(); //~ ERROR call to unsafe function is unsafe | ||
//~^ NOTE | ||
//~| NOTE | ||
unsf(); //~ ERROR call to unsafe function is unsafe | ||
//~^ NOTE | ||
//~| NOTE | ||
}} | ||
|
||
pub unsafe fn bar(x: *const i32) -> i32 { unsafe { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
}} | ||
|
||
static mut BAZ: i32 = 0; | ||
pub unsafe fn baz() -> i32 { unsafe { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
}} | ||
|
||
macro_rules! unsafe_macro { () => (unsf()) } | ||
//~^ ERROR call to unsafe function is unsafe | ||
//~| NOTE | ||
//~| NOTE | ||
//~| ERROR call to unsafe function is unsafe | ||
//~| NOTE | ||
//~| NOTE | ||
|
||
pub unsafe fn unsafe_in_macro() { unsafe { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
unsafe_macro!(); | ||
//~^ NOTE | ||
//~| NOTE | ||
unsafe_macro!(); | ||
//~^ NOTE | ||
//~| NOTE | ||
}} | ||
|
||
pub unsafe fn unsafe_in_external_macro() { | ||
// FIXME: https://github.com/rust-lang/rust/issues/112504 | ||
// FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
external_unsafe_macro::unsafe_macro!(); | ||
external_unsafe_macro::unsafe_macro!(); | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// run-rustfix | ||
// aux-build:external_unsafe_macro.rs | ||
|
||
#![deny(unsafe_op_in_unsafe_fn)] //~ NOTE | ||
|
||
extern crate external_unsafe_macro; | ||
|
||
unsafe fn unsf() {} | ||
|
||
pub unsafe fn foo() { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
unsf(); //~ ERROR call to unsafe function is unsafe | ||
//~^ NOTE | ||
//~| NOTE | ||
unsf(); //~ ERROR call to unsafe function is unsafe | ||
//~^ NOTE | ||
//~| NOTE | ||
} | ||
|
||
pub unsafe fn bar(x: *const i32) -> i32 { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
} | ||
|
||
static mut BAZ: i32 = 0; | ||
pub unsafe fn baz() -> i32 { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block | ||
//~^ NOTE | ||
//~| NOTE | ||
} | ||
|
||
macro_rules! unsafe_macro { () => (unsf()) } | ||
//~^ ERROR call to unsafe function is unsafe | ||
//~| NOTE | ||
//~| NOTE | ||
//~| ERROR call to unsafe function is unsafe | ||
//~| NOTE | ||
//~| NOTE | ||
|
||
pub unsafe fn unsafe_in_macro() { | ||
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
unsafe_macro!(); | ||
//~^ NOTE | ||
//~| NOTE | ||
unsafe_macro!(); | ||
//~^ NOTE | ||
//~| NOTE | ||
} | ||
|
||
pub unsafe fn unsafe_in_external_macro() { | ||
// FIXME: https://github.com/rust-lang/rust/issues/112504 | ||
// FIXME: ~^ NOTE an unsafe function restricts its caller, but its body is safe by default | ||
external_unsafe_macro::unsafe_macro!(); | ||
external_unsafe_macro::unsafe_macro!(); | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
error: call to unsafe function is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:12:5 | ||
| | ||
LL | unsf(); | ||
| ^^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
note: an unsafe function restricts its caller, but its body is safe by default | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:10:1 | ||
| | ||
LL | pub unsafe fn foo() { | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
note: the lint level is defined here | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:4:9 | ||
| | ||
LL | #![deny(unsafe_op_in_unsafe_fn)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: call to unsafe function is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:15:5 | ||
| | ||
LL | unsf(); | ||
| ^^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
|
||
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:22:13 | ||
| | ||
LL | let y = *x; | ||
| ^^ dereference of raw pointer | ||
| | ||
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior | ||
note: an unsafe function restricts its caller, but its body is safe by default | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:20:1 | ||
| | ||
LL | pub unsafe fn bar(x: *const i32) -> i32 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:25:9 | ||
| | ||
LL | y + *x | ||
| ^^ dereference of raw pointer | ||
| | ||
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior | ||
|
||
error: use of mutable static is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:33:13 | ||
| | ||
LL | let y = BAZ; | ||
| ^^^ use of mutable static | ||
| | ||
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior | ||
note: an unsafe function restricts its caller, but its body is safe by default | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:31:1 | ||
| | ||
LL | pub unsafe fn baz() -> i32 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: use of mutable static is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:36:9 | ||
| | ||
LL | y + BAZ | ||
| ^^^ use of mutable static | ||
| | ||
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior | ||
|
||
error: call to unsafe function is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36 | ||
| | ||
LL | macro_rules! unsafe_macro { () => (unsf()) } | ||
| ^^^^^^ call to unsafe function | ||
... | ||
LL | unsafe_macro!(); | ||
| --------------- in this macro invocation | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
note: an unsafe function restricts its caller, but its body is safe by default | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:49:1 | ||
| | ||
LL | pub unsafe fn unsafe_in_macro() { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: call to unsafe function is unsafe and requires unsafe block (error E0133) | ||
--> $DIR/wrapping-unsafe-block-sugg.rs:41:36 | ||
| | ||
LL | macro_rules! unsafe_macro { () => (unsf()) } | ||
| ^^^^^^ call to unsafe function | ||
... | ||
LL | unsafe_macro!(); | ||
| --------------- in this macro invocation | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
= note: this error originates in the macro `unsafe_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 8 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a doc comment describing what each of the three spans means?