fix: let non_canonical_impls skip proc marco

This commit is contained in:
WeiTheShinobi 2024-05-26 17:54:44 +08:00
parent 05c4053628
commit c53cea90ad
5 changed files with 33 additions and 4 deletions

View file

@ -128,6 +128,11 @@ impl LateLintPass<'_> for NonCanonicalImpls {
let ExprKind::Block(block, ..) = body.value.kind else {
return;
};
if let Some(expr) = block.expr
&& expr.span.from_expansion()
{
return;
}
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl.def_id)
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)

View file

@ -169,3 +169,17 @@ pub fn derive_ignored_unit_pattern(_: TokenStream) -> TokenStream {
}
}
}
#[proc_macro_derive(NonCanonicalClone)]
pub fn non_canonical_clone_derive(_: TokenStream) -> TokenStream {
quote! {
struct NonCanonicalClone;
impl Clone for NonCanonicalClone {
fn clone(&self) -> Self {
let a = *self;
a
}
}
impl Copy for NonCanonicalClone {}
}
}

View file

@ -1,3 +1,4 @@
//@aux-build:proc_macro_derive.rs
#![allow(clippy::clone_on_copy, unused)]
#![allow(clippy::assigning_clones)]
#![no_main]
@ -95,3 +96,7 @@ impl<A: Copy> Clone for Uwu<A> {
}
impl<A: std::fmt::Debug + Copy + Clone> Copy for Uwu<A> {}
// should skip proc macros, see https://github.com/rust-lang/rust-clippy/issues/12788
#[derive(proc_macro_derive::NonCanonicalClone)]
pub struct G;

View file

@ -1,3 +1,4 @@
//@aux-build:proc_macro_derive.rs
#![allow(clippy::clone_on_copy, unused)]
#![allow(clippy::assigning_clones)]
#![no_main]
@ -105,3 +106,7 @@ impl<A: Copy> Clone for Uwu<A> {
}
impl<A: std::fmt::Debug + Copy + Clone> Copy for Uwu<A> {}
// should skip proc macros, see https://github.com/rust-lang/rust-clippy/issues/12788
#[derive(proc_macro_derive::NonCanonicalClone)]
pub struct G;

View file

@ -1,5 +1,5 @@
error: non-canonical implementation of `clone` on a `Copy` type
--> tests/ui/non_canonical_clone_impl.rs:10:29
--> tests/ui/non_canonical_clone_impl.rs:11:29
|
LL | fn clone(&self) -> Self {
| _____________________________^
@ -11,7 +11,7 @@ LL | | }
= help: to override `-D warnings` add `#[allow(clippy::non_canonical_clone_impl)]`
error: unnecessary implementation of `clone_from` on a `Copy` type
--> tests/ui/non_canonical_clone_impl.rs:14:5
--> tests/ui/non_canonical_clone_impl.rs:15:5
|
LL | / fn clone_from(&mut self, source: &Self) {
LL | | source.clone();
@ -20,7 +20,7 @@ LL | | }
| |_____^ help: remove it
error: non-canonical implementation of `clone` on a `Copy` type
--> tests/ui/non_canonical_clone_impl.rs:81:29
--> tests/ui/non_canonical_clone_impl.rs:82:29
|
LL | fn clone(&self) -> Self {
| _____________________________^
@ -29,7 +29,7 @@ LL | | }
| |_____^ help: change this to: `{ *self }`
error: unnecessary implementation of `clone_from` on a `Copy` type
--> tests/ui/non_canonical_clone_impl.rs:85:5
--> tests/ui/non_canonical_clone_impl.rs:86:5
|
LL | / fn clone_from(&mut self, source: &Self) {
LL | | source.clone();