diff --git a/src/rust-2024/cargo-remove-implicit-features.md b/src/rust-2024/cargo-remove-implicit-features.md index 385ca97..f689835 100644 --- a/src/rust-2024/cargo-remove-implicit-features.md +++ b/src/rust-2024/cargo-remove-implicit-features.md @@ -2,6 +2,9 @@ 🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". +This feature has not yet been implemented. +More information may be found in the tracking issue at . + ## Summary ## Details diff --git a/src/rust-2024/public-private-dependencies.md b/src/rust-2024/public-private-dependencies.md index b03cf70..2267c4b 100644 --- a/src/rust-2024/public-private-dependencies.md +++ b/src/rust-2024/public-private-dependencies.md @@ -2,6 +2,9 @@ 🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". +This feature is only partially implemented, and not yet ready for testing. +More information may be found in the tracking issue at . + ## Summary ## Details diff --git a/src/rust-2024/rpit-lifetime-capture.md b/src/rust-2024/rpit-lifetime-capture.md index 737f325..c4c5165 100644 --- a/src/rust-2024/rpit-lifetime-capture.md +++ b/src/rust-2024/rpit-lifetime-capture.md @@ -2,6 +2,9 @@ 🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". +This feature is partially implemented, and not yet ready for testing. +More information may be found in the tracking issue at . + ## Summary ## Details diff --git a/src/rust-2024/rustfmt-overflow-delimited-expr.md b/src/rust-2024/rustfmt-overflow-delimited-expr.md index f295a2c..72ee290 100644 --- a/src/rust-2024/rustfmt-overflow-delimited-expr.md +++ b/src/rust-2024/rustfmt-overflow-delimited-expr.md @@ -2,6 +2,9 @@ 🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". +This feature is not yet implemented. +More information may be found in . + ## Summary ## Details diff --git a/src/rust-2024/static-mut-reference.md b/src/rust-2024/static-mut-reference.md index e5c1043..539016c 100644 --- a/src/rust-2024/static-mut-reference.md +++ b/src/rust-2024/static-mut-reference.md @@ -4,6 +4,37 @@ ## Summary +- The [`static_mut_ref`] lint is now a hard error that cannot be disabled. + This prevents taking a shared or mutable reference to a `static mut`. + +[`static_mut_ref`]: ../../rustc/lints/listing/warn-by-default.html#static-mut-ref + ## Details +Taking a reference to a [`static mut`] is no longer allowed: + + +```rust +static mut X: i32 = 23; +static mut Y: i32 = 24; + +unsafe { + let y = &X; // ERROR: reference of mutable static + let ref x = X; // ERROR: reference of mutable static + let (x, y) = (&X, &Y); // ERROR: reference of mutable static +} +``` + +Shared or mutable references of mutable static are almost always a mistake and can lead to undefined behavior and various other problems in your code. +For example, another thread writing to the `static mut` will cause an aliasing violation and incur [Undefined Behavior]. + + + +[Undefined Behavior]: ../../reference/behavior-considered-undefined.html +[`static mut`]: ../../reference/items/static-items.html#mutable-statics + ## Migration + +🚧 The automatic migration for this has not yet been implemented. + + diff --git a/src/rust-2024/unsafe-op-in-unsafe-fn.md b/src/rust-2024/unsafe-op-in-unsafe-fn.md index e95adfa..0093f14 100644 --- a/src/rust-2024/unsafe-op-in-unsafe-fn.md +++ b/src/rust-2024/unsafe-op-in-unsafe-fn.md @@ -4,6 +4,55 @@ ## Summary +- The [`unsafe_op_in_unsafe_fn`] lint now warns by default. + This warning detects calls to unsafe operations in unsafe functions without an explicit unsafe block. + +[`unsafe_op_in_unsafe_fn`]: ../../rustc/lints/listing/allowed-by-default.html#unsafe-op-in-unsafe-fn + ## Details +The [`unsafe_op_in_unsafe_fn`] lint will fire if there are [unsafe operations] in an unsafe function without an explicit [`unsafe {}` block][unsafe-block]. + +```rust +# #![warn(unsafe_op_in_unsafe_fn)] +unsafe fn get_unchecked(x: &[T], i: usize) -> &T { + x.get_unchecked(i) // WARNING: requires unsafe block +} +``` + +The solution is to wrap any unsafe operations in an `unsafe` block: + +```rust +# #![deny(unsafe_op_in_unsafe_fn)] +unsafe fn get_unchecked(x: &[T], i: usize) -> &T { + unsafe { x.get_unchecked(i) } +} +``` + +This change is intended to help protect against accidental use of unsafe operations in an unsafe function. +The `unsafe` function keyword was performing two roles. +One was to declare that *calling* the function requires unsafe, and that the caller is responsible to uphold additional safety requirements. +The other role was to allow the use of unsafe operations inside of the function. +This second role was determined to be too risky without explicit `unsafe` blocks. + +More information and motivation may be found in [RFC #2585]. + +[unsafe operations]: ../../reference/unsafety.html +[unsafe-block]: ../../reference/expressions/block-expr.html#unsafe-blocks +[RFC #2585]: https://rust-lang.github.io/rfcs/2585-unsafe-block-in-unsafe-fn.html + ## Migration + +The [`unsafe_op_in_unsafe_fn`] lint is part of the `rust-2024-compatibility` lint group. +In order to migrate your code to be Rust 2024 Edition compatible, run: + +```sh +cargo fix --edition +``` + +Alternatively, you can manually enable the lint to find places where unsafe blocks need to be added, or switch it to `allow` to silence the lint completely. + +```rust +// Add this to the root of your crate to do a manual migration. +#![warn(unsafe_op_in_unsafe_fn)] +```