Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to disable a lint for all macro invocations? #11472

Closed
pitdicker opened this issue Sep 8, 2023 · 5 comments
Closed

Is it possible to disable a lint for all macro invocations? #11472

pitdicker opened this issue Sep 8, 2023 · 5 comments

Comments

@pitdicker
Copy link

Description

Is there a way to suppress a lint (zero_prefixed_literal in my case) for all invocations of a macro?

For chrono (chronotope/chrono#1270) I am trying to write macros that can be used as

time!(7:03:25);

But that generates warnings (https://github.com/chronotope/chrono/actions/runs/6119764599/job/16610345590?pr=1270)

I am looking for a solution that doesn't require users to disable the lint. Maybe it can be solved by adjusting the macro, I just don't know enough about this part of Rust yet.

Version

No response

Additional Labels

No response

@Centri3
Copy link
Member

Centri3 commented Sep 8, 2023

You can have it expand to

#[allow(clippy::zero_prefixed_literal)]
{
    (actual code)
}

However, it's worth noting that this lint already checks if it's in an external macro (i.e., macro outside the current crate) and doesn't lint if so. So this will only come up for chrono in particular.

@Alexendoo
Copy link
Member

The literals aren't coming from an expansion so it'd still be linted in other crates

@pitdicker
Copy link
Author

Thank you for the help, but as you say the literals are in the other crate.
Are there tricks to make clippy ignore the macro arguments?

@Alexendoo
Copy link
Member

I don't think so, but there is a trick to get rid of an indentation level for the allow attribute. Since the tail expression of a block can have attributes you can do

    ($y:literal-$m:literal-$d:literal) => {{
        #[allow(clippy::zero_prefixed_literal)]
        match $crate::NaiveDate::from_ymd_opt($y, $m, $d) {
            Some(d) => d,
            None => panic!("invalid calendar date"),
        }
    }};

@pitdicker
Copy link
Author

This is perfect, thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants