this post was submitted on 28 Sep 2021
8 points (100.0% liked)

Rust Programming

8438 readers
42 users here now

founded 5 years ago
MODERATORS
 

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>),
    ...
}

Macro definition is here.

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?

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 3 years ago

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.