From e86e810889481d6f4284e6ec29224ed5fca490bd Mon Sep 17 00:00:00 2001 From: kraktus Date: Mon, 24 Oct 2022 18:25:59 +0200 Subject: [PATCH] [`use_self`] fix FP when trait impl defined in macro Found when working on `lintcheck --fix` --- clippy_lints/src/use_self.rs | 1 + tests/ui/use_self_trait.fixed | 26 ++++++++++++++++++++++++++ tests/ui/use_self_trait.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 65f1b546208..acf27c34e94 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -103,6 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { if parameters.as_ref().map_or(true, |params| { !params.parenthesized && !params.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_))) }); + if !item.span.from_expansion(); if !is_from_proc_macro(cx, item); // expensive, should be last check then { StackItem::Check { diff --git a/tests/ui/use_self_trait.fixed b/tests/ui/use_self_trait.fixed index 9bcd692fb35..12adf1e41a6 100644 --- a/tests/ui/use_self_trait.fixed +++ b/tests/ui/use_self_trait.fixed @@ -112,4 +112,30 @@ impl NameTrait for u8 { } } +mod impl_in_macro { + macro_rules! parse_ip_impl { + // minimized from serde=1.0.118 + ($ty:ty) => { + impl FooTrait for $ty { + fn new() -> Self { + <$ty>::bar() + } + } + }; + } + + struct Foo; + + trait FooTrait { + fn new() -> Self; + } + + impl Foo { + fn bar() -> Self { + Self + } + } + parse_ip_impl!(Foo); // Should not lint +} + fn main() {} diff --git a/tests/ui/use_self_trait.rs b/tests/ui/use_self_trait.rs index de305d40f33..49dbcddc1d8 100644 --- a/tests/ui/use_self_trait.rs +++ b/tests/ui/use_self_trait.rs @@ -112,4 +112,30 @@ impl NameTrait for u8 { } } +mod impl_in_macro { + macro_rules! parse_ip_impl { + // minimized from serde=1.0.118 + ($ty:ty) => { + impl FooTrait for $ty { + fn new() -> Self { + <$ty>::bar() + } + } + }; + } + + struct Foo; + + trait FooTrait { + fn new() -> Self; + } + + impl Foo { + fn bar() -> Self { + Self + } + } + parse_ip_impl!(Foo); // Should not lint +} + fn main() {}