From 42b71a569163dc73b872fa75955554e1c7adcefd Mon Sep 17 00:00:00 2001 From: Alisa Sireneva Date: Wed, 30 Oct 2024 10:31:48 +0300 Subject: [PATCH] Clippy --- src/api.rs | 10 ++++++++-- src/backend/itanium.rs | 4 ++-- src/backend/mod.rs | 2 +- src/backend/seh.rs | 2 +- src/heterogeneous_stack/align.rs | 6 ++++-- src/heterogeneous_stack/heap.rs | 10 ++++++++-- src/lib.rs | 15 +++++++++++++-- src/stacked_exceptions.rs | 1 - 8 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/api.rs b/src/api.rs index 539bc34..c8618c7 100644 --- a/src/api.rs +++ b/src/api.rs @@ -63,7 +63,10 @@ pub unsafe fn throw(cause: E) -> ! { /// /// assert_eq!(res, Err("Oops!")); /// ``` -#[expect(clippy::missing_errors_doc)] +#[expect( + clippy::missing_errors_doc, + reason = "`Err` value is described immediately" +)] #[inline] pub unsafe fn catch(func: impl FnOnce() -> R) -> Result { // SAFETY: @@ -169,7 +172,10 @@ impl InFlightException { /// // SAFETY: g only ever throws Error /// println!("{}", unsafe { catch::<_, Error>(|| g()) }.unwrap_err()); /// ``` -#[expect(clippy::missing_errors_doc)] +#[expect( + clippy::missing_errors_doc, + reason = "`Err` value is described immediately" +)] #[inline] pub unsafe fn intercept(func: impl FnOnce() -> R) -> Result)> { // SAFETY: Requirements forwarded. diff --git a/src/backend/itanium.rs b/src/backend/itanium.rs index 463aec0..baea095 100644 --- a/src/backend/itanium.rs +++ b/src/backend/itanium.rs @@ -22,7 +22,7 @@ unsafe impl ThrowByPointer for ActiveBackend { unsafe fn throw(ex: *mut Header) -> ! { // SAFETY: We provide a valid exception header. unsafe { - #[expect(clippy::used_underscore_items)] + #[expect(clippy::used_underscore_items, reason = "External API")] _Unwind_RaiseException(ex); } } @@ -99,7 +99,7 @@ unsafe impl ThrowByPointer for ActiveBackend { // If project-ffi-unwind changes the rustc behavior, we might have to update this // code. unsafe { - #[expect(clippy::used_underscore_items)] + #[expect(clippy::used_underscore_items, reason = "External API")] _Unwind_RaiseException(ex); } } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index bd5a80a..8646833 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -46,7 +46,7 @@ /// /// The user of this trait is allowed to reuse the header when rethrowing exceptions. In particular, /// the return value of `intercept` may be used as an argument to `throw`. -#[allow(dead_code)] +#[allow(dead_code, reason = "This is only used by some of the backends")] pub unsafe trait ThrowByPointer { /// An exception header. /// diff --git a/src/backend/seh.rs b/src/backend/seh.rs index 2f9b4f9..e13c830 100644 --- a/src/backend/seh.rs +++ b/src/backend/seh.rs @@ -390,7 +390,7 @@ unsafe fn cxx_throw(exception_object: *mut ExceptionHeader, throw_info: *const T RaiseException( EH_EXCEPTION_NUMBER, EH_NONCONTINUABLE, - #[expect(clippy::arithmetic_side_effects)] + #[expect(clippy::arithmetic_side_effects, reason = "This is a constant")] (core::mem::size_of::() / core::mem::size_of::()) .try_into() .unwrap(), diff --git a/src/heterogeneous_stack/align.rs b/src/heterogeneous_stack/align.rs index 6401286..0aad474 100644 --- a/src/heterogeneous_stack/align.rs +++ b/src/heterogeneous_stack/align.rs @@ -1,6 +1,8 @@ pub fn assert_aligned(n: usize) { - // Clippy thinks % can panic here, but the divisor is never 0 and we're working in unsigned - #[expect(clippy::arithmetic_side_effects)] + #[expect( + clippy::arithmetic_side_effects, + reason = "The divisor is never 0 and we're working in unsigned" + )] let modulo = n % align_of::(); assert!(modulo == 0, "Unaligned"); } diff --git a/src/heterogeneous_stack/heap.rs b/src/heterogeneous_stack/heap.rs index 0452f7c..f00b048 100644 --- a/src/heterogeneous_stack/heap.rs +++ b/src/heterogeneous_stack/heap.rs @@ -23,7 +23,10 @@ impl Heap { /// # Panics /// /// Panics if `n` is not a multiple of `align_of::()` or `n` is 0, or if out of memory. - #[expect(clippy::unused_self)] + #[expect( + clippy::unused_self, + reason = "Using a static method is harder in presence of generic parameters" + )] pub fn alloc(&self, n: usize) -> *mut u8 { assert_aligned::(n); assert_ne!(n, 0, "Allocating 0 bytes is invalid"); @@ -43,7 +46,10 @@ impl Heap { /// The caller must ensure that the pointer was produced by a call to [`Heap::alloc`] with the /// same value of `n`. In addition, references to the deallocated memory must not be used after /// `dealloc` is called. - #[expect(clippy::unused_self)] + #[expect( + clippy::unused_self, + reason = "Using a static method is harder in presence of generic parameters" + )] pub unsafe fn dealloc(&self, ptr: *mut u8, n: usize) { // SAFETY: alloc would fail if the preconditions for this weren't established let layout = unsafe { Layout::from_size_align_unchecked(n, align_of::()) }; diff --git a/src/lib.rs b/src/lib.rs index 8daeee9..296d735 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,15 +74,21 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), feature(thread_local))] -#![cfg_attr(backend = "itanium", expect(internal_features))] +#![cfg_attr( + any(backend = "itanium", backend = "seh"), + expect( + internal_features, + reason = "Can't do anything about core::intrinsics::catch_unwind yet", + ) +)] #![cfg_attr(backend = "itanium", feature(core_intrinsics))] #![cfg_attr(backend = "seh", feature(core_intrinsics, fn_ptr_trait))] #![deny(unsafe_op_in_unsafe_fn)] #![warn( clippy::cargo, clippy::pedantic, - clippy::missing_const_for_fn, clippy::alloc_instead_of_core, + clippy::allow_attributes_without_reason, clippy::arithmetic_side_effects, clippy::as_underscore, clippy::assertions_on_result_states, @@ -102,6 +108,7 @@ clippy::inline_asm_x86_att_syntax, clippy::mem_forget, // use ManuallyDrop instead clippy::missing_assert_message, + clippy::missing_const_for_fn, clippy::missing_inline_in_public_items, clippy::mixed_read_write_in_expression, clippy::multiple_unsafe_ops_per_block, @@ -131,6 +138,10 @@ clippy::unused_result_ok, clippy::wildcard_enum_match_arm, )] +#![allow( + clippy::inline_always, + reason = "I'm not an idiot, this is a result of benchmarking/profiling" +)] extern crate alloc; diff --git a/src/stacked_exceptions.rs b/src/stacked_exceptions.rs index cd7023e..4e52348 100644 --- a/src/stacked_exceptions.rs +++ b/src/stacked_exceptions.rs @@ -176,7 +176,6 @@ const fn get_alloc_size() -> usize { } /// Push an exception onto the thread-local exception stack. -#[expect(clippy::inline_always)] #[inline(always)] pub fn push(cause: E) -> *mut Exception { // SAFETY: We don't let the stack leak past the call frame.