1
Fork 0

Add MVP suggestion for unsafe_op_in_unsafe_fn

Nemo157 rebase notes: Migrated the changes to the lint into fluent
This commit is contained in:
Léo Lanteri Thauvin 2022-07-28 00:03:16 +02:00 committed by Wim Looman
parent a525c7ddba
commit 975152ce30
No known key found for this signature in database
GPG key ID: C6F5748C6DD1607B
7 changed files with 105 additions and 11 deletions

View file

@ -55,6 +55,7 @@ mir_transform_unaligned_packed_ref = reference to packed field is unaligned
mir_transform_union_access_label = access to union field mir_transform_union_access_label = access to union field
mir_transform_union_access_note = the field may not be properly initialized: using uninitialized data will cause undefined behavior mir_transform_union_access_note = the field may not be properly initialized: using uninitialized data will cause undefined behavior
mir_transform_unsafe_op_in_unsafe_fn = {$details} is unsafe and requires unsafe block (error E0133) mir_transform_unsafe_op_in_unsafe_fn = {$details} is unsafe and requires unsafe block (error E0133)
.suggestion = consider wrapping the function body in an unsafe block
mir_transform_unused_unsafe = unnecessary `unsafe` block mir_transform_unused_unsafe = unnecessary `unsafe` block
.label = because it's nested under this `unsafe` block .label = because it's nested under this `unsafe` block

View file

@ -527,6 +527,8 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
} }
let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id); let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id);
// Only suggest wrapping the entire function body in an unsafe block once
let mut suggest_unsafe_block = true;
for &UnsafetyViolation { source_info, lint_root, kind, details } in violations.iter() { for &UnsafetyViolation { source_info, lint_root, kind, details } in violations.iter() {
let details = errors::RequiresUnsafeDetail { violation: details, span: source_info.span }; let details = errors::RequiresUnsafeDetail { violation: details, span: source_info.span };
@ -561,12 +563,24 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
op_in_unsafe_fn_allowed, op_in_unsafe_fn_allowed,
}); });
} }
UnsafetyViolationKind::UnsafeFn => tcx.emit_spanned_lint( UnsafetyViolationKind::UnsafeFn => {
UNSAFE_OP_IN_UNSAFE_FN, tcx.emit_spanned_lint(
lint_root, UNSAFE_OP_IN_UNSAFE_FN,
source_info.span, lint_root,
errors::UnsafeOpInUnsafeFn { details }, source_info.span,
), errors::UnsafeOpInUnsafeFn {
details,
suggest_unsafe_block: suggest_unsafe_block.then(|| {
let body = tcx.hir().body_owned_by(def_id);
let body_span = tcx.hir().body(body).value.span;
let start = tcx.sess.source_map().start_point(body_span).shrink_to_hi();
let end = tcx.sess.source_map().end_point(body_span).shrink_to_lo();
(start, end)
}),
},
);
suggest_unsafe_block = false;
}
} }
} }

View file

@ -1,5 +1,6 @@
use rustc_errors::{ use rustc_errors::{
DecorateLint, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler, IntoDiagnostic, Applicability, DecorateLint, DiagnosticBuilder, DiagnosticMessage, EmissionGuarantee, Handler,
IntoDiagnostic,
}; };
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
use rustc_middle::mir::{AssertKind, UnsafetyViolationDetails}; use rustc_middle::mir::{AssertKind, UnsafetyViolationDetails};
@ -130,6 +131,7 @@ impl RequiresUnsafeDetail {
pub(crate) struct UnsafeOpInUnsafeFn { pub(crate) struct UnsafeOpInUnsafeFn {
pub details: RequiresUnsafeDetail, pub details: RequiresUnsafeDetail,
pub suggest_unsafe_block: Option<(Span, Span)>,
} }
impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn { impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
@ -138,13 +140,20 @@ impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
self, self,
diag: &'b mut DiagnosticBuilder<'a, ()>, diag: &'b mut DiagnosticBuilder<'a, ()>,
) -> &'b mut DiagnosticBuilder<'a, ()> { ) -> &'b mut DiagnosticBuilder<'a, ()> {
let desc = diag let handler = diag.handler().expect("lint should not yet be emitted");
.handler() let desc = handler.eagerly_translate_to_string(self.details.label(), [].into_iter());
.expect("lint should not yet be emitted")
.eagerly_translate_to_string(self.details.label(), [].into_iter());
diag.set_arg("details", desc); diag.set_arg("details", desc);
diag.span_label(self.details.span, self.details.label()); diag.span_label(self.details.span, self.details.label());
diag.note(self.details.note()); diag.note(self.details.note());
if let Some((start, end)) = self.suggest_unsafe_block {
diag.multipart_suggestion_verbose(
crate::fluent_generated::mir_transform_suggestion,
vec![(start, " unsafe {".into()), (end, "}".into())],
Applicability::MaybeIncorrect,
);
}
diag diag
} }

View file

@ -10,6 +10,14 @@ note: the lint level is defined here
| |
LL | #![deny(unsafe_op_in_unsafe_fn)] LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
help: consider wrapping the function body in an unsafe block
|
LL ~ unsafe fn deny_level() { unsafe {
LL | unsf();
...
LL |
LL ~ }}
|
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:15:5 --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:15:5
@ -52,6 +60,14 @@ note: the lint level is defined here
LL | #[deny(warnings)] LL | #[deny(warnings)]
| ^^^^^^^^ | ^^^^^^^^
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]` = note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(warnings)]`
help: consider wrapping the function body in an unsafe block
|
LL ~ unsafe fn warning_level() { unsafe {
LL | unsf();
...
LL |
LL ~ }}
|
error: dereference of raw pointer is unsafe and requires unsafe block (error E0133) error: dereference of raw pointer is unsafe and requires unsafe block (error E0133)
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5 --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:31:5

View file

@ -0,0 +1,12 @@
// run-rustfix
#![deny(unsafe_op_in_unsafe_fn)]
unsafe fn unsf() {}
pub unsafe fn foo() { unsafe {
unsf(); //~ ERROR call to unsafe function is unsafe
unsf(); //~ ERROR call to unsafe function is unsafe
}}
fn main() {}

View file

@ -0,0 +1,12 @@
// run-rustfix
#![deny(unsafe_op_in_unsafe_fn)]
unsafe fn unsf() {}
pub unsafe fn foo() {
unsf(); //~ ERROR call to unsafe function is unsafe
unsf(); //~ ERROR call to unsafe function is unsafe
}
fn main() {}

View file

@ -0,0 +1,30 @@
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:8:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
note: the lint level is defined here
--> $DIR/wrapping-unsafe-block-sugg.rs:3:9
|
LL | #![deny(unsafe_op_in_unsafe_fn)]
| ^^^^^^^^^^^^^^^^^^^^^^
help: consider wrapping the function body in an unsafe block
|
LL ~ pub unsafe fn foo() { unsafe {
LL | unsf();
LL | unsf();
LL ~ }}
|
error: call to unsafe function is unsafe and requires unsafe block (error E0133)
--> $DIR/wrapping-unsafe-block-sugg.rs:9:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior
error: aborting due to 2 previous errors