From 119e0fff8a78ed149cc855ae409de1e9d5997a9f Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 27 May 2023 13:00:51 +0200 Subject: [PATCH] Change unsafe_op_in_unsafe_fn to be warn-by-default from edition 2024 --- compiler/rustc_lint_defs/src/builtin.rs | 5 +++-- compiler/rustc_lint_defs/src/lib.rs | 20 ++++--------------- .../edition-2024-unsafe_op_in_unsafe_fn.rs | 17 ++++++++++++++++ ...edition-2024-unsafe_op_in_unsafe_fn.stderr | 16 +++++++++++++++ 4 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs create mode 100644 tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 7e745de4f1a..d4a90f82224 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -2554,8 +2554,8 @@ declare_lint! { /// /// The fix to this is to wrap the unsafe code in an `unsafe` block. /// - /// This lint is "allow" by default since this will affect a large amount - /// of existing code, and the exact plan for increasing the severity is + /// This lint is "allow" by default on editions up to 2021, from 2024 it is + /// "warn" by default; the plan for increasing severity further is /// still being considered. See [RFC #2585] and [issue #71668] for more /// details. /// @@ -2567,6 +2567,7 @@ declare_lint! { pub UNSAFE_OP_IN_UNSAFE_FN, Allow, "unsafe operations in unsafe functions without an explicit unsafe block are deprecated", + @edition Edition2024 => Warn; } declare_lint! { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 76e736859d3..ef2f7940477 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -719,36 +719,24 @@ macro_rules! declare_lint { ($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr, $(@feature_gate = $gate:expr;)? $(@future_incompatible = FutureIncompatibleInfo { $($field:ident : $val:expr),* $(,)* }; )? + $(@edition $lint_edition:ident => $edition_level:ident;)? $($v:ident),*) => ( $(#[$attr])* $vis static $NAME: &$crate::Lint = &$crate::Lint { name: stringify!($NAME), default_level: $crate::$Level, desc: $desc, - edition_lint_opts: None, is_plugin: false, $($v: true,)* - $(feature_gate: Some($gate),)* + $(feature_gate: Some($gate),)? $(future_incompatible: Some($crate::FutureIncompatibleInfo { $($field: $val,)* ..$crate::FutureIncompatibleInfo::default_fields_for_macro() - }),)* + }),)? + $(edition_lint_opts: Some(($crate::Edition::$lint_edition, $crate::$edition_level)),)? ..$crate::Lint::default_fields_for_macro() }; ); - ($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr, - $lint_edition: expr => $edition_level: ident - ) => ( - $(#[$attr])* - $vis static $NAME: &$crate::Lint = &$crate::Lint { - name: stringify!($NAME), - default_level: $crate::$Level, - desc: $desc, - edition_lint_opts: Some(($lint_edition, $crate::Level::$edition_level)), - report_in_external_macro: false, - is_plugin: false, - }; - ); } #[macro_export] diff --git a/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs new file mode 100644 index 00000000000..a192f3445f7 --- /dev/null +++ b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.rs @@ -0,0 +1,17 @@ +// edition: 2024 +// compile-flags: -Zunstable-options +// check-pass + +#![crate_type = "lib"] + +#![deny(unused_unsafe)] + +unsafe fn unsf() {} + +unsafe fn foo() { + unsf(); + //~^ WARN call to unsafe function is unsafe and requires unsafe block + + // no unused_unsafe + unsafe { unsf(); } +} diff --git a/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr new file mode 100644 index 00000000000..fbc621f4d0e --- /dev/null +++ b/tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr @@ -0,0 +1,16 @@ +warning: call to unsafe function is unsafe and requires unsafe block (error E0133) + --> $DIR/edition-2024-unsafe_op_in_unsafe_fn.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/edition-2024-unsafe_op_in_unsafe_fn.rs:11:1 + | +LL | unsafe fn foo() { + | ^^^^^^^^^^^^^^^ + = note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default + +warning: 1 warning emitted +