rust-clippy
rust-clippy copied to clipboard
`impl_trait_in_params` doesn't work for non-public functions
Summary
The lint only appears for public functions (i.e. public API surface of a library).
Lint Name
impl_trait_in_params
Reproducer
I tried this code:
#![deny(clippy::impl_trait_in_params)]
#![allow(dead_code)]
pub fn true_positive(_: impl Sized) {}
fn false_negative(_: impl Sized) {}
I expected to see this happen:
2 instances of impl_trait_in_params should appear, one for each function.
Instead, this happened:
Only 1 instance appeared for the pub function:
error: `impl Trait` used as a function parameter
--> src/lib.rs:3:25
|
3 | pub fn true_positive(_: impl Sized) {}
| ^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#impl_trait_in_params
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::impl_trait_in_params)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: add a type parameter
|
3 | pub fn true_positive<{ /* Generic name */ }: Sized>(_: impl Sized) {}
| +++++++++++++++++++++++++++++++
Version
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
looks like it was intended by design, but the doc didn't specify that: https://github.com/rust-lang/rust-clippy/blob/a4a1a7365d60280b83d83374fda97742fb959239/clippy_lints/src/functions/impl_trait_in_params.rs#L61
Hmm, I just read through the PR in which the lint was added (https://github.com/rust-lang/rust-clippy/pull/10197) and there wasn't any discussion about whether this was desirable or intentional. Maybe it being written this way was accidental. Either way, I personally think this lint should apply to all instances, not just public ones.
This was as per the original request: https://github.com/rust-lang/rust-clippy/issues/10030. The rational is that in internal items it's easy to just write impl Foo and because the function is internal you can change it at any time if you realize you need a turbofish.