rust-lang / rust-lang/rust-clippy
False positive for only_used_in_recursion for trait implementations
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
I stumbled over this warning in a trait implementation. The issue is that a trait defines a method with multiple parameters, but not all its implementations use all the parameters. Some will simply pass the parameter to recursive calls. However, they need to still define (and pass) the parameter to conform to the trait definition.
More concretely, I identified this issue with the physical_optimizer::AggregateStatistics::optimize implementation of the physical_optimizer::PhysicalOptimizerRule::optimize trait in the DataFusion project.
In my opinion, Clippy needs to take more context into account when the warning is triggered in a trait implementation. For example, when the rule triggers for all implementations of a trait (and it is private), then a warning could still suggest to remove that parameter. If at least one implementation is using the parameter, it shouldn't trigger.
A more incremental improvement would be to simply not trigger whenever this warning is triggered on a trait implementation.
Lint Name
only_used_in_recursion
Reproducer
I tried this code:
trait DoSomething {
fn do_something(&self, a: i32, b: i32) -> i32;
}
struct Impl1 {}
// This works fine
impl DoSomething for Impl1 {
fn do_something(&self, a: i32, b: i32) -> i32 {
a + b
}
}
struct Impl2 {}
// I get a a warning here
impl DoSomething for Impl2 {
fn do_something(&self, a: i32, b: i32) -> i32 {
if a == 0 {
self.do_something(a, b)
} else {
self.do_something(a - 1, b)
}
}
}
I saw this happen:
warning: parameter is only used in recursion
--> src/test.rs:24:36
|
24 | fn do_something(&self, a: i32, b: i32) -> i32 {
| ^ help: if this is intentional, prefix it with an underscore: `_b`
|
note: parameter used here
--> src/test.rs:26:34
|
26 | self.do_something(a, b)
| ^
27 | } else {
28 | self.do_something(a - 1, b)
| ^
I expected to see this happen:
I expect it to pass.
Version
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7
and
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the warning with the trait and Impl2 example in the issue, then locate the implementation and tests for the only_used_in_recursion lint. Compare the behavior with the linked DataFusion implementations in aggregate_statistics.rs and optimizer.rs. Done means the trait implementation no longer receives this warning while the lint continues to report applicable non-trait cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100