diff --git a/clippy_lints/src/functions/impl_trait_in_params.rs b/clippy_lints/src/functions/impl_trait_in_params.rs index 4a776dd26133..ee66c841ed25 100644 --- a/clippy_lints/src/functions/impl_trait_in_params.rs +++ b/clippy_lints/src/functions/impl_trait_in_params.rs @@ -6,7 +6,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, GenericParam, Generics, HirId, ImplItem, ImplItemKind, TraitItem, TraitItemKind}; use rustc_lint::LateContext; use rustc_span::symbol::Ident; -use rustc_span::Span; +use rustc_span::{BytePos, Span}; use super::IMPL_TRAIT_IN_PARAMS; @@ -89,12 +89,16 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { pub(super) fn check_trait_item(cx: &LateContext<'_>, trait_item: &TraitItem<'_>, avoid_breaking_exported_api: bool) { if_chain! { if !avoid_breaking_exported_api; - if let TraitItemKind::Fn(sig, _) = trait_item.kind; + if let TraitItemKind::Fn(_, _) = trait_item.kind; + if let hir::Node::Item(item) = cx.tcx.hir().get_parent(trait_item.hir_id()); + // ^^ (Will always be a trait) + if !item.vis_span.is_empty(); // Is public if !is_in_test_function(cx.tcx, trait_item.hir_id()); then { for param in trait_item.generics.params { if param.is_impl_trait() { - report(cx, param, &trait_item.ident, trait_item.generics, sig.decl.inputs[0].span); + let sp = trait_item.ident.span.with_hi(trait_item.ident.span.hi() + BytePos(1)); + report(cx, param, &trait_item.ident, trait_item.generics, sp.shrink_to_hi()); } } } diff --git a/tests/ui-toml/impl_trait_in_params/clippy.toml b/tests/ui-toml/impl_trait_in_params/clippy.toml new file mode 100644 index 000000000000..87e1f235741d --- /dev/null +++ b/tests/ui-toml/impl_trait_in_params/clippy.toml @@ -0,0 +1 @@ +avoid-breaking-exported-api = false \ No newline at end of file diff --git a/tests/ui-toml/impl_trait_in_params/impl_trait_in_params.rs b/tests/ui-toml/impl_trait_in_params/impl_trait_in_params.rs new file mode 100644 index 000000000000..08fc7edf1c86 --- /dev/null +++ b/tests/ui-toml/impl_trait_in_params/impl_trait_in_params.rs @@ -0,0 +1,16 @@ +//! As avoid-breaking-exported-api is `false`, nothing here should lint +#![warn(clippy::impl_trait_in_params)] +#![no_main] +//@no-rustfix + +pub trait Trait {} + +trait Private { + fn t(_: impl Trait); + fn tt(_: T); +} + +pub trait Public { + fn t(_: impl Trait); //~ ERROR: `impl Trait` used as a function parameter + fn tt(_: T); +} diff --git a/tests/ui-toml/impl_trait_in_params/impl_trait_in_params.stderr b/tests/ui-toml/impl_trait_in_params/impl_trait_in_params.stderr new file mode 100644 index 000000000000..80c4f5ed4b0c --- /dev/null +++ b/tests/ui-toml/impl_trait_in_params/impl_trait_in_params.stderr @@ -0,0 +1,15 @@ +error: `impl Trait` used as a function parameter + --> $DIR/impl_trait_in_params.rs:14:13 + | +LL | fn t(_: impl Trait); + | ^^^^^^^^^^ + | + = note: `-D clippy::impl-trait-in-params` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::impl_trait_in_params)]` +help: add a type parameter + | +LL | fn t<{ /* Generic name */ }: Trait>(_: impl Trait); + | +++++++++++++++++++++++++++++++ + +error: aborting due to previous error + diff --git a/tests/ui-toml/impl_trait_in_params/true/clippy.toml b/tests/ui-toml/impl_trait_in_params/true/clippy.toml deleted file mode 100644 index 0e7ffd070a35..000000000000 --- a/tests/ui-toml/impl_trait_in_params/true/clippy.toml +++ /dev/null @@ -1 +0,0 @@ -avoid-breaking-exported-api = true \ No newline at end of file diff --git a/tests/ui-toml/impl_trait_in_params/true/impl_trait_in_params.rs b/tests/ui-toml/impl_trait_in_params/true/impl_trait_in_params.rs deleted file mode 100644 index d1176ab89635..000000000000 --- a/tests/ui-toml/impl_trait_in_params/true/impl_trait_in_params.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! As avoid-breaking-exported-api is `true`, nothing here should lint -#![warn(clippy::impl_trait_in_params)] -#![no_main] - -trait Trait {} - -trait T { - fn t(_: impl Trait); - fn tt(_: T); -} diff --git a/tests/ui/impl_trait_in_params.rs b/tests/ui/impl_trait_in_params.rs index 825bb76e3f78..a6251a370d1d 100644 --- a/tests/ui/impl_trait_in_params.rs +++ b/tests/ui/impl_trait_in_params.rs @@ -19,8 +19,14 @@ fn d(_: impl AnotherTrait) {} //------ IMPLS -trait T { - // See test in ui-toml for a case where avoid-breaking-exported-api is set to true +pub trait Public { + // See test in ui-toml for a case where avoid-breaking-exported-api is set to false + fn t(_: impl Trait); + fn tt(_: T) {} +} + +trait Private { + // This shouldn't lint fn t(_: impl Trait); fn tt(_: T) {} } @@ -34,7 +40,7 @@ impl S { } // Trying with traits -impl T for S { +impl Public for S { fn t(_: impl Trait) {} } diff --git a/tests/ui/impl_trait_in_params.stderr b/tests/ui/impl_trait_in_params.stderr index 9f6e545c3f28..0ae7a3672d19 100644 --- a/tests/ui/impl_trait_in_params.stderr +++ b/tests/ui/impl_trait_in_params.stderr @@ -23,7 +23,7 @@ LL | pub fn c(_: C, _: impl Trait) {} | +++++++++++++++++++++++++++++++ error: `impl Trait` used as a function parameter - --> $DIR/impl_trait_in_params.rs:30:17 + --> $DIR/impl_trait_in_params.rs:36:17 | LL | pub fn h(_: impl Trait) {} | ^^^^^^^^^^ @@ -34,7 +34,7 @@ LL | pub fn h<{ /* Generic name */ }: Trait>(_: impl Trait) {} | +++++++++++++++++++++++++++++++ error: `impl Trait` used as a function parameter - --> $DIR/impl_trait_in_params.rs:33:45 + --> $DIR/impl_trait_in_params.rs:39:45 | LL | pub fn k>(_: K, _: impl AnotherTrait) {} | ^^^^^^^^^^^^^^^^^^^^^^