rust-lang / rust-lang/rust-clippy

FP: `used_underscore_items` lints on debatably legitimate use-cases

Open
#13,478 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I feel the new used_underscore_items lint is not universally applicable and would best live in restriction rather than pedantic, as there are sometimes legitimate use-cases for prefixing item names with a _.

Lint Name

used_underscore_items

Reproducer

As motivation, I'll give two examples where prefixing method names with _ makes sense:

First, as a way to reduce the impact of monomorphization on binary size for functions that have large bodies:

pub fn my_method(&self, param: impl AsRef<str>) {
    self._my_method(param.as_ref())
}

fn _my_method(&self, param: &str) {
    // large function body here
}

Second, as a way to introduce non-breaking changes when adding a new argument to a function. If we have the following initial code:

pub fn my_method(&self, param1: u32) {
    // impl
}

...and we want to add support for a second parameter without making breaking changes, we could transform it in the following way:

pub fn my_method(&self, param1: u32) {
    self._my_method(param1, None)
}

pub fn my_method_2(&self, param1: u32, param2: u64) {
    self._my_method(param1, Some(param2))
}

fn _my_method(&self, param1: u32, param2: Option<u64>) {
    // impl
}

In both cases I feel that naming the private method with a prefixed underscore is a valid choice to make, in order to preserve the name and to not conflict with the public wrapper method. Granted, maybe this still means it should be a pedantic lint, but either way I feel these two cases qualify as false positives.

Version

rustc 1.83.0-nightly (ed04567ba 2024-09-28)
binary: rustc
commit-hash: ed04567ba1d5956d1080fb8121caa005ce059e12
commit-date: 2024-09-28
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the used_underscore_items lint entry point and its existing tests, then compare the two reproducer patterns in the issue with the lint’s current behavior. Review the discussion about pedantic versus restriction classification and determine the agreed scope. Done means the accepted behavior and lint classification are reflected consistently in the implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.