Skip to content

Conversation

ghost
Copy link

@ghost ghost 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
@ghost ghost force-pushed the feature/fix_ptr_arg branch from fc15f8a to a78eeb3 Compare June 22, 2025 20:57
@ghost
Copy link
Author

ghost 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).

@ghost
Copy link
Author

ghost commented Jun 26, 2025

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.

@ghost ghost force-pushed the feature/fix_ptr_arg branch 2 times, most recently from 7fe341c to dbb0910 Compare June 26, 2025 10:03
Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

Looks good. Can you just squash the two commits please.

@ghost ghost force-pushed the feature/fix_ptr_arg branch from dbb0910 to 76038d5 Compare July 14, 2025 13:00
changelog: fix false positive: [`ptr_arg`] no longer triggers with
underscore binding to `&mut` argument
@ghost ghost force-pushed the feature/fix_ptr_arg branch from 76038d5 to 75c330b Compare July 14, 2025 13:00
@ghost
Copy link
Author

ghost commented Jul 14, 2025

Sure, there you go, @Jarcho

@Jarcho Jarcho added this pull request to the merge queue Jul 16, 2025
Merged via the queue into rust-lang:master with commit 786330e Jul 16, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 16, 2025
@ghost ghost deleted the feature/fix_ptr_arg branch July 16, 2025 20:00
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

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

2 participants