Skip to content

Commit ff0a96b

Browse files
authored
Rollup merge of rust-lang#134008 - jswrenn:unsafe-fields-copy, r=compiler-errors
Make `Copy` unsafe to implement for ADTs with `unsafe` fields As a rule, the application of `unsafe` to a declaration requires that use-sites of that declaration also entail `unsafe`. For example, a field declared `unsafe` may only be read in the lexical context of an `unsafe` block. For nearly all safe traits, the safety obligations of fields are explicitly discharged when they are mentioned in method definitions. For example, idiomatically implementing `Clone` (a safe trait) for a type with unsafe fields will require `unsafe` to clone those fields. Prior to this commit, `Copy` violated this rule. The trait is marked safe, and although it has no explicit methods, its implementation permits reads of `Self`. This commit resolves this by making `Copy` conditionally safe to implement. It remains safe to implement for ADTs without unsafe fields, but unsafe to implement for ADTs with unsafe fields. Tracking: rust-lang#132922 r? ```@compiler-errors```
2 parents 59392be + cfee37d commit ff0a96b

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

clippy_lints/src/needless_pass_by_value.rs

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
200200
cx.param_env,
201201
ty,
202202
traits::ObligationCause::dummy_with_span(span),
203+
rustc_hir::Safety::Safe,
203204
)
204205
.is_ok()
205206
{

0 commit comments

Comments
 (0)