Skip to content

Commit 6f16596

Browse files
committed
Auto merge of #9704 - kraktus:fix_use_self, r=giraffate
[`use_self`] fix FP when trait impl defined in macro changelog: [`use_self`] fix FP when trait impl defined in macro
2 parents 039af9c + e86e810 commit 6f16596

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

clippy_lints/src/use_self.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
103103
if parameters.as_ref().map_or(true, |params| {
104104
!params.parenthesized && !params.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)))
105105
});
106+
if !item.span.from_expansion();
106107
if !is_from_proc_macro(cx, item); // expensive, should be last check
107108
then {
108109
StackItem::Check {

tests/ui/use_self_trait.fixed

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,30 @@ impl NameTrait for u8 {
112112
}
113113
}
114114

115+
mod impl_in_macro {
116+
macro_rules! parse_ip_impl {
117+
// minimized from serde=1.0.118
118+
($ty:ty) => {
119+
impl FooTrait for $ty {
120+
fn new() -> Self {
121+
<$ty>::bar()
122+
}
123+
}
124+
};
125+
}
126+
127+
struct Foo;
128+
129+
trait FooTrait {
130+
fn new() -> Self;
131+
}
132+
133+
impl Foo {
134+
fn bar() -> Self {
135+
Self
136+
}
137+
}
138+
parse_ip_impl!(Foo); // Should not lint
139+
}
140+
115141
fn main() {}

tests/ui/use_self_trait.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,30 @@ impl NameTrait for u8 {
112112
}
113113
}
114114

115+
mod impl_in_macro {
116+
macro_rules! parse_ip_impl {
117+
// minimized from serde=1.0.118
118+
($ty:ty) => {
119+
impl FooTrait for $ty {
120+
fn new() -> Self {
121+
<$ty>::bar()
122+
}
123+
}
124+
};
125+
}
126+
127+
struct Foo;
128+
129+
trait FooTrait {
130+
fn new() -> Self;
131+
}
132+
133+
impl Foo {
134+
fn bar() -> Self {
135+
Self
136+
}
137+
}
138+
parse_ip_impl!(Foo); // Should not lint
139+
}
140+
115141
fn main() {}

0 commit comments

Comments
 (0)