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

Add crate_in_macro_def lint #8576

Merged
merged 8 commits into from
Mar 30, 2022
Merged

Conversation

smoelius
Copy link
Contributor

This PR adds a lint to check for crate as opposed to $crate used in a macro definition.

I think this can close #4798. That issue focused on the case where the macro author "imports something into said macro."

But I think use of crate is likely to be a bug whether it appears in a use statement or not. There could be some use case I am failing to see, though. (cc: @nilscript @flip1995)

changelog: crate_in_macro_def

@rust-highfive
Copy link

r? @llogiq

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 24, 2022
Comment on lines 5 to 7
// For cases where use of `crate` is intentional, applying `allow` to the macro definition
// should suppress the lint.
#[allow(clippy::crate_in_macro_def)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can go in the same test file as above or be removed, as you aren't having to do something specific to get it working

Could be worth noting in the description that the allow attribute has to go on the macro_rules itself, ie

#[allow(clippy::crate_in_macro_def)]
macro_rules! ok { ... crate::foo ... }

macro_rules! lints {
...
    // Still errors since the allow attribute is part of the macro token stream
     #[allow(clippy::crate_in_macro_def)]
    crate::foo
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can go in the same test file as above or be removed

My concern is that someone uses crate intentionally, they apply allow to the macro definition, and then a future change to Clippy breaks their uses of allow. E.g., it would be annoying to have to use allow at every call site. The test is meant to help prevent this possibility.

Comment on lines 20 to 28
/// ### Example
/// ```rust
/// macro_rules! print_message {
/// () => {
/// println!("{}", crate::MESSAGE);
/// };
/// }
/// pub const MESSAGE: &str = "Hello!";
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For internal macros crate is fine, you could check for a macro_export attribute using check_item to avoid some false positives

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am embarrassed that I didn't think of this. 😬

@smoelius
Copy link
Contributor Author

Thank you for the review @Alexendoo. I think cb307bb addresses all of your comments.

Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. I have but a small number of suggestions. The only thing that is really worrying me is the lint level.

/// ```
#[clippy::version = "1.61.0"]
pub CRATE_IN_MACRO_DEF,
correctness,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a correctness lint. Correctness implies the lint level is deny. While the lint will be right in the general case, there may be exceptions and I would not want to stop the build for that. My suggestion here would be to make it a suspicious lint (which is warn by default).

clippy_lints/src/crate_in_macro_def.rs Outdated Show resolved Hide resolved
clippy_lints/src/crate_in_macro_def.rs Outdated Show resolved Hide resolved
clippy_lints/src/crate_in_macro_def.rs Outdated Show resolved Hide resolved
smoelius and others added 4 commits March 30, 2022 12:40
Co-authored-by: llogiq <bogusandre@gmail.com>
Co-authored-by: llogiq <bogusandre@gmail.com>
Co-authored-by: llogiq <bogusandre@gmail.com>
@llogiq
Copy link
Contributor

llogiq commented Mar 30, 2022

Thank you! @bors r+

@bors
Copy link
Collaborator

bors commented Mar 30, 2022

📌 Commit d6eb82c has been approved by llogiq

@smoelius
Copy link
Contributor Author

smoelius commented Mar 30, 2022

@llogiq Sorry, sorry. One more change was needed to fix the error message.

@llogiq
Copy link
Contributor

llogiq commented Mar 30, 2022

@bors r-

@llogiq
Copy link
Contributor

llogiq commented Mar 30, 2022

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 30, 2022

📌 Commit aaf04dc has been approved by llogiq

@bors
Copy link
Collaborator

bors commented Mar 30, 2022

⌛ Testing commit aaf04dc with merge fe7254f...

@bors
Copy link
Collaborator

bors commented Mar 30, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing fe7254f to master...

@smoelius smoelius deleted the crate_in_macro_def branch March 30, 2022 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint request: Warn when importing inside a macro and not using the $ keyword.
5 participants