From 189eaa54c6e1d3b5a40fa82b8656eb3ac5ebb7a8 Mon Sep 17 00:00:00 2001 From: James Wang Date: Fri, 27 Sep 2019 20:46:55 -0500 Subject: [PATCH] Ignore impls derived from macros --- clippy_lints/src/inherent_impl.rs | 5 +++-- tests/ui/crashes/inherent_impl.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/ui/crashes/inherent_impl.rs diff --git a/clippy_lints/src/inherent_impl.rs b/clippy_lints/src/inherent_impl.rs index 0eaff14b7bd3..063aade4e7f8 100644 --- a/clippy_lints/src/inherent_impl.rs +++ b/clippy_lints/src/inherent_impl.rs @@ -1,6 +1,6 @@ //! lint on inherent implementations -use crate::utils::span_lint_and_then; +use crate::utils::{in_macro, span_lint_and_then}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, impl_lint_pass}; @@ -52,7 +52,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MultipleInherentImpl { if let ItemKind::Impl(_, _, _, ref generics, None, _, _) = item.kind { // Remember for each inherent implementation encoutered its span and generics // but filter out implementations that have generic params (type or lifetime) - if generics.params.len() == 0 { + // or are derived from a macro + if !in_macro(item.span) && generics.params.len() == 0 { self.impls.insert(item.hir_id.owner_def_id(), item.span); } } diff --git a/tests/ui/crashes/inherent_impl.rs b/tests/ui/crashes/inherent_impl.rs new file mode 100644 index 000000000000..aeb27b5ba8c2 --- /dev/null +++ b/tests/ui/crashes/inherent_impl.rs @@ -0,0 +1,26 @@ +#![deny(clippy::multiple_inherent_impl)] + +/// Test for https://github.com/rust-lang/rust-clippy/issues/4578 + +macro_rules! impl_foo { + ($struct:ident) => { + impl $struct { + fn foo() {} + } + }; +} + +macro_rules! impl_bar { + ($struct:ident) => { + impl $struct { + fn bar() {} + } + }; +} + +struct MyStruct; + +impl_foo!(MyStruct); +impl_bar!(MyStruct); + +fn main() {}