Skip to content

Commit e8d5b1e

Browse files
borsflip1995
authored andcommitted
Auto merge of rust-lang#11130 - smoelius:fix-10535, r=Jarcho
Fix ICE in rust-lang#10535 Fixes rust-lang#10535 r? `@Jarcho` changelog: Eliminate ICE described in rust-lang#10535
1 parent c879eb5 commit e8d5b1e

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

src/tools/clippy/clippy_lints/src/dereference.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,8 @@ fn referent_used_exactly_once<'tcx>(
12961296
possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>,
12971297
reference: &Expr<'tcx>,
12981298
) -> bool {
1299-
let mir = enclosing_mir(cx.tcx, reference.hir_id);
1300-
if let Some(local) = expr_local(cx.tcx, reference)
1299+
if let Some(mir) = enclosing_mir(cx.tcx, reference.hir_id)
1300+
&& let Some(local) = expr_local(cx.tcx, reference)
13011301
&& let [location] = *local_assignments(mir, local).as_slice()
13021302
&& let Some(statement) = mir.basic_blocks[location.block].statements.get(location.statement_index)
13031303
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind

src/tools/clippy/clippy_utils/src/mir/mod.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,26 @@ pub fn used_exactly_once(mir: &rustc_middle::mir::Body<'_>, local: rustc_middle:
101101

102102
/// Returns the `mir::Body` containing the node associated with `hir_id`.
103103
#[allow(clippy::module_name_repetitions)]
104-
pub fn enclosing_mir(tcx: TyCtxt<'_>, hir_id: HirId) -> &Body<'_> {
104+
pub fn enclosing_mir(tcx: TyCtxt<'_>, hir_id: HirId) -> Option<&Body<'_>> {
105105
let body_owner_local_def_id = tcx.hir().enclosing_body_owner(hir_id);
106-
tcx.optimized_mir(body_owner_local_def_id.to_def_id())
106+
if tcx.hir().body_owner_kind(body_owner_local_def_id).is_fn_or_closure() {
107+
Some(tcx.optimized_mir(body_owner_local_def_id.to_def_id()))
108+
} else {
109+
None
110+
}
107111
}
108112

109113
/// Tries to determine the `Local` corresponding to `expr`, if any.
110114
/// This function is expensive and should be used sparingly.
111115
pub fn expr_local(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> Option<Local> {
112-
let mir = enclosing_mir(tcx, expr.hir_id);
113-
mir.local_decls.iter_enumerated().find_map(|(local, local_decl)| {
114-
if local_decl.source_info.span == expr.span {
115-
Some(local)
116-
} else {
117-
None
118-
}
116+
enclosing_mir(tcx, expr.hir_id).and_then(|mir| {
117+
mir.local_decls.iter_enumerated().find_map(|(local, local_decl)| {
118+
if local_decl.source_info.span == expr.span {
119+
Some(local)
120+
} else {
121+
None
122+
}
123+
})
119124
})
120125
}
121126

src/tools/clippy/tests/ui/needless_borrow.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,15 @@ mod issue_9782_method_variant {
492492
S.foo::<&[u8; 100]>(&a);
493493
}
494494
}
495+
496+
mod issue_10535 {
497+
static SOME_STATIC: String = String::new();
498+
499+
static UNIT: () = compute(&SOME_STATIC);
500+
501+
pub const fn compute<T>(_: T)
502+
where
503+
T: Copy,
504+
{
505+
}
506+
}

src/tools/clippy/tests/ui/needless_borrow.rs

+12
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,15 @@ mod issue_9782_method_variant {
492492
S.foo::<&[u8; 100]>(&a);
493493
}
494494
}
495+
496+
mod issue_10535 {
497+
static SOME_STATIC: String = String::new();
498+
499+
static UNIT: () = compute(&SOME_STATIC);
500+
501+
pub const fn compute<T>(_: T)
502+
where
503+
T: Copy,
504+
{
505+
}
506+
}

0 commit comments

Comments
 (0)