Skip to content

Commit a8bb418

Browse files
committed
make unsafe_op_in_unsafe_fn MachineApplicable and add it to 2024 compatibility
1 parent bfcc027 commit a8bb418

14 files changed

+160
-51
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,11 @@ declare_lint! {
27552755
pub UNSAFE_OP_IN_UNSAFE_FN,
27562756
Allow,
27572757
"unsafe operations in unsafe functions without an explicit unsafe block are deprecated",
2758+
@future_incompatible = FutureIncompatibleInfo {
2759+
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
2760+
reference: "issue #71668 <https://github.com/rust-lang/rust/issues/71668>",
2761+
explain_reason: false
2762+
};
27582763
@edition Edition2024 => Warn;
27592764
}
27602765

compiler/rustc_mir_build/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl AddToDiagnostic for UnsafeNotInheritedLintNote {
430430
diag.tool_only_multipart_suggestion(
431431
fluent::mir_build_wrap_suggestion,
432432
vec![(body_start, "{ unsafe ".into()), (body_end, "}".into())],
433-
Applicability::MaybeIncorrect,
433+
Applicability::MachineApplicable,
434434
);
435435
}
436436
}

tests/ui/rfcs/rfc-2396-target_feature-11/safe-calls.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ error: call to function `sse2` with `#[target_feature]` is unsafe and requires u
9797
LL | sse2();
9898
| ^^^^^^ call to function with `#[target_feature]`
9999
|
100+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
100101
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
101102
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
102103
note: an unsafe function restricts its caller, but its body is safe by default

tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: call to unsafe function `unsf` is unsafe and requires unsafe block (err
44
LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
7+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
78
= note: consult the function's documentation for information on how to avoid undefined behavior
89
note: an unsafe function restricts its caller, but its body is safe by default
910
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:9:1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// edition: 2024
2+
// compile-flags: -Zunstable-options
3+
// check-pass
4+
5+
// Tests that `unsafe_op_in_unsafe_fn` is warn-by-default in edition 2024 and that the
6+
// `unused_unsafe` lint does not consider the inner unsafe block to be unused.
7+
#![crate_type = "lib"]
8+
#![deny(unused_unsafe)]
9+
10+
unsafe fn unsf() {}
11+
12+
unsafe fn foo() {
13+
unsf();
14+
//~^ WARN
15+
16+
// no unused_unsafe
17+
unsafe {
18+
unsf();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/edition_2024_default.rs:13:5
3+
|
4+
LL | unsf();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
8+
= note: consult the function's documentation for information on how to avoid undefined behavior
9+
note: an unsafe function restricts its caller, but its body is safe by default
10+
--> $DIR/edition_2024_default.rs:12:1
11+
|
12+
LL | unsafe fn foo() {
13+
| ^^^^^^^^^^^^^^^
14+
= note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default
15+
16+
warning: 1 warning emitted
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![deny(rust_2024_compatibility)]
2+
#![crate_type = "lib"]
3+
4+
unsafe fn unsf() {}
5+
6+
unsafe fn foo() {
7+
unsf();
8+
//~^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
2+
--> $DIR/in_2024_compatibility.rs:7:5
3+
|
4+
LL | unsf();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
8+
= note: consult the function's documentation for information on how to avoid undefined behavior
9+
note: an unsafe function restricts its caller, but its body is safe by default
10+
--> $DIR/in_2024_compatibility.rs:6:1
11+
|
12+
LL | unsafe fn foo() {
13+
| ^^^^^^^^^^^^^^^
14+
note: the lint level is defined here
15+
--> $DIR/in_2024_compatibility.rs:1:9
16+
|
17+
LL | #![deny(rust_2024_compatibility)]
18+
| ^^^^^^^^^^^^^^^^^^^^^^^
19+
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(rust_2024_compatibility)]`
20+
21+
error: aborting due to 1 previous error
22+

tests/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.stderr tests/ui/unsafe/unsafe_op_in_unsafe_fn/rfc-2585-unsafe_op_in_unsafe_fn.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: call to unsafe function `unsf` is unsafe and requires unsafe block (error
44
LL | unsf();
55
| ^^^^^^ call to unsafe function
66
|
7+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
78
= note: consult the function's documentation for information on how to avoid undefined behavior
89
note: an unsafe function restricts its caller, but its body is safe by default
910
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:8:1
@@ -22,6 +23,7 @@ error: dereference of raw pointer is unsafe and requires unsafe block (error E01
2223
LL | *PTR;
2324
| ^^^^ dereference of raw pointer
2425
|
26+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
2527
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
2628

2729
error: use of mutable static is unsafe and requires unsafe block (error E0133)
@@ -30,6 +32,7 @@ error: use of mutable static is unsafe and requires unsafe block (error E0133)
3032
LL | VOID = ();
3133
| ^^^^ use of mutable static
3234
|
35+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
3336
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
3437

3538
error: unnecessary `unsafe` block
@@ -50,6 +53,7 @@ error: call to unsafe function `unsf` is unsafe and requires unsafe block (error
5053
LL | unsf();
5154
| ^^^^^^ call to unsafe function
5255
|
56+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
5357
= note: consult the function's documentation for information on how to avoid undefined behavior
5458
note: an unsafe function restricts its caller, but its body is safe by default
5559
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:23:1
@@ -69,6 +73,7 @@ error: dereference of raw pointer is unsafe and requires unsafe block (error E01
6973
LL | *PTR;
7074
| ^^^^ dereference of raw pointer
7175
|
76+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
7277
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
7378

7479
error: use of mutable static is unsafe and requires unsafe block (error E0133)
@@ -77,6 +82,7 @@ error: use of mutable static is unsafe and requires unsafe block (error E0133)
7782
LL | VOID = ();
7883
| ^^^^ use of mutable static
7984
|
85+
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
8086
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
8187

8288
error: unnecessary `unsafe` block

tests/ui/unsafe/wrapping-unsafe-block-sugg.fixed tests/ui/unsafe/unsafe_op_in_unsafe_fn/wrapping-unsafe-block-sugg.fixed

+30-20
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,60 @@ unsafe fn unsf() {}
1111
pub unsafe fn foo() { unsafe {
1212
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
1313
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
14-
//~^ NOTE
15-
//~| NOTE
14+
//~^ NOTE call to unsafe function
15+
//~| NOTE for more information, see issue #71668
16+
//~| NOTE consult the function's documentation
1617
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
17-
//~^ NOTE
18-
//~| NOTE
18+
//~^ NOTE call to unsafe function
19+
//~| NOTE for more information, see issue #71668
20+
//~| NOTE consult the function's documentation
1921
}}
2022

2123
pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
2224
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
2325
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
24-
//~^ NOTE
25-
//~| NOTE
26+
//~^ NOTE dereference of raw pointer
27+
//~| NOTE for more information, see issue #71668
28+
//~| NOTE raw pointers may be null
2629
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
27-
//~^ NOTE
28-
//~| NOTE
30+
//~^ NOTE dereference of raw pointer
31+
//~| NOTE for more information, see issue #71668
32+
//~| NOTE raw pointers may be null
2933
}}
3034

3135
static mut BAZ: i32 = 0;
3236
pub unsafe fn baz() -> i32 { unsafe {
3337
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
3438
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
35-
//~^ NOTE
36-
//~| NOTE
39+
//~^ NOTE use of mutable static
40+
//~| NOTE for more information, see issue #71668
41+
//~| NOTE mutable statics can be mutated by multiple threads
3742
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
38-
//~^ NOTE
39-
//~| NOTE
43+
//~^ NOTE use of mutable static
44+
//~| NOTE for more information, see issue #71668
45+
//~| NOTE mutable statics can be mutated by multiple threads
4046
}}
4147

4248
macro_rules! unsafe_macro { () => (unsf()) }
4349
//~^ ERROR call to unsafe function `unsf` is unsafe
44-
//~| NOTE
45-
//~| NOTE
50+
//~| NOTE call to unsafe function
51+
//~| NOTE for more information, see issue #71668
52+
//~| NOTE consult the function's documentation
4653
//~| ERROR call to unsafe function `unsf` is unsafe
47-
//~| NOTE
48-
//~| NOTE
54+
//~| NOTE call to unsafe function
55+
//~| NOTE for more information, see issue #71668
56+
//~| NOTE consult the function's documentation
4957

5058
pub unsafe fn unsafe_in_macro() { unsafe {
5159
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
5260
unsafe_macro!();
53-
//~^ NOTE
54-
//~| NOTE
61+
//~^ NOTE in this expansion
62+
//~| NOTE in this expansion
63+
//~| NOTE in this expansion
5564
unsafe_macro!();
56-
//~^ NOTE
57-
//~| NOTE
65+
//~^ NOTE in this expansion
66+
//~| NOTE in this expansion
67+
//~| NOTE in this expansion
5868
}}
5969

6070
pub unsafe fn unsafe_in_external_macro() {

tests/ui/unsafe/wrapping-unsafe-block-sugg.rs tests/ui/unsafe/unsafe_op_in_unsafe_fn/wrapping-unsafe-block-sugg.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,60 @@ unsafe fn unsf() {}
1111
pub unsafe fn foo() {
1212
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
1313
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
14-
//~^ NOTE
15-
//~| NOTE
14+
//~^ NOTE call to unsafe function
15+
//~| NOTE for more information, see issue #71668
16+
//~| NOTE consult the function's documentation
1617
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
17-
//~^ NOTE
18-
//~| NOTE
18+
//~^ NOTE call to unsafe function
19+
//~| NOTE for more information, see issue #71668
20+
//~| NOTE consult the function's documentation
1921
}
2022

2123
pub unsafe fn bar(x: *const i32) -> i32 {
2224
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
2325
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
24-
//~^ NOTE
25-
//~| NOTE
26+
//~^ NOTE dereference of raw pointer
27+
//~| NOTE for more information, see issue #71668
28+
//~| NOTE raw pointers may be null
2629
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
27-
//~^ NOTE
28-
//~| NOTE
30+
//~^ NOTE dereference of raw pointer
31+
//~| NOTE for more information, see issue #71668
32+
//~| NOTE raw pointers may be null
2933
}
3034

3135
static mut BAZ: i32 = 0;
3236
pub unsafe fn baz() -> i32 {
3337
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
3438
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
35-
//~^ NOTE
36-
//~| NOTE
39+
//~^ NOTE use of mutable static
40+
//~| NOTE for more information, see issue #71668
41+
//~| NOTE mutable statics can be mutated by multiple threads
3742
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
38-
//~^ NOTE
39-
//~| NOTE
43+
//~^ NOTE use of mutable static
44+
//~| NOTE for more information, see issue #71668
45+
//~| NOTE mutable statics can be mutated by multiple threads
4046
}
4147

4248
macro_rules! unsafe_macro { () => (unsf()) }
4349
//~^ ERROR call to unsafe function `unsf` is unsafe
44-
//~| NOTE
45-
//~| NOTE
50+
//~| NOTE call to unsafe function
51+
//~| NOTE for more information, see issue #71668
52+
//~| NOTE consult the function's documentation
4653
//~| ERROR call to unsafe function `unsf` is unsafe
47-
//~| NOTE
48-
//~| NOTE
54+
//~| NOTE call to unsafe function
55+
//~| NOTE for more information, see issue #71668
56+
//~| NOTE consult the function's documentation
4957

5058
pub unsafe fn unsafe_in_macro() {
5159
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
5260
unsafe_macro!();
53-
//~^ NOTE
54-
//~| NOTE
61+
//~^ NOTE in this expansion
62+
//~| NOTE in this expansion
63+
//~| NOTE in this expansion
5564
unsafe_macro!();
56-
//~^ NOTE
57-
//~| NOTE
65+
//~^ NOTE in this expansion
66+
//~| NOTE in this expansion
67+
//~| NOTE in this expansion
5868
}
5969

6070
pub unsafe fn unsafe_in_external_macro() {

0 commit comments

Comments
 (0)