From b392eb4eb522e9f2dfe5b307780fae5f039f6c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Thu, 25 Jul 2024 08:27:15 +0300 Subject: [PATCH] Fix and enable unsafe_op_in_unsafe_fn --- .github/workflows/ci.yaml | 2 +- Cargo.toml | 6 +++--- crates/salsa/src/intern_id.rs | 3 ++- crates/stdx/src/anymap.rs | 4 ++-- crates/stdx/src/process.rs | 18 ++++++++---------- lib/line-index/src/lib.rs | 26 +++++++++++++------------- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 87a1729d2b41..6d3e488bb082 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -15,7 +15,7 @@ env: CARGO_NET_RETRY: 10 CI: 1 RUST_BACKTRACE: short - RUSTFLAGS: "-D warnings -W unreachable-pub -W bare-trait-objects" + RUSTFLAGS: "-D warnings -D elided_lifetimes_in_paths -D explicit_outlives_requirements -D unsafe_op_in_unsafe_fn -D unused_extern_crates -D unused_lifetimes -D unreachable_pub" RUSTUP_MAX_RETRIES: 10 jobs: diff --git a/Cargo.toml b/Cargo.toml index 428d11ad60d4..c2f601a91bc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -163,14 +163,14 @@ xshell = "0.2.5" dashmap = { version = "=5.5.3", features = ["raw-api"] } [workspace.lints.rust] -bare_trait_objects = "warn" +# remember to update RUSTFLAGS in ci.yml if you add something here + elided_lifetimes_in_paths = "warn" -ellipsis_inclusive_range_patterns = "warn" explicit_outlives_requirements = "warn" +unsafe_op_in_unsafe_fn = "warn" unused_extern_crates = "warn" unused_lifetimes = "warn" unreachable_pub = "warn" -semicolon_in_expressions_from_macros = "warn" [workspace.lints.clippy] # FIXME Remove the tidy test once the lint table is stable diff --git a/crates/salsa/src/intern_id.rs b/crates/salsa/src/intern_id.rs index b060d8aab688..8e74c100acab 100644 --- a/crates/salsa/src/intern_id.rs +++ b/crates/salsa/src/intern_id.rs @@ -63,7 +63,8 @@ impl InternId { /// `value` must be less than `MAX` pub const unsafe fn new_unchecked(value: u32) -> Self { debug_assert!(value < InternId::MAX); - InternId { value: NonZeroU32::new_unchecked(value + 1) } + let value = unsafe { NonZeroU32::new_unchecked(value + 1) }; + InternId { value } } /// Convert this raw-id into a u32 value. diff --git a/crates/stdx/src/anymap.rs b/crates/stdx/src/anymap.rs index 4eafcfb060f2..91fab8e92380 100644 --- a/crates/stdx/src/anymap.rs +++ b/crates/stdx/src/anymap.rs @@ -271,12 +271,12 @@ macro_rules! implement { #[inline] unsafe fn downcast_ref_unchecked(&self) -> &T { - &*(self as *const Self as *const T) + unsafe { &*(self as *const Self as *const T) } } #[inline] unsafe fn downcast_mut_unchecked(&mut self) -> &mut T { - &mut *(self as *mut Self as *mut T) + unsafe { &mut *(self as *mut Self as *mut T) } } } diff --git a/crates/stdx/src/process.rs b/crates/stdx/src/process.rs index c54d850d7b56..75ae064db9a4 100644 --- a/crates/stdx/src/process.rs +++ b/crates/stdx/src/process.rs @@ -212,17 +212,13 @@ mod imp { impl<'a> Pipe<'a> { unsafe fn new(p: P, dst: &'a mut Vec) -> Pipe<'a> { - Pipe { - dst, - pipe: NamedPipe::from_raw_handle(p.into_raw_handle()), - overlapped: Overlapped::zero(), - done: false, - } + let pipe = unsafe { NamedPipe::from_raw_handle(p.into_raw_handle()) }; + Pipe { dst, pipe, overlapped: Overlapped::zero(), done: false } } unsafe fn read(&mut self) -> io::Result<()> { - let dst = slice_to_end(self.dst); - match self.pipe.read_overlapped(dst, self.overlapped.raw()) { + let dst = unsafe { slice_to_end(self.dst) }; + match unsafe { self.pipe.read_overlapped(dst, self.overlapped.raw()) } { Ok(_) => Ok(()), Err(e) => { if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) { @@ -237,7 +233,7 @@ mod imp { unsafe fn complete(&mut self, status: &CompletionStatus) { let prev = self.dst.len(); - self.dst.set_len(prev + status.bytes_transferred() as usize); + unsafe { self.dst.set_len(prev + status.bytes_transferred() as usize) }; if status.bytes_transferred() == 0 { self.done = true; } @@ -251,7 +247,9 @@ mod imp { if v.capacity() == v.len() { v.reserve(1); } - slice::from_raw_parts_mut(v.as_mut_ptr().add(v.len()), v.capacity() - v.len()) + let data = unsafe { v.as_mut_ptr().add(v.len()) }; + let len = v.capacity() - v.len(); + unsafe { slice::from_raw_parts_mut(data, len) } } } diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index 1ab62e99235f..66875e25242b 100644 --- a/lib/line-index/src/lib.rs +++ b/lib/line-index/src/lib.rs @@ -275,21 +275,21 @@ unsafe fn analyze_source_file_sse2( let ptr = src_bytes.as_ptr() as *const __m128i; // We don't know if the pointer is aligned to 16 bytes, so we // use `loadu`, which supports unaligned loading. - let chunk = _mm_loadu_si128(ptr.add(chunk_index)); + let chunk = unsafe { _mm_loadu_si128(ptr.add(chunk_index)) }; // For character in the chunk, see if its byte value is < 0, which // indicates that it's part of a UTF-8 char. - let multibyte_test = _mm_cmplt_epi8(chunk, _mm_set1_epi8(0)); + let multibyte_test = unsafe { _mm_cmplt_epi8(chunk, _mm_set1_epi8(0)) }; // Create a bit mask from the comparison results. - let multibyte_mask = _mm_movemask_epi8(multibyte_test); + let multibyte_mask = unsafe { _mm_movemask_epi8(multibyte_test) }; // If the bit mask is all zero, we only have ASCII chars here: if multibyte_mask == 0 { assert!(intra_chunk_offset == 0); // Check for newlines in the chunk - let newlines_test = _mm_cmpeq_epi8(chunk, _mm_set1_epi8(b'\n' as i8)); - let newlines_mask = _mm_movemask_epi8(newlines_test); + let newlines_test = unsafe { _mm_cmpeq_epi8(chunk, _mm_set1_epi8(b'\n' as i8)) }; + let newlines_mask = unsafe { _mm_movemask_epi8(newlines_test) }; if newlines_mask != 0 { // All control characters are newlines, record them @@ -349,8 +349,8 @@ unsafe fn analyze_source_file_sse2( unsafe fn move_mask(v: std::arch::aarch64::uint8x16_t) -> u64 { use std::arch::aarch64::*; - let nibble_mask = vshrn_n_u16(vreinterpretq_u16_u8(v), 4); - vget_lane_u64(vreinterpret_u64_u8(nibble_mask), 0) + let nibble_mask = unsafe { vshrn_n_u16(vreinterpretq_u16_u8(v), 4) }; + unsafe { vget_lane_u64(vreinterpret_u64_u8(nibble_mask), 0) } } #[target_feature(enable = "neon")] @@ -368,7 +368,7 @@ unsafe fn analyze_source_file_neon( let chunk_count = src.len() / CHUNK_SIZE; - let newline = vdupq_n_s8(b'\n' as i8); + let newline = unsafe { vdupq_n_s8(b'\n' as i8) }; // This variable keeps track of where we should start decoding a // chunk. If a multi-byte character spans across chunk boundaries, @@ -378,21 +378,21 @@ unsafe fn analyze_source_file_neon( for chunk_index in 0..chunk_count { let ptr = src_bytes.as_ptr() as *const i8; - let chunk = vld1q_s8(ptr.add(chunk_index * CHUNK_SIZE)); + let chunk = unsafe { vld1q_s8(ptr.add(chunk_index * CHUNK_SIZE)) }; // For character in the chunk, see if its byte value is < 0, which // indicates that it's part of a UTF-8 char. - let multibyte_test = vcltzq_s8(chunk); + let multibyte_test = unsafe { vcltzq_s8(chunk) }; // Create a bit mask from the comparison results. - let multibyte_mask = move_mask(multibyte_test); + let multibyte_mask = unsafe { move_mask(multibyte_test) }; // If the bit mask is all zero, we only have ASCII chars here: if multibyte_mask == 0 { assert!(intra_chunk_offset == 0); // Check for newlines in the chunk - let newlines_test = vceqq_s8(chunk, newline); - let mut newlines_mask = move_mask(newlines_test); + let newlines_test = unsafe { vceqq_s8(chunk, newline) }; + let mut newlines_mask = unsafe { move_mask(newlines_test) }; // If the bit mask is not all zero, there are newlines in this chunk. if newlines_mask != 0 {