Minimize the scope of unsafe blocks in gencode
Before this change we would generate nested unsafe blocks that led to compiler warnings. This change minimizes each unsafe block and thereby avoids the nesting and compiler warnings. It's generally a best practice to keep unsafe blocks minimal to avoid the situation where a currently safe function is marked unsafe at a later point and all existing callsides don't need to be updated.
PiperOrigin-RevId: 582993146
diff --git a/src/google/protobuf/compiler/rust/accessors/singular_string.cc b/src/google/protobuf/compiler/rust/accessors/singular_string.cc
index cff20e0..f3ae1e2 100644
--- a/src/google/protobuf/compiler/rust/accessors/singular_string.cc
+++ b/src/google/protobuf/compiler/rust/accessors/singular_string.cc
@@ -52,14 +52,12 @@
{"transform_view", transform_view}},
R"rs(
pub fn $field$_opt(&self) -> $pb$::Optional<&$proxied_type$> {
- unsafe {
- let view = $getter_thunk$(self.inner.msg).as_ref();
+ let view = unsafe { $getter_thunk$(self.inner.msg).as_ref() };
$pb$::Optional::new(
$transform_view$ ,
- $hazzer_thunk$(self.inner.msg)
+ unsafe { $hazzer_thunk$(self.inner.msg) }
)
}
- }
)rs");
}},
{"field_mutator_getter",