rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.28k stars 1.52k forks source link

`impl_trait_in_params` doesn't work for non-public functions #12792

Open CobaltCause opened 4 months ago

CobaltCause commented 4 months ago

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
J-ZhengLi commented 4 months ago

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

CobaltCause commented 4 months ago

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.

SUPERCILEX commented 1 month ago

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.