Skip to content

Commit 8f82878

Browse files
committed
Add FIXME comment to unsafe env modification
Addresses rust-lang#124636 (comment). I think that the diff display regresses a little, because it's no longer showing the `+` to show where the `unsafe {}` is added. I think it's still fine.
1 parent 2d28b63 commit 8f82878

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9797
if !span.at_least_rust_2024()
9898
&& self.tcx.has_attr(id, sym::rustc_deprecated_safe_2024) =>
9999
{
100+
let sm = self.tcx.sess.source_map();
100101
self.tcx.emit_node_span_lint(
101102
DEPRECATED_SAFE,
102103
self.hir_context,
@@ -105,6 +106,8 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
105106
span,
106107
function: with_no_trimmed_paths!(self.tcx.def_path_str(id)),
107108
sub: CallToDeprecatedSafeFnRequiresUnsafeSub {
109+
indent: sm.indentation_before(span).unwrap_or_default(),
110+
start_of_line: sm.span_extend_to_line(span).shrink_to_lo(),
108111
left: span.shrink_to_lo(),
109112
right: span.shrink_to_hi(),
110113
},

compiler/rustc_mir_build/src/errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafe {
3333
#[derive(Subdiagnostic)]
3434
#[multipart_suggestion(mir_build_suggestion, applicability = "machine-applicable")]
3535
pub(crate) struct CallToDeprecatedSafeFnRequiresUnsafeSub {
36+
pub(crate) indent: String,
37+
#[suggestion_part(
38+
code = "{indent}// FIXME: Audit that the environment access only happens in single-threaded code.\n"
39+
)]
40+
pub(crate) start_of_line: Span,
3641
#[suggestion_part(code = "unsafe {{ ")]
3742
pub(crate) left: Span,
3843
#[suggestion_part(code = " }}")]

tests/ui/rust-2024/unsafe-env-suggestion.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use std::env;
66

77
#[deny(unused_unsafe)]
88
fn main() {
9+
// FIXME: Audit that the environment access only happens in single-threaded code.
910
unsafe { env::set_var("FOO", "BAR") };
1011
//~^ ERROR call to deprecated safe function
1112
//~| WARN this is accepted in the current edition
13+
// FIXME: Audit that the environment access only happens in single-threaded code.
1214
unsafe { env::remove_var("FOO") };
1315
//~^ ERROR call to deprecated safe function
1416
//~| WARN this is accepted in the current edition

tests/ui/rust-2024/unsafe-env-suggestion.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ LL | #![deny(deprecated_safe)]
1313
| ^^^^^^^^^^^^^^^
1414
help: you can wrap the call in an `unsafe` block if you can guarantee the code is only ever called from single-threaded code
1515
|
16-
LL | unsafe { env::set_var("FOO", "BAR") };
17-
| ++++++++ +
16+
LL + // FIXME: Audit that the environment access only happens in single-threaded code.
17+
LL ~ unsafe { env::set_var("FOO", "BAR") };
18+
|
1819

1920
error: call to deprecated safe function `std::env::remove_var` is unsafe and requires unsafe block
2021
--> $DIR/unsafe-env-suggestion.rs:12:5
@@ -26,8 +27,9 @@ LL | env::remove_var("FOO");
2627
= note: for more information, see issue #27970 <https://github.com/rust-lang/rust/issues/27970>
2728
help: you can wrap the call in an `unsafe` block if you can guarantee the code is only ever called from single-threaded code
2829
|
29-
LL | unsafe { env::remove_var("FOO") };
30-
| ++++++++ +
30+
LL + // FIXME: Audit that the environment access only happens in single-threaded code.
31+
LL ~ unsafe { env::remove_var("FOO") };
32+
|
3133

3234
error: aborting due to 2 previous errors
3335

0 commit comments

Comments
 (0)