Skip to content

Commit c53cea9

Browse files
committed
fix: let non_canonical_impls skip proc marco
1 parent 05c4053 commit c53cea9

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

clippy_lints/src/non_canonical_impls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ impl LateLintPass<'_> for NonCanonicalImpls {
128128
let ExprKind::Block(block, ..) = body.value.kind else {
129129
return;
130130
};
131+
if let Some(expr) = block.expr
132+
&& expr.span.from_expansion()
133+
{
134+
return;
135+
}
131136

132137
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl.def_id)
133138
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)

tests/ui/auxiliary/proc_macro_derive.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ pub fn derive_ignored_unit_pattern(_: TokenStream) -> TokenStream {
169169
}
170170
}
171171
}
172+
173+
#[proc_macro_derive(NonCanonicalClone)]
174+
pub fn non_canonical_clone_derive(_: TokenStream) -> TokenStream {
175+
quote! {
176+
struct NonCanonicalClone;
177+
impl Clone for NonCanonicalClone {
178+
fn clone(&self) -> Self {
179+
let a = *self;
180+
a
181+
}
182+
}
183+
impl Copy for NonCanonicalClone {}
184+
}
185+
}

tests/ui/non_canonical_clone_impl.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macro_derive.rs
12
#![allow(clippy::clone_on_copy, unused)]
23
#![allow(clippy::assigning_clones)]
34
#![no_main]
@@ -95,3 +96,7 @@ impl<A: Copy> Clone for Uwu<A> {
9596
}
9697

9798
impl<A: std::fmt::Debug + Copy + Clone> Copy for Uwu<A> {}
99+
100+
// should skip proc macros, see https://github.com/rust-lang/rust-clippy/issues/12788
101+
#[derive(proc_macro_derive::NonCanonicalClone)]
102+
pub struct G;

tests/ui/non_canonical_clone_impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:proc_macro_derive.rs
12
#![allow(clippy::clone_on_copy, unused)]
23
#![allow(clippy::assigning_clones)]
34
#![no_main]
@@ -105,3 +106,7 @@ impl<A: Copy> Clone for Uwu<A> {
105106
}
106107

107108
impl<A: std::fmt::Debug + Copy + Clone> Copy for Uwu<A> {}
109+
110+
// should skip proc macros, see https://github.com/rust-lang/rust-clippy/issues/12788
111+
#[derive(proc_macro_derive::NonCanonicalClone)]
112+
pub struct G;

tests/ui/non_canonical_clone_impl.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: non-canonical implementation of `clone` on a `Copy` type
2-
--> tests/ui/non_canonical_clone_impl.rs:10:29
2+
--> tests/ui/non_canonical_clone_impl.rs:11:29
33
|
44
LL | fn clone(&self) -> Self {
55
| _____________________________^
@@ -11,7 +11,7 @@ LL | | }
1111
= help: to override `-D warnings` add `#[allow(clippy::non_canonical_clone_impl)]`
1212

1313
error: unnecessary implementation of `clone_from` on a `Copy` type
14-
--> tests/ui/non_canonical_clone_impl.rs:14:5
14+
--> tests/ui/non_canonical_clone_impl.rs:15:5
1515
|
1616
LL | / fn clone_from(&mut self, source: &Self) {
1717
LL | | source.clone();
@@ -20,7 +20,7 @@ LL | | }
2020
| |_____^ help: remove it
2121

2222
error: non-canonical implementation of `clone` on a `Copy` type
23-
--> tests/ui/non_canonical_clone_impl.rs:81:29
23+
--> tests/ui/non_canonical_clone_impl.rs:82:29
2424
|
2525
LL | fn clone(&self) -> Self {
2626
| _____________________________^
@@ -29,7 +29,7 @@ LL | | }
2929
| |_____^ help: change this to: `{ *self }`
3030

3131
error: unnecessary implementation of `clone_from` on a `Copy` type
32-
--> tests/ui/non_canonical_clone_impl.rs:85:5
32+
--> tests/ui/non_canonical_clone_impl.rs:86:5
3333
|
3434
LL | / fn clone_from(&mut self, source: &Self) {
3535
LL | | source.clone();

0 commit comments

Comments
 (0)