Skip to content

False positive for only_used_in_recursion for trait implementations #14566

Open
@DerGut

Description

@DerGut

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions