Skip to content

Fix ptr_arg suggests changes when it's actually better not to bother #15105

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

Conversation

immersum
Copy link

@immersum immersum commented Jun 22, 2025

No longer suggests &[i32] or &mut [i32] instead of &Vec<i32> or &mut Vec<i32> (also: Path and PathBuf, etc.) for the parameter type when the parameter name starts with an underscore (or, if that does not start with one, then a local let binding in the function body, pointing to the same value, does) – (Fixes #13489, fixes #13728)

changelog: fix false positive: [ptr_arg] no longer triggers with underscore binding to &mut argument
changelog: fix false positive: [ptr_arg] no longer triggers with underscore binding to &T or &mut T argument
Edit: This change has been extended to all references, not just mutable ones. See discussion below.

@rustbot
Copy link
Collaborator

rustbot commented Jun 22, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
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 22, 2025
changelog: fix false positive: [`ptr_arg`] no longer triggers with
underscore binding to `&mut` argument
@immersum immersum force-pushed the feature/fix_ptr_arg branch from fc15f8a to a78eeb3 Compare June 22, 2025 20:57
@immersum
Copy link
Author

immersum commented Jun 23, 2025

This change only makes an exception for mutable all reference types having binding names with an underscore prefix.

An exception is not made in the following scenarios:

  • _x: &Vec<i32> – It's not considered okay because requiring a reference to Vec to be passed should never be necessary. In this case, let's still suggest _x: &[i32] despite that the parameter is intentionally unused.
    Note: Maybe, when the underscore is present, modify the hint to suggest either &[i32] or &mut Vec<i32>?
    Edit: The underscore named parameter is a good indicator that the signature matters for some reason.
  • x: &mut Vec<i32> (or x: &Vec<i32>), but when x is not used – It may or may not be considered okay. However, such a case is outside the scope of this change.
    Note: Maybe decide whether, in general, an unused warning should inhibit ptr_arg lints, and open a new issue?

@Jarcho
Copy link
Contributor

Jarcho commented Jun 25, 2025

I would ignore all references, not just mutable ones. An underscore named parameter is a good indicator that the signature matters for some reason (e.g. for function pointers).

@immersum
Copy link
Author

For example, it matters for methods that are not otherwise possible to call, or the user may want to clone the value.

I've applied the necessary changes. I've also updated the parameter names in some earlier tests so that they remain effective.

@immersum immersum force-pushed the feature/fix_ptr_arg branch from 2d36728 to 7fe341c Compare June 26, 2025 08:48
…o bother

changelog: fix false positive: [`ptr_arg`] no longer triggers with
underscore binding to `&T` or `&mut T` argument
@immersum immersum force-pushed the feature/fix_ptr_arg branch from 7fe341c to dbb0910 Compare June 26, 2025 10:03
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.

Don't emit ptr_arg warning about unused function arguments ptr_arg should not warn about &mut Vec<T> for underscore prefixed parameters
3 participants