Skip to content

Commit 1c66461

Browse files
ojeday86-dev
authored andcommitted
rust: start using the #[expect(...)] attribute
In Rust, it is possible to `allow` particular warnings (diagnostics, lints) locally, making the compiler ignore instances of a given warning within a given function, module, block, etc. It is similar to `#pragma GCC diagnostic push` + `ignored` + `pop` in C: #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" static void f(void) {} #pragma GCC diagnostic pop But way less verbose: #[allow(dead_code)] fn f() {} By that virtue, it makes it possible to comfortably enable more diagnostics by default (i.e. outside `W=` levels) that may have some false positives but that are otherwise quite useful to keep enabled to catch potential mistakes. The `#[expect(...)]` attribute [1] takes this further, and makes the compiler warn if the diagnostic was _not_ produced. For instance, the following will ensure that, when `f()` is called somewhere, we will have to remove the attribute: #[expect(dead_code)] fn f() {} If we do not, we get a warning from the compiler: warning: this lint expectation is unfulfilled --> x.rs:3:10 | 3 | #[expect(dead_code)] | ^^^^^^^^^ | = note: `#[warn(unfulfilled_lint_expectations)]` on by default This means that `expect`s do not get forgotten when they are not needed. See the next commit for more details, nuances on its usage and documentation on the feature. The attribute requires the `lint_reasons` [2] unstable feature, but it is becoming stable in 1.81.0 (to be released on 2024-09-05) and it has already been useful to clean things up in this patch series, finding cases where the `allow`s should not have been there. Thus, enable `lint_reasons` and convert some of our `allow`s to `expect`s where possible. This feature was also an example of the ongoing collaboration between Rust and the kernel -- we tested it in the kernel early on and found an issue that was quickly resolved [3]. Cc: Fridtjof Stoldt <[email protected]> Cc: Urgau <[email protected]> Link: https://rust-lang.github.io/rfcs/2383-lint-reasons.html#expect-lint-attribute [1] Link: rust-lang/rust#54503 [2] Link: rust-lang/rust#114557 [3] Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Tested-by: Gary Guo <[email protected]> Reviewed-by: Gary Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> (cherry picked from commit 1f9ed172545687e5c04c77490a45896be6d2e459) [pinned_init: sync ui tests with new expanded code. - Paolo] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3e11bd5 commit 1c66461

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

src/__internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
pub unsafe trait HasPinData {
6363
type PinData: PinData;
6464

65-
#[allow(clippy::missing_safety_doc)]
65+
#[expect(clippy::missing_safety_doc)]
6666
unsafe fn __pin_data() -> Self::PinData;
6767
}
6868

