Skip to content

Commit 65409c4

Browse files
committed
Ensure callee_ids are body owners
1 parent 1ead476 commit 65409c4

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

src/tools/clippy/clippy_lints/src/derive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
450450
&& let Some(def_id) = trait_ref.trait_def_id()
451451
&& cx.tcx.is_diagnostic_item(sym::PartialEq, def_id)
452452
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
453-
&& !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, adt.did(),&[])
453+
&& !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[])
454454
// If all of our fields implement `Eq`, we can implement `Eq` too
455455
&& adt
456456
.all_fields()
457457
.map(|f| f.ty(cx.tcx, args))
458-
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, adt.did(), &[]))
458+
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]))
459459
{
460460
span_lint_and_sugg(
461461
cx,

src/tools/clippy/clippy_lints/src/loops/explicit_iter_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn is_ref_iterable<'tcx>(
118118
.liberate_late_bound_regions(fn_id, cx.tcx.fn_sig(fn_id).skip_binder())
119119
&& let &[req_self_ty, req_res_ty] = &**sig.inputs_and_output
120120
&& let param_env = cx.tcx.param_env(fn_id)
121-
&& implements_trait_with_env(cx.tcx, param_env, req_self_ty, trait_id, fn_id, &[])
121+
&& implements_trait_with_env(cx.tcx, param_env, req_self_ty, trait_id, Some(fn_id), &[])
122122
&& let Some(into_iter_ty) =
123123
make_normalized_projection_with_regions(cx.tcx, param_env, trait_id, sym!(IntoIter), [req_self_ty])
124124
&& let req_res_ty = normalize_with_regions(cx.tcx, param_env, req_res_ty)

src/tools/clippy/clippy_utils/src/ty.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -214,36 +214,21 @@ pub fn implements_trait<'tcx>(
214214
trait_id: DefId,
215215
args: &[GenericArg<'tcx>],
216216
) -> bool {
217-
let callee_id = cx
218-
.enclosing_body
219-
.map(|body| cx.tcx.hir().body_owner(body).owner.to_def_id());
220-
implements_trait_with_env_from_iter(
221-
cx.tcx,
222-
cx.param_env,
223-
ty,
224-
trait_id,
225-
callee_id,
226-
args.iter().map(|&x| Some(x)),
227-
)
217+
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, trait_id, None, args.iter().map(|&x| Some(x)))
228218
}
229219

230220
/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
221+
///
222+
/// The `callee_id` argument is used to determine the "effect arg", if one is needed.
231223
pub fn implements_trait_with_env<'tcx>(
232224
tcx: TyCtxt<'tcx>,
233225
param_env: ParamEnv<'tcx>,
234226
ty: Ty<'tcx>,
235227
trait_id: DefId,
236-
callee_id: DefId,
228+
callee_id: Option<DefId>,
237229
args: &[GenericArg<'tcx>],
238230
) -> bool {
239-
implements_trait_with_env_from_iter(
240-
tcx,
241-
param_env,
242-
ty,
243-
trait_id,
244-
Some(callee_id),
245-
args.iter().map(|&x| Some(x)),
246-
)
231+
implements_trait_with_env_from_iter(tcx, param_env, ty, trait_id, callee_id, args.iter().map(|&x| Some(x)))
247232
}
248233

249234
/// Same as `implements_trait_from_env` but takes the arguments as an iterator.
@@ -258,6 +243,11 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
258243
// Clippy shouldn't have infer types
259244
assert!(!ty.has_infer());
260245

246+
// If a `callee_id` is passed, then "assert" it is a body owner.
247+
if let Some(callee_id) = callee_id {
248+
let _ = tcx.hir().body_owner_kind(callee_id);
249+
}
250+
261251
let ty = tcx.erase_regions(ty);
262252
if ty.has_escaping_bound_vars() {
263253
return false;

0 commit comments

Comments
 (0)