Skip to content

Commit 65ae516

Browse files
committed
fix clippy
1 parent de8f4ac commit 65ae516

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ impl<'hir> Map<'hir> {
336336
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
337337
///
338338
/// Panics if `LocalDefId` does not have an associated body.
339-
pub fn body_owner_kind(self, def_id: LocalDefId) -> BodyOwnerKind {
339+
pub fn body_owner_kind(self, def_id: impl Into<DefId>) -> BodyOwnerKind {
340+
let def_id = def_id.into();
340341
match self.tcx.def_kind(def_id) {
341342
DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
342343
BodyOwnerKind::Const { inline: false }
@@ -356,20 +357,17 @@ impl<'hir> Map<'hir> {
356357
/// This should only be used for determining the context of a body, a return
357358
/// value of `Some` does not always suggest that the owner of the body is `const`,
358359
/// just that it has to be checked as if it were.
359-
pub fn body_const_context(self, def_id: LocalDefId) -> Option<ConstContext> {
360+
pub fn body_const_context(self, def_id: impl Into<DefId>) -> Option<ConstContext> {
361+
let def_id = def_id.into();
360362
let ccx = match self.body_owner_kind(def_id) {
361363
BodyOwnerKind::Const { inline } => ConstContext::Const { inline },
362364
BodyOwnerKind::Static(mt) => ConstContext::Static(mt),
363365

364-
BodyOwnerKind::Fn if self.tcx.is_constructor(def_id.to_def_id()) => return None,
365-
BodyOwnerKind::Fn | BodyOwnerKind::Closure
366-
if self.tcx.is_const_fn_raw(def_id.to_def_id()) =>
367-
{
368-
ConstContext::ConstFn
369-
}
370-
BodyOwnerKind::Fn if self.tcx.is_const_default_method(def_id.to_def_id()) => {
366+
BodyOwnerKind::Fn if self.tcx.is_constructor(def_id) => return None,
367+
BodyOwnerKind::Fn | BodyOwnerKind::Closure if self.tcx.is_const_fn_raw(def_id) => {
371368
ConstContext::ConstFn
372369
}
370+
BodyOwnerKind::Fn if self.tcx.is_const_default_method(def_id) => ConstContext::ConstFn,
373371
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
374372
};
375373

compiler/rustc_middle/src/ty/util.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,8 @@ impl<'tcx> TyCtxt<'tcx> {
782782
|| self.extern_crate(key.as_def_id()).is_some_and(|e| e.is_direct())
783783
}
784784

785-
pub fn expected_host_effect_param_for_body(self, def_id: LocalDefId) -> ty::Const<'tcx> {
785+
pub fn expected_host_effect_param_for_body(self, def_id: impl Into<DefId>) -> ty::Const<'tcx> {
786+
let def_id = def_id.into();
786787
// FIXME(effects): This is suspicious and should probably not be done,
787788
// especially now that we enforce host effects and then properly handle
788789
// effect vars during fallback.

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, &[])
453+
&& !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, adt.did(),&[])
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, &[]))
458+
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, adt.did(), &[]))
459459
{
460460
span_lint_and_sugg(
461461
cx,

src/tools/clippy/clippy_lints/src/implied_bounds_in_impls.rs

+4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ fn is_same_generics<'tcx>(
194194
.enumerate()
195195
.skip(1) // skip `Self` implicit arg
196196
.all(|(arg_index, arg)| {
197+
if [implied_by_generics.host_effect_index, implied_generics.host_effect_index].contains(&Some(arg_index)) {
198+
// skip host effect params in determining whether generics are same
199+
return true;
200+
}
197201
if let Some(ty) = arg.as_type() {
198202
if let &ty::Param(ty::ParamTy { index, .. }) = ty.kind()
199203
// `index == 0` means that it's referring to `Self`,

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, &[])
121+
&& implements_trait_with_env(cx.tcx, param_env, req_self_ty, trait_id, 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_lints/src/needless_pass_by_value.rs

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
186186
cx.param_env,
187187
ty,
188188
t,
189+
None,
189190
[Option::<ty::GenericArg<'tcx>>::None],
190191
)
191192
})

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

+23-11
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ pub fn implements_trait<'tcx>(
214214
trait_id: DefId,
215215
args: &[GenericArg<'tcx>],
216216
) -> bool {
217-
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, trait_id, args.iter().map(|&x| Some(x)))
217+
let callee_id = cx.enclosing_body.map(|body| cx.tcx.hir().body_owner(body).owner.to_def_id());
218+
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, trait_id, callee_id, args.iter().map(|&x| Some(x)))
218219
}
219220

220221
/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
@@ -223,9 +224,10 @@ pub fn implements_trait_with_env<'tcx>(
223224
param_env: ParamEnv<'tcx>,
224225
ty: Ty<'tcx>,
225226
trait_id: DefId,
227+
callee_id: DefId,
226228
args: &[GenericArg<'tcx>],
227229
) -> bool {
228-
implements_trait_with_env_from_iter(tcx, param_env, ty, trait_id, args.iter().map(|&x| Some(x)))
230+
implements_trait_with_env_from_iter(tcx, param_env, ty, trait_id, Some(callee_id), args.iter().map(|&x| Some(x)))
229231
}
230232

231233
/// Same as `implements_trait_from_env` but takes the arguments as an iterator.
@@ -234,6 +236,7 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
234236
param_env: ParamEnv<'tcx>,
235237
ty: Ty<'tcx>,
236238
trait_id: DefId,
239+
callee_id: Option<DefId>,
237240
args: impl IntoIterator<Item = impl Into<Option<GenericArg<'tcx>>>>,
238241
) -> bool {
239242
// Clippy shouldn't have infer types
@@ -245,20 +248,29 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
245248
}
246249

247250
let infcx = tcx.infer_ctxt().build();
251+
let args = args.into_iter().map(|arg| {
252+
arg.into().unwrap_or_else(|| {
253+
let orig = TypeVariableOrigin {
254+
kind: TypeVariableOriginKind::MiscVariable,
255+
span: DUMMY_SP,
256+
};
257+
infcx.next_ty_var(orig).into()
258+
})
259+
}).collect::<Vec<_>>();
260+
261+
// If an effect arg was not specified, we need to specify it.
262+
let effect_arg = if tcx.generics_of(trait_id).host_effect_index.is_some_and(|x| args.get(x - 1).is_none()) {
263+
Some(GenericArg::from(callee_id.map(|def_id| tcx.expected_host_effect_param_for_body(def_id)).unwrap_or(tcx.consts.true_)))
264+
} else {
265+
None
266+
};
267+
248268
let trait_ref = TraitRef::new(
249269
tcx,
250270
trait_id,
251271
Some(GenericArg::from(ty))
252272
.into_iter()
253-
.chain(args.into_iter().map(|arg| {
254-
arg.into().unwrap_or_else(|| {
255-
let orig = TypeVariableOrigin {
256-
kind: TypeVariableOriginKind::MiscVariable,
257-
span: DUMMY_SP,
258-
};
259-
infcx.next_ty_var(orig).into()
260-
})
261-
})),
273+
.chain(args).chain(effect_arg),
262274
);
263275

264276
debug_assert_matches!(

0 commit comments

Comments
 (0)