Skip to content

Commit a95286b

Browse files
committed
Auto merge of rust-lang#10189 - Alexendoo:copy-packed-struct, r=giraffate
expl_impl_clone_on_copy: ignore packed structs with type/const params changelog: [`expl_impl_clone_on_copy`]: Ignore `#[repr(packed)]` structs with type or const paramaters Fixes rust-lang#10188 A more involved solution that checks if any bound on the trait impl aren't present on the struct definition would be ideal, but I couldn't see a nice way to go about that
2 parents 15226f9 + 34024ad commit a95286b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clippy_lints/src/derive.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::hir::nested_filter;
1515
use rustc_middle::traits::Reveal;
1616
use rustc_middle::ty::{
17-
self, Binder, BoundConstness, Clause, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind, TraitPredicate,
18-
Ty, TyCtxt,
17+
self, Binder, BoundConstness, Clause, GenericArgKind, GenericParamDefKind, ImplPolarity, ParamEnv, PredicateKind,
18+
TraitPredicate, Ty, TyCtxt,
1919
};
2020
use rustc_session::{declare_lint_pass, declare_tool_lint};
2121
use rustc_span::source_map::Span;
@@ -366,6 +366,15 @@ fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &h
366366
if ty_subs.types().any(|ty| !implements_trait(cx, ty, clone_id, &[])) {
367367
return;
368368
}
369+
// `#[repr(packed)]` structs with type/const parameters can't derive `Clone`.
370+
// https://github.com/rust-lang/rust-clippy/issues/10188
371+
if ty_adt.repr().packed()
372+
&& ty_subs
373+
.iter()
374+
.any(|arg| matches!(arg.unpack(), GenericArgKind::Type(_) | GenericArgKind::Const(_)))
375+
{
376+
return;
377+
}
369378

370379
span_lint_and_note(
371380
cx,

tests/ui/derive.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,15 @@ impl<T: Clone, U> Clone for GenericRef<'_, T, U> {
8585
}
8686
}
8787

88+
// https://github.com/rust-lang/rust-clippy/issues/10188
89+
#[repr(packed)]
90+
#[derive(Copy)]
91+
struct Packed<T>(T);
92+
93+
impl<T: Copy> Clone for Packed<T> {
94+
fn clone(&self) -> Self {
95+
*self
96+
}
97+
}
98+
8899
fn main() {}

0 commit comments

Comments
 (0)