Skip to content

Fix min_ident_chars: ignore on trait impl. #15275

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 10 commits into
base: master
Choose a base branch
from

Conversation

t-webber
Copy link

@t-webber t-webber commented Jul 14, 2025

fixes #13396

changelog: [min_ident_chars]: ignore lint when implementing a trait, to respect [renamed_function_params]

@rustbot
Copy link
Collaborator

rustbot commented Jul 14, 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 Jul 14, 2025
Copy link

github-actions bot commented Jul 14, 2025

Lintcheck changes for b5b8d33

Lint Added Removed Changed
clippy::min_ident_chars 89 2491 39

This comment will be updated if you push new changes

@t-webber t-webber changed the title Min idents char Fix min_ident_chars: ignore on trait impl. Jul 14, 2025
@samueltardieu
Copy link
Member

For a local trait, it should warn about the short parameter name in the trait method definition. I don't see a test for this, and I suspect it won't warn. To suppress warnings about trait implementations, we should ensure it warns at the trait definition so that users are warned at least once about the usage of short identifiers.

@t-webber
Copy link
Author

t-webber commented Jul 15, 2025

Yes, I didn't look at that case, and it seems it never fired the lint even before my PR:

#![allow(dead_code)]
#![warn(clippy::min_ident_chars)]

trait Trait {
    fn func(x: u32) -> u32;
}

struct Struct;

impl Trait for Struct {
    fn func(x: u32) -> u32 {
        x
    }
}

fn main() {}

This code doesn't produce any warnings.

For functions with one char, it works though, and it warns on trait definition only, so I guess I should do the same thing for one-char-long params

@t-webber
Copy link
Author

t-webber commented Jul 28, 2025

My previous message wasn't correct, as x is allowed as a single char by default. If you change my code and put another variable than x, the lint fires. It lints only on the implementation of the function, not the definition though.

Strange thing that the CIs stopped passing on an error in the source file, that I didn't change on this commit

Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Could you add a "Limitations" section to the lint definition, after the "Example"? Something like

    /// ### Limitations
    /// Trait implementations which use the same function or parameter name as the trait declaration will
    /// not be warned about, even if the name is below the configured limit.

Also, it would be great to open an issue with label C-enhancement suggesting to add a new configuration option for this lint so that it triggers when using short parameter names even in the case of trait implementation: while we cannot do anything about the function names themselves, we can suggest renaming the function parameters in the implementation.

@samueltardieu
Copy link
Member

r? samueltardieu
@rustbot author

@rustbot rustbot assigned samueltardieu and unassigned dswij Jul 29, 2025
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 29, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@t-webber
Copy link
Author

Also, it would be great to open an issue with label C-enhancement suggesting to add a new configuration option for this lint so that it triggers when using short parameter names even in the case of trait implementation: while we cannot do anything about the function names themselves, we can suggest renaming the function parameters in the implementation.

cf. #15365

t-webber added 2 commits July 29, 2025 18:19
Trait implementations which use the same function or parameter name as the trait declaration will not be warned about, even if the name is below the configured limit.
@t-webber
Copy link
Author

@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Jul 29, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 29, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

It is starting to look good. Could you test the suggested change (and added tests), and tell me what you think about it? Also, you may squash the commits into one, that will be necessary before merging.

Comment on lines +143 to +149
for (_, parent_node) in cx.tcx.hir_parent_iter(hir_id) {
if let Node::Item(item) = parent_node
&& let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = &item.kind
{
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Won't that test prevent linting of inner functions? For example, if a function in a trait implementation defines a closure using a single letter parameter name, will it still get linted?

Can't you just look at the direct parent?

Suggested change
for (_, parent_node) in cx.tcx.hir_parent_iter(hir_id) {
if let Node::Item(item) = parent_node
&& let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = &item.kind
{
return;
}
}
if matches!(cx.tcx.parent_hir_node(hir_id), Node::TraitRef(_)) {
return;
}

Can you also check that inner function and their parameters are checked, even in such a function?

impl D for Issue13396 {
    fn f(g: i32) {
        fn h() {}
        //~^ min_ident_chars
        fn inner(a: i32) {}
        //~^ min_ident_chars
    }
    fn long(long: i32) {}
}

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

min_ident_chars triggers on implementation block for foreign traits
4 participants