@@ -92,7 +92,7 @@ pub unsafe trait PinData: Copy {
9292
pub unsafe trait HasInitData {
9393
type InitData: InitData;
9494

95-
#[allow(clippy::missing_safety_doc)]
95+
#[expect(clippy::missing_safety_doc)]
9696
unsafe fn __init_data() -> Self::InitData;
9797
}
9898

src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
//! that you need to write `<-` instead of `:` for fields that you want to initialize in-place.
6363
//!
6464
//! ```rust
65-
//! # #![allow(clippy::disallowed_names)]
65+
//! # #![expect(clippy::disallowed_names)]
6666
//! # #![feature(allocator_api)]
6767
//! use pinned_init::*;
6868
//! # use core::pin::Pin;
@@ -85,7 +85,7 @@
8585
//! (or just the stack) to actually initialize a `Foo`:
8686
//!
8787
//! ```rust
88-
//! # #![allow(clippy::disallowed_names)]
88+
//! # #![expect(clippy::disallowed_names)]
8989
//! # #![feature(allocator_api)]
9090
//! # #[path = "../examples/mutex.rs"] mod mutex; use mutex::*;
9191
//! # use pinned_init::*;
@@ -277,7 +277,7 @@ pub use pinned_init_macro::{pin_data, pinned_drop, Zeroable};
277277
/// # Examples
278278
///
279279
/// ```rust
280-
/// # #![allow(clippy::disallowed_names)]
280+
/// # #![expect(clippy::disallowed_names)]
281281
/// # #![feature(allocator_api)]
282282
/// # #[path = "../examples/mutex.rs"] mod mutex; use mutex::*;
283283
/// # use pinned_init::*;
@@ -329,7 +329,7 @@ macro_rules! stack_pin_init {
329329
/// # Examples
330330
///
331331
/// ```rust
332-
/// # #![allow(clippy::disallowed_names)]
332+
/// # #![expect(clippy::disallowed_names)]
333333
/// # #![feature(allocator_api)]
334334
/// # #[path = "../examples/error.rs"] mod error; use error::Error;
335335
/// # #[path = "../examples/mutex.rs"] mod mutex; use mutex::*;
@@ -356,7 +356,7 @@ macro_rules! stack_pin_init {
356356
/// ```
357357
///
358358
/// ```rust
359-
/// # #![allow(clippy::disallowed_names)]
359+
/// # #![expect(clippy::disallowed_names)]
360360
/// # #![feature(allocator_api)]
361361
/// # #[path = "../examples/error.rs"] mod error; use error::Error;
362362
/// # #[path = "../examples/mutex.rs"] mod mutex; use mutex::*;
@@ -479,7 +479,7 @@ macro_rules! stack_try_pin_init {
479479
/// Users of `Foo` can now create it like this:
480480
///
481481
/// ```rust
482-
/// # #![allow(clippy::disallowed_names)]
482+
/// # #![expect(clippy::disallowed_names)]
483483
/// # use pinned_init::*;
484484
/// # use core::pin::Pin;
485485
/// # #[pin_data]
@@ -953,7 +953,7 @@ pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
953953
/// # Examples
954954
///
955955
/// ```rust
956-
/// # #![allow(clippy::disallowed_names)]
956+
/// # #![expect(clippy::disallowed_names)]
957957
/// # use pinned_init::*;
958958
/// struct Foo {
959959
/// buf: [u8; 1_000_000],

src/macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@
182182
//! // Normally `Drop` bounds do not have the correct semantics, but for this purpose they do
183183
//! // (normally people want to know if a type has any kind of drop glue at all, here we want
184184
//! // to know if it has any kind of custom drop glue, which is exactly what this bound does).
185-
//! #[allow(drop_bounds)]
185+
//! #[expect(drop_bounds)]
186186
//! impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
187187
//! impl<T> MustNotImplDrop for Bar<T> {}
188188
//! // Here comes a convenience check, if one implemented `PinnedDrop`, but forgot to add it to
189189
//! // `#[pin_data]`, then this will error with the same mechanic as above, this is not needed
190190
//! // for safety, but a good sanity check, since no normal code calls `PinnedDrop::drop`.
191-
//! #[allow(non_camel_case_types)]
191+
//! #[expect(non_camel_case_types)]
192192
//! trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
193193
//! impl<
194194
//! T: ::pinned_init::PinnedDrop,
@@ -925,14 +925,14 @@ macro_rules! __pin_data {
925925
// `Drop`. Additionally we will implement this trait for the struct leading to a conflict,
926926
// if it also implements `Drop`
927927
trait MustNotImplDrop {}
928-
#[allow(drop_bounds)]
928+
#[expect(drop_bounds)]
929929
impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
930930
impl<$($impl_generics)*> MustNotImplDrop for $name<$($ty_generics)*>
931931
where $($whr)* {}
932932
// We also take care to prevent users from writing a useless `PinnedDrop` implementation.
933933
// They might implement `PinnedDrop` correctly for the struct, but forget to give
934934
// `PinnedDrop` as the parameter to `#[pin_data]`.
935-
#[allow(non_camel_case_types)]
935+
#[expect(non_camel_case_types)]
936936
trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
937937
impl<T: $crate::PinnedDrop>
938938
UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {}
@@ -989,7 +989,7 @@ macro_rules! __pin_data {
989989
//
990990
// The functions are `unsafe` to prevent accidentally calling them.
991991
#[allow(dead_code)]
992-
#[allow(clippy::missing_safety_doc)]
992+
#[expect(clippy::missing_safety_doc)]
993993
impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
994994
where $($whr)*
995995
{

tests/ui/expand/many_generics.expanded.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const _: () = {
3535
T: Bar<'a, 1>,
3636
{}
3737
#[allow(dead_code)]
38+
#[expect(clippy::missing_safety_doc)]
3839
impl<
3940
'a,
4041
'b: 'a,

tests/ui/expand/pin-data.expanded.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const _: () = {
1515
}
1616
impl ::core::marker::Copy for __ThePinData {}
1717
#[allow(dead_code)]
18+
#[expect(clippy::missing_safety_doc)]
1819
impl __ThePinData {
1920
unsafe fn _pin<E>(
2021
self,
@@ -54,10 +55,10 @@ const _: () = {
5455
__Unpin<'__pin>: ::core::marker::Unpin,
5556
{}
5657
trait MustNotImplDrop {}
57-
#[allow(drop_bounds)]
58+
#[expect(drop_bounds)]
5859
impl<T: ::core::ops::Drop> MustNotImplDrop for T {}
5960
impl MustNotImplDrop for Foo {}
60-
#[allow(non_camel_case_types)]
61+
#[expect(non_camel_case_types)]
6162
trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {}
6263
impl<
6364
T: ::pinned_init::PinnedDrop,

tests/ui/expand/pinned_drop.expanded.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const _: () = {
1515
}
1616
impl ::core::marker::Copy for __ThePinData {}
1717
#[allow(dead_code)]
18+
#[expect(clippy::missing_safety_doc)]
1819
impl __ThePinData {
1920
unsafe fn _pin<E>(
2021
self,

0 commit comments

Comments
 (0)