Skip to content

Commit

Permalink
Fix tests, only lint for public tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Oct 8, 2023
1 parent 5ed338d commit 7755737
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 19 deletions.
10 changes: 7 additions & 3 deletions clippy_lints/src/functions/impl_trait_in_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/impl_trait_in_params/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
avoid-breaking-exported-api = false
16 changes: 16 additions & 0 deletions tests/ui-toml/impl_trait_in_params/impl_trait_in_params.rs
Original file line number Diff line number Diff line change
@@ -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: Trait>(_: T);
}

pub trait Public {
fn t(_: impl Trait); //~ ERROR: `impl Trait` used as a function parameter
fn tt<T: Trait>(_: T);
}
15 changes: 15 additions & 0 deletions tests/ui-toml/impl_trait_in_params/impl_trait_in_params.stderr
Original file line number Diff line number Diff line change
@@ -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

1 change: 0 additions & 1 deletion tests/ui-toml/impl_trait_in_params/true/clippy.toml

This file was deleted.

10 changes: 0 additions & 10 deletions tests/ui-toml/impl_trait_in_params/true/impl_trait_in_params.rs

This file was deleted.

12 changes: 9 additions & 3 deletions tests/ui/impl_trait_in_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ fn d(_: impl AnotherTrait<u32>) {}

//------ 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>(_: T) {}
}

trait Private {
// This shouldn't lint
fn t(_: impl Trait);
fn tt<T: Trait>(_: T) {}
}
Expand All @@ -34,7 +40,7 @@ impl S {
}

// Trying with traits
impl T for S {
impl Public for S {
fn t(_: impl Trait) {}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl_trait_in_params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL | pub fn c<C: Trait, { /* Generic name */ }: Trait>(_: 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) {}
| ^^^^^^^^^^
Expand All @@ -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: AnotherTrait<u32>>(_: K, _: impl AnotherTrait<u32>) {}
| ^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 7755737

Please sign in to comment.