Skip to content

Use placeref abstraction #10989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions clippy_lints/src/redundant_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ fn base_local_and_movability<'tcx>(
mir: &mir::Body<'tcx>,
place: mir::Place<'tcx>,
) -> (mir::Local, CannotMoveOut) {
use rustc_middle::mir::PlaceRef;

// Dereference. You cannot move things out from a borrowed value.
let mut deref = false;
// Accessing a field of an ADT that has `Drop`. Moving the field out will cause E0509.
Expand All @@ -330,17 +328,14 @@ fn base_local_and_movability<'tcx>(
// underlying type implements Copy
let mut slice = false;

let PlaceRef { local, mut projection } = place.as_ref();
while let [base @ .., elem] = projection {
projection = base;
for (base, elem) in place.as_ref().iter_projections() {
let base_ty = base.ty(&mir.local_decls, cx.tcx).ty;
deref |= matches!(elem, mir::ProjectionElem::Deref);
field |= matches!(elem, mir::ProjectionElem::Field(..))
&& has_drop(cx, mir::Place::ty_from(local, projection, &mir.local_decls, cx.tcx).ty);
slice |= matches!(elem, mir::ProjectionElem::Index(..))
&& !is_copy(cx, mir::Place::ty_from(local, projection, &mir.local_decls, cx.tcx).ty);
field |= matches!(elem, mir::ProjectionElem::Field(..)) && has_drop(cx, base_ty);
slice |= matches!(elem, mir::ProjectionElem::Index(..)) && !is_copy(cx, base_ty);
}

(local, deref || field || slice)
(place.local, deref || field || slice)
}

#[derive(Default)]
Expand Down
7 changes: 2 additions & 5 deletions clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,10 @@ fn check_operand<'tcx>(tcx: TyCtxt<'tcx>, operand: &Operand<'tcx>, span: Span, b
}

fn check_place<'tcx>(tcx: TyCtxt<'tcx>, place: Place<'tcx>, span: Span, body: &Body<'tcx>) -> McfResult {
let mut cursor = place.projection.as_ref();

while let [ref proj_base @ .., elem] = *cursor {
cursor = proj_base;
for (base, elem) in place.as_ref().iter_projections() {
match elem {
ProjectionElem::Field(..) => {
let base_ty = Place::ty_from(place.local, proj_base, body, tcx).ty;
let base_ty = base.ty(body, tcx).ty;
if let Some(def) = base_ty.ty_adt_def() {
// No union field accesses in `const fn`
if def.is_union() {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/missing_const_for_fn/cant_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,12 @@ impl Issue10617 {
self.0
}
}

union U {
f: u32,
}

// Do not lint because accessing union fields from const functions is unstable
fn h(u: U) -> u32 {
unsafe { u.f }
}