I’m getting a weird compiler warning from using a macro that I wrote:
warning: unnecessary parentheses around type
--> crates/apub/src/activities/community/announce.rs:43:19
|
43 | #[activity_handler(LemmyContext)]
| ^ ^
|
= note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
|
43 - #[activity_handler(LemmyContext)]
43 + #[activity_handlerLemmyContext]
|
Here is the code that it is warning about:
#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
#[serde(untagged)]
#[activity_handler(LemmyContext)]
pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment),
CreateOrUpdatePost(Box<CreateOrUpdatePost>),
...
}
The warning is clearly wrong, because the code compiles, but if after applying the suggested fix, it breaks. I also can’t find any info about this problem, or how to fix it. In general there seems to be a real lack of resources for Rust macros.
So does anyone here know how to get rid of the warning, without explicitly ignoring it?
For the record, here is the solution
The cargo-expand tool is very useful for debugging issues like this, check it out. My guess here is that since you’re using the tokens from the attribute directly as the DataType type definition, those tokens include the surrounding parentheses, so you end up with type DataType = (LemmyContext);, which triggers that warning. The reason the compiler suggestion is wrong is that it’s marking the warning on the original tokens which came from the attribute.
I’ve had this occasionally happen too, clippy lints aren’t perfect. Just disable it on that specific line:
https://stackoverflow.com/questions/39269408/how-to-quiet-a-warning-for-a-single-statement-in-rust
after searching the github issues it looks like rust does report “unnecessary parentheses” a lot more than it should
due to the buggy nature of macros, opening an issue on github is probably the best way to solve (i suspect there is nothing wrong with the code itself)