#[expect(dead_code)]
mod unused {
struct Unused;
impl Clone for Unused {
fn clone(&self) -> Self {
todo!()
}
}
}
If I run cargo clippy on this I get:
warning: this lint expectation is unfulfilled
--> src/lib.rs:1:10
|
1 | #[expect(dead_code)]
| ^^^^^^^^^
|
= note: `#[warn(unfulfilled_lint_expectations)]` on by default
But If I remove the #[expect(dead_code)] I get:
warning: struct `Unused` is never constructed
--> src/lib.rs:2:12
|
2 | struct Unused;
| ^^^^^^
|
= note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
If I turn the expect into an allow there are no warnings. Also interestingly, if I remove the impl Clone for Unused the expect also works. So seems like the expect has problems if there are trait implementations for the unused type.
Thanks for the reply. Yes I agree that would be the right thing to do for this small example. In my real use case the struct gets generated through a macro so I cannot put it directly on the struct (I don't want it to put it inside the macro as for production use cases the type should always be used, the case where it is not used are just some tests of the macro).
So I agree putting it over the struct is better in the simple example, but shouldn't it also work if I put it over the module? I always thought #![expect(...)] always works when removing it would lead to some violations, but it seems like this is wrong for my example.
I think the more general problem is about mod/crate level expect(dead_code) with at least one type that is not part of the public API as even something like below doesn't compile:
Indeed. I discovered that some time ago only because I'm one of the pariahs that globally denys all lints. In the clippy::restriction group, you have lints like clippy::allow_attributes_without_reason which fire when you don't provide a reason.
Lint reasons and #[expect] were actually stabilized at the same time, in 1.81. So they are relatively new features, and there is lots of older code out there that doesn’t use reasons and just puts comments after their allows.