Skip to content

Fix unused_trait_names FP when as name is from macro input #14947

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

profetia
Copy link
Contributor

@profetia profetia commented Jun 1, 2025

Closes #14924

changelog: [unused_trait_names] fix FP when as name is from macro input

@rustbot
Copy link
Collaborator

rustbot commented Jun 1, 2025

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 1, 2025
Comment on lines +76 to +77
// Skip if the ident of `as` is from a macro's input
&& (!last_segment.ident.span.from_expansion() || ident.span.from_expansion())
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reasoning here? Why check only the last segment? For example, the following will still trigger the lint, I'm not sure it should:

    macro_rules! mac {
        ($i:ident) => { use $i::Tr; };
    }
    mac!(m);
error: importing trait that is only used anonymously
  --> tests/ui/unused_trait_names.rs:313:33
   |
LL |         ($i:ident) => { use $i::Tr; };
   |                                 ^^ help: use: `Tr as _`
LL |     }
LL |     mac!(m);
   |     ------- in this macro invocation
   

What do we want to see linted exactly here?

Copy link
Contributor Author

@profetia profetia Jun 1, 2025

Choose a reason for hiding this comment

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

The original implementation will lint items inside macros. Note that external macros will not be linted, only those locally since users can edit them. I think it is reasonable to ask users to change it to Tr as _ if the as part is not from the macro input. However, if the as part is from user input, like as $i, then it should not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we should ignore macros altogether

@@ -73,6 +73,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedTraitNames {
&& let Some(snip) = snippet_opt(cx, last_segment.ident.span)
&& self.msrv.meets(cx, msrvs::UNDERSCORE_IMPORTS)
&& !is_from_proc_macro(cx, &last_segment.ident)
// Skip if the ident of `as` is from a macro's input
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment does not seem to correspond to the test which also checks the last segment.

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.

FP unused_trait_names
4 participants