Skip to content

Commit

Permalink
Don't lint multiple_inherent_impl with generic arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Apr 15, 2021
1 parent b1c675f commit 23ddf7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
24 changes: 18 additions & 6 deletions clippy_lints/src/inherent_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::in_macro;
use rustc_hir::def_id::DefIdMap;
use rustc_hir::{def_id, Crate, Impl, Item, ItemKind};
use rustc_hir::{def_id, Crate, Impl, Item, ItemKind, QPath, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;
Expand Down Expand Up @@ -53,15 +53,27 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
if let ItemKind::Impl(Impl {
ref generics,
of_trait: None,
self_ty,
..
}) = item.kind
{
// Remember for each inherent implementation encountered its span and generics
// but filter out implementations that have generic params (type or lifetime)
// or are derived from a macro
if !in_macro(item.span) && generics.params.is_empty() {
self.impls.insert(item.def_id.to_def_id(), item.span);
// Don't count impls from macros, or with generic parameters.
if in_macro(item.span) || !generics.params.is_empty() {
return;
}
match self_ty.kind {
TyKind::OpaqueDef(_, []) => (),
TyKind::Path(QPath::Resolved(None, path)) if path.segments.iter().all(|s| s.args.is_none()) => (),
TyKind::TraitObject(traits, ..)
if traits.iter().all(|t| {
t.bound_generic_params.is_empty() && t.trait_ref.path.segments.iter().all(|s| s.args.is_none())
}) => {},
// Don't count impls with generic arguments.
_ => return,
}

// Remember the span of each inherent implementation encountered
self.impls.insert(item.def_id.to_def_id(), item.span);
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/ui/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,21 @@ impl fmt::Debug for MyStruct {
}
}

// issue #5772
struct WithArgs<T>(T);
impl WithArgs<u32> {
fn f1() {}
}
impl WithArgs<u64> {
fn f2() {}
}

trait TraitWithArgs<T> {}
impl dyn TraitWithArgs<u32> {
fn f1(&self) {}
}
impl dyn TraitWithArgs<u64> {
fn f2(&self) {}
}

fn main() {}

0 comments on commit 23ddf7a

Please sign in to comment.