Skip to content

Commit a4da3f8

Browse files
committed
Fix clippy's missing substs
1 parent 472444b commit a4da3f8

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,12 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
466466
if let Some(def_id) = trait_ref.trait_def_id();
467467
if cx.tcx.is_diagnostic_item(sym::PartialEq, def_id);
468468
let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id);
469-
if !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, &[]);
469+
if !implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, []);
470470
// If all of our fields implement `Eq`, we can implement `Eq` too
471471
if adt
472472
.all_fields()
473473
.map(|f| f.ty(cx.tcx, substs))
474-
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, &[]));
474+
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, []));
475475
then {
476476
span_lint_and_sugg(
477477
cx,

src/tools/clippy/clippy_lints/src/eta_reduction.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,13 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
119119
let callee_ty_unadjusted = cx.typeck_results().expr_ty(callee).peel_refs();
120120
if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Arc);
121121
if !is_type_diagnostic_item(cx, callee_ty_unadjusted, sym::Rc);
122+
if let ty::Closure(_, substs) = *closure_ty.kind();
122123
then {
123124
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| {
124125
if let Some(mut snippet) = snippet_opt(cx, callee.span) {
125126
if let Some(fn_mut_id) = cx.tcx.lang_items().fn_mut_trait()
126-
&& implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &[])
127+
&& let args = cx.tcx.erase_late_bound_regions(ty::ClosureSubsts { substs }.sig()).inputs()
128+
&& implements_trait(cx, callee_ty.peel_refs(), fn_mut_id, &args.iter().copied().map(Into::into).collect::<Vec<_>>())
127129
&& path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr))
128130
{
129131
// Mutable closure is used after current expr; we cannot consume it.

src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ fn is_cow_into_owned(cx: &LateContext<'_>, method_name: Symbol, method_def_id: D
474474
}
475475

476476
/// Returns true if the named method is `ToString::to_string` and it's called on a type that
477-
/// is string-like i.e. implements `AsRef<str>` or `Deref<str>`.
477+
/// is string-like i.e. implements `AsRef<str>` or `Deref<Target = str>`.
478478
fn is_to_string_on_string_like<'a>(
479479
cx: &LateContext<'_>,
480480
call_expr: &'a Expr<'a>,
@@ -490,7 +490,7 @@ fn is_to_string_on_string_like<'a>(
490490
&& let GenericArgKind::Type(ty) = generic_arg.unpack()
491491
&& let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref)
492492
&& let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef)
493-
&& (implements_trait(cx, ty, deref_trait_id, &[cx.tcx.types.str_.into()]) ||
493+
&& (get_associated_type(cx, ty, deref_trait_id, "Target") == Some(cx.tcx.types.str_) ||
494494
implements_trait(cx, ty, as_ref_trait_id, &[cx.tcx.types.str_.into()])) {
495495
true
496496
} else {

src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
22
use clippy_utils::ptr::get_spans;
33
use clippy_utils::source::{snippet, snippet_opt};
4-
use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item};
4+
use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy, is_type_diagnostic_item, is_type_lang_item};
55
use clippy_utils::{get_trait_def_id, is_self, paths};
66
use if_chain::if_chain;
77
use rustc_ast::ast::Attribute;
@@ -185,7 +185,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
185185
if !ty.is_mutable_ptr();
186186
if !is_copy(cx, ty);
187187
if ty.is_sized(cx.tcx, cx.param_env);
188-
if !allowed_traits.iter().any(|&t| implements_trait(cx, ty, t, &[]));
188+
if !allowed_traits.iter().any(|&t| implements_trait_with_env(cx.tcx, cx.param_env, ty, t, [None]));
189189
if !implements_borrow_trait;
190190
if !all_borrowable_trait;
191191

src/tools/clippy/clippy_lints/src/neg_cmp_op_on_partial_ord.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for NoNegCompOpForPartialOrd {
6565

6666
let implements_partial_ord = {
6767
if let Some(id) = cx.tcx.lang_items().partial_ord_trait() {
68-
implements_trait(cx, ty, id, &[])
68+
implements_trait(cx, ty, id, &[ty.into()])
6969
} else {
7070
return;
7171
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir as hir;
99
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_hir::{Expr, FnDecl, LangItem, TyKind, Unsafety};
12-
use rustc_infer::infer::TyCtxtInferExt;
12+
use rustc_infer::infer::{TyCtxtInferExt, type_variable::{TypeVariableOrigin, TypeVariableOriginKind}};
1313
use rustc_lint::LateContext;
1414
use rustc_middle::mir::interpret::{ConstValue, Scalar};
1515
use rustc_middle::ty::{
@@ -18,7 +18,7 @@ use rustc_middle::ty::{
1818
};
1919
use rustc_middle::ty::{GenericArg, GenericArgKind};
2020
use rustc_span::symbol::Ident;
21-
use rustc_span::{sym, Span, Symbol};
21+
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
2222
use rustc_target::abi::{Size, VariantIdx};
2323
use rustc_trait_selection::infer::InferCtxtExt;
2424
use rustc_trait_selection::traits::query::normalize::AtExt;
@@ -153,7 +153,7 @@ pub fn implements_trait<'tcx>(
153153
trait_id: DefId,
154154
ty_params: &[GenericArg<'tcx>],
155155
) -> bool {
156-
implements_trait_with_env(cx.tcx, cx.param_env, ty, trait_id, ty_params)
156+
implements_trait_with_env(cx.tcx, cx.param_env, ty, trait_id, ty_params.iter().map(|&arg| Some(arg)))
157157
}
158158

159159
/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
@@ -162,7 +162,7 @@ pub fn implements_trait_with_env<'tcx>(
162162
param_env: ParamEnv<'tcx>,
163163
ty: Ty<'tcx>,
164164
trait_id: DefId,
165-
ty_params: &[GenericArg<'tcx>],
165+
ty_params: impl IntoIterator<Item = Option<GenericArg<'tcx>>>,
166166
) -> bool {
167167
// Clippy shouldn't have infer types
168168
assert!(!ty.needs_infer());
@@ -171,8 +171,12 @@ pub fn implements_trait_with_env<'tcx>(
171171
if ty.has_escaping_bound_vars() {
172172
return false;
173173
}
174-
let ty_params = tcx.mk_substs(ty_params.iter());
175174
let infcx = tcx.infer_ctxt().build();
175+
let orig = TypeVariableOrigin {
176+
kind: TypeVariableOriginKind::MiscVariable,
177+
span: DUMMY_SP,
178+
};
179+
let ty_params = tcx.mk_substs(ty_params.into_iter().map(|arg| arg.unwrap_or_else(|| infcx.next_ty_var(orig).into())));
176180
infcx
177181
.type_implements_trait(trait_id, ty, ty_params, param_env)
178182
.must_apply_modulo_regions()

0 commit comments

Comments
 (0)