Skip to content

Commit 7755737

Browse files
committed
Fix tests, only lint for public tests
1 parent 5ed338d commit 7755737

File tree

8 files changed

+50
-19
lines changed

8 files changed

+50
-19
lines changed

clippy_lints/src/functions/impl_trait_in_params.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hir::intravisit::FnKind;
66
use rustc_hir::{Body, GenericParam, Generics, HirId, ImplItem, ImplItemKind, TraitItem, TraitItemKind};
77
use rustc_lint::LateContext;
88
use rustc_span::symbol::Ident;
9-
use rustc_span::Span;
9+
use rustc_span::{BytePos, Span};
1010

1111
use super::IMPL_TRAIT_IN_PARAMS;
1212

@@ -89,12 +89,16 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
8989
pub(super) fn check_trait_item(cx: &LateContext<'_>, trait_item: &TraitItem<'_>, avoid_breaking_exported_api: bool) {
9090
if_chain! {
9191
if !avoid_breaking_exported_api;
92-
if let TraitItemKind::Fn(sig, _) = trait_item.kind;
92+
if let TraitItemKind::Fn(_, _) = trait_item.kind;
93+
if let hir::Node::Item(item) = cx.tcx.hir().get_parent(trait_item.hir_id());
94+
// ^^ (Will always be a trait)
95+
if !item.vis_span.is_empty(); // Is public
9396
if !is_in_test_function(cx.tcx, trait_item.hir_id());
9497
then {
9598
for param in trait_item.generics.params {
9699
if param.is_impl_trait() {
97-
report(cx, param, &trait_item.ident, trait_item.generics, sig.decl.inputs[0].span);
100+
let sp = trait_item.ident.span.with_hi(trait_item.ident.span.hi() + BytePos(1));
101+
report(cx, param, &trait_item.ident, trait_item.generics, sp.shrink_to_hi());
98102
}
99103
}
100104
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avoid-breaking-exported-api = false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! As avoid-breaking-exported-api is `false`, nothing here should lint
2+
#![warn(clippy::impl_trait_in_params)]
3+
#![no_main]
4+
//@no-rustfix
5+
6+
pub trait Trait {}
7+
8+
trait Private {
9+
fn t(_: impl Trait);
10+
fn tt<T: Trait>(_: T);
11+
}
12+
13+
pub trait Public {
14+
fn t(_: impl Trait); //~ ERROR: `impl Trait` used as a function parameter
15+
fn tt<T: Trait>(_: T);
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: `impl Trait` used as a function parameter
2+
--> $DIR/impl_trait_in_params.rs:14:13
3+
|
4+
LL | fn t(_: impl Trait);
5+
| ^^^^^^^^^^
6+
|
7+
= note: `-D clippy::impl-trait-in-params` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::impl_trait_in_params)]`
9+
help: add a type parameter
10+
|
11+
LL | fn t<{ /* Generic name */ }: Trait>(_: impl Trait);
12+
| +++++++++++++++++++++++++++++++
13+
14+
error: aborting due to previous error
15+

tests/ui-toml/impl_trait_in_params/true/clippy.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ui-toml/impl_trait_in_params/true/impl_trait_in_params.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/ui/impl_trait_in_params.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ fn d(_: impl AnotherTrait<u32>) {}
1919

2020
//------ IMPLS
2121

22-
trait T {
23-
// See test in ui-toml for a case where avoid-breaking-exported-api is set to true
22+
pub trait Public {
23+
// See test in ui-toml for a case where avoid-breaking-exported-api is set to false
24+
fn t(_: impl Trait);
25+
fn tt<T: Trait>(_: T) {}
26+
}
27+
28+
trait Private {
29+
// This shouldn't lint
2430
fn t(_: impl Trait);
2531
fn tt<T: Trait>(_: T) {}
2632
}
@@ -34,7 +40,7 @@ impl S {
3440
}
3541

3642
// Trying with traits
37-
impl T for S {
43+
impl Public for S {
3844
fn t(_: impl Trait) {}
3945
}
4046

tests/ui/impl_trait_in_params.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | pub fn c<C: Trait, { /* Generic name */ }: Trait>(_: C, _: impl Trait) {}
2323
| +++++++++++++++++++++++++++++++
2424

2525
error: `impl Trait` used as a function parameter
26-
--> $DIR/impl_trait_in_params.rs:30:17
26+
--> $DIR/impl_trait_in_params.rs:36:17
2727
|
2828
LL | pub fn h(_: impl Trait) {}
2929
| ^^^^^^^^^^
@@ -34,7 +34,7 @@ LL | pub fn h<{ /* Generic name */ }: Trait>(_: impl Trait) {}
3434
| +++++++++++++++++++++++++++++++
3535

3636
error: `impl Trait` used as a function parameter
37-
--> $DIR/impl_trait_in_params.rs:33:45
37+
--> $DIR/impl_trait_in_params.rs:39:45
3838
|
3939
LL | pub fn k<K: AnotherTrait<u32>>(_: K, _: impl AnotherTrait<u32>) {}
4040
| ^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)