Skip to content

Commit

Permalink
Update clippy for associated item changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper authored and camelid committed Jan 7, 2022
1 parent d7a6033 commit c34e3f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
12 changes: 7 additions & 5 deletions clippy_lints/src/len_zero.rs
Expand Up @@ -214,14 +214,14 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, trait_items
{
let mut current_and_super_traits = DefIdSet::default();
fill_trait_set(visited_trait.def_id.to_def_id(), &mut current_and_super_traits, cx);
let is_empty = sym!(is_empty);

let is_empty_method_found = current_and_super_traits
.iter()
.flat_map(|&i| cx.tcx.associated_items(i).in_definition_order())
.flat_map(|&i| cx.tcx.associated_items(i).filter_by_name_unhygienic(is_empty))
.any(|i| {
i.kind == ty::AssocKind::Fn
&& i.fn_has_self_parameter
&& i.ident.name == sym!(is_empty)
&& cx.tcx.fn_sig(i.def_id).inputs().skip_binder().len() == 1
});

Expand Down Expand Up @@ -458,7 +458,7 @@ fn is_empty_array(expr: &Expr<'_>) -> bool {
fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
/// Gets an `AssocItem` and return true if it matches `is_empty(self)`.
fn is_is_empty(cx: &LateContext<'_>, item: &ty::AssocItem) -> bool {
if item.kind == ty::AssocKind::Fn && item.ident.name.as_str() == "is_empty" {
if item.kind == ty::AssocKind::Fn {
let sig = cx.tcx.fn_sig(item.def_id);
let ty = sig.skip_binder();
ty.inputs().len() == 1
Expand All @@ -469,20 +469,22 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {

/// Checks the inherent impl's items for an `is_empty(self)` method.
fn has_is_empty_impl(cx: &LateContext<'_>, id: DefId) -> bool {
let is_empty = sym!(is_empty);
cx.tcx.inherent_impls(id).iter().any(|imp| {
cx.tcx
.associated_items(*imp)
.in_definition_order()
.filter_by_name_unhygienic(is_empty)
.any(|item| is_is_empty(cx, item))
})
}

let ty = &cx.typeck_results().expr_ty(expr).peel_refs();
match ty.kind() {
ty::Dynamic(tt, ..) => tt.principal().map_or(false, |principal| {
let is_empty = sym!(is_empty);
cx.tcx
.associated_items(principal.def_id())
.in_definition_order()
.filter_by_name_unhygienic(is_empty)
.any(|item| is_is_empty(cx, item))
}),
ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id),
Expand Down
11 changes: 6 additions & 5 deletions clippy_lints/src/non_copy_const.rs
Expand Up @@ -12,11 +12,10 @@ use rustc_hir::def_id::DefId;
use rustc_hir::{
BodyId, Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
};
use rustc_infer::traits::specialization_graph;
use rustc_lint::{LateContext, LateLintPass, Lint};
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
use rustc_middle::ty::adjustment::Adjust;
use rustc_middle::ty::{self, AssocKind, Const, Ty};
use rustc_middle::ty::{self, Const, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{InnerSpan, Span, DUMMY_SP};
use rustc_typeck::hir_ty_to_ty;
Expand Down Expand Up @@ -293,8 +292,10 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
// Lint a trait impl item only when the definition is a generic type,
// assuming an assoc const is not meant to be an interior mutable type.
if let Some(of_trait_def_id) = of_trait_ref.trait_def_id();
if let Some(of_assoc_item) = specialization_graph::Node::Trait(of_trait_def_id)
.item(cx.tcx, impl_item.ident, AssocKind::Const, of_trait_def_id);
if let Some(of_assoc_item) = cx
.tcx
.associated_item(impl_item.def_id)
.trait_item_def_id;
if cx
.tcx
.layout_of(cx.tcx.param_env(of_trait_def_id).and(
Expand All @@ -303,7 +304,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
// and, in that case, the definition is *not* generic.
cx.tcx.normalize_erasing_regions(
cx.tcx.param_env(of_trait_def_id),
cx.tcx.type_of(of_assoc_item.def_id),
cx.tcx.type_of(of_assoc_item),
),
))
.is_err();
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/use_self.rs
Expand Up @@ -13,7 +13,6 @@ use rustc_hir::{
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::hir::map::Map;
use rustc_middle::ty::AssocKind;
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;
Expand Down Expand Up @@ -143,10 +142,10 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
// trait, not in the impl of the trait.
let trait_method = cx
.tcx
.associated_items(impl_trait_ref.def_id)
.find_by_name_and_kind(cx.tcx, impl_item.ident, AssocKind::Fn, impl_trait_ref.def_id)
.associated_item(impl_item.def_id)
.trait_item_def_id
.expect("impl method matches a trait method");
let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
let trait_method_sig = cx.tcx.fn_sig(trait_method);
let trait_method_sig = cx.tcx.erase_late_bound_regions(trait_method_sig);

// `impl_inputs_outputs` is an iterator over the types (`hir::Ty`) declared in the
Expand Down

0 comments on commit c34e3f0

Please sign in to comment.