mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 15:11:30 +00:00
Improve AdtDef
interning.
This commit makes `AdtDef` use `Interned`. Much the commit is tedious changes to introduce getter functions. The interesting changes are in `compiler/rustc_middle/src/ty/adt.rs`.
This commit is contained in:
parent
a4d6c61bdc
commit
e110231260
42 changed files with 93 additions and 92 deletions
|
@ -149,7 +149,7 @@ impl LateLintPass<'_> for AwaitHolding {
|
||||||
fn check_interior_types(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
|
fn check_interior_types(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
|
||||||
for ty_cause in ty_causes {
|
for ty_cause in ty_causes {
|
||||||
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
|
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
|
||||||
if is_mutex_guard(cx, adt.did) {
|
if is_mutex_guard(cx, adt.did()) {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
AWAIT_HOLDING_LOCK,
|
AWAIT_HOLDING_LOCK,
|
||||||
|
@ -167,7 +167,7 @@ fn check_interior_types(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorType
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if is_refcell_ref(cx, adt.did) {
|
if is_refcell_ref(cx, adt.did()) {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
AWAIT_HOLDING_REFCELL_REF,
|
AWAIT_HOLDING_REFCELL_REF,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::LitKind;
|
use rustc_ast::ast::LitKind;
|
||||||
|
use rustc_data_structures::intern::Interned;
|
||||||
use rustc_hir::{Expr, ExprKind, PathSegment};
|
use rustc_hir::{Expr, ExprKind, PathSegment};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
|
@ -55,7 +56,7 @@ fn check_case_sensitive_file_extension_comparison(ctx: &LateContext<'_>, expr: &
|
||||||
ty::Str => {
|
ty::Str => {
|
||||||
return Some(span);
|
return Some(span);
|
||||||
},
|
},
|
||||||
ty::Adt(&ty::AdtDef { did, .. }, _) => {
|
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did, .. }, _)), _) => {
|
||||||
if ctx.tcx.is_diagnostic_item(sym::String, did) {
|
if ctx.tcx.is_diagnostic_item(sym::String, did) {
|
||||||
return Some(span);
|
return Some(span);
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,15 +116,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
|
||||||
&& let Res::Def(DefKind::Ctor(..), id) = cx.qpath_res(p, cast_expr.hir_id)
|
&& let Res::Def(DefKind::Ctor(..), id) = cx.qpath_res(p, cast_expr.hir_id)
|
||||||
{
|
{
|
||||||
let i = def.variant_index_with_ctor_id(id);
|
let i = def.variant_index_with_ctor_id(id);
|
||||||
let variant = &def.variants[i];
|
let variant = def.variant(i);
|
||||||
let nbits = utils::enum_value_nbits(get_discriminant_value(cx.tcx, def, i));
|
let nbits = utils::enum_value_nbits(get_discriminant_value(cx.tcx, *def, i));
|
||||||
(nbits, Some(variant))
|
(nbits, Some(variant))
|
||||||
} else {
|
} else {
|
||||||
(utils::enum_ty_to_nbits(def, cx.tcx), None)
|
(utils::enum_ty_to_nbits(*def, cx.tcx), None)
|
||||||
};
|
};
|
||||||
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
|
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
|
||||||
|
|
||||||
let cast_from_ptr_size = def.repr.int.map_or(true, |ty| {
|
let cast_from_ptr_size = def.repr().int.map_or(true, |ty| {
|
||||||
matches!(
|
matches!(
|
||||||
ty,
|
ty,
|
||||||
IntType::SignedInt(ast::IntTy::Isize) | IntType::UnsignedInt(ast::UintTy::Usize)
|
IntType::SignedInt(ast::IntTy::Isize) | IntType::UnsignedInt(ast::UintTy::Usize)
|
||||||
|
|
|
@ -34,10 +34,10 @@ pub(super) fn enum_value_nbits(value: EnumValue) -> u64 {
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn enum_ty_to_nbits(adt: &AdtDef, tcx: TyCtxt<'_>) -> u64 {
|
pub(super) fn enum_ty_to_nbits(adt: AdtDef<'_>, tcx: TyCtxt<'_>) -> u64 {
|
||||||
let mut explicit = 0i128;
|
let mut explicit = 0i128;
|
||||||
let (start, end) = adt
|
let (start, end) = adt
|
||||||
.variants
|
.variants()
|
||||||
.iter()
|
.iter()
|
||||||
.fold((0, i128::MIN), |(start, end), variant| match variant.discr {
|
.fold((0, i128::MIN), |(start, end), variant| match variant.discr {
|
||||||
VariantDiscr::Relative(x) => match explicit.checked_add(i128::from(x)) {
|
VariantDiscr::Relative(x) => match explicit.checked_add(i128::from(x)) {
|
||||||
|
|
|
@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
|
||||||
then {
|
then {
|
||||||
// TODO: Work out a way to put "whatever the imported way of referencing
|
// TODO: Work out a way to put "whatever the imported way of referencing
|
||||||
// this type in this file" rather than a fully-qualified type.
|
// this type in this file" rather than a fully-qualified type.
|
||||||
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
|
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did()));
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
DEFAULT_TRAIT_ACCESS,
|
DEFAULT_TRAIT_ACCESS,
|
||||||
|
@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
|
||||||
if let Some(adt) = binding_type.ty_adt_def();
|
if let Some(adt) = binding_type.ty_adt_def();
|
||||||
if adt.is_struct();
|
if adt.is_struct();
|
||||||
let variant = adt.non_enum_variant();
|
let variant = adt.non_enum_variant();
|
||||||
if adt.did.is_local() || !variant.is_field_list_non_exhaustive();
|
if adt.did().is_local() || !variant.is_field_list_non_exhaustive();
|
||||||
let module_did = cx.tcx.parent_module(stmt.hir_id).to_def_id();
|
let module_did = cx.tcx.parent_module(stmt.hir_id).to_def_id();
|
||||||
if variant
|
if variant
|
||||||
.fields
|
.fields
|
||||||
|
@ -216,7 +216,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
|
||||||
if let ty::Adt(adt_def, substs) = binding_type.kind();
|
if let ty::Adt(adt_def, substs) = binding_type.kind();
|
||||||
if !substs.is_empty();
|
if !substs.is_empty();
|
||||||
then {
|
then {
|
||||||
let adt_def_ty_name = cx.tcx.item_name(adt_def.did);
|
let adt_def_ty_name = cx.tcx.item_name(adt_def.did());
|
||||||
let generic_args = substs.iter().collect::<Vec<_>>();
|
let generic_args = substs.iter().collect::<Vec<_>>();
|
||||||
let tys_str = generic_args
|
let tys_str = generic_args
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -148,7 +148,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(adt_def) = ty.ty_adt_def();
|
if let Some(adt_def) = ty.ty_adt_def();
|
||||||
if adt_def.is_struct();
|
if adt_def.is_struct();
|
||||||
if let Some(variant) = adt_def.variants.iter().next();
|
if let Some(variant) = adt_def.variants().iter().next();
|
||||||
then {
|
then {
|
||||||
let fields_def = &variant.fields;
|
let fields_def = &variant.fields;
|
||||||
|
|
||||||
|
|
|
@ -103,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
if should_emit {
|
if should_emit {
|
||||||
let path_string = cx.tcx.def_path_str(adt_def.did);
|
let path_string = cx.tcx.def_path_str(adt_def.did());
|
||||||
span_lint_and_help(
|
span_lint_and_help(
|
||||||
cx,
|
cx,
|
||||||
DERIVABLE_IMPLS,
|
DERIVABLE_IMPLS,
|
||||||
|
|
|
@ -315,7 +315,7 @@ fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &T
|
||||||
let has_copy_impl = cx.tcx.all_local_trait_impls(()).get(©_id).map_or(false, |impls| {
|
let has_copy_impl = cx.tcx.all_local_trait_impls(()).get(©_id).map_or(false, |impls| {
|
||||||
impls
|
impls
|
||||||
.iter()
|
.iter()
|
||||||
.any(|&id| matches!(cx.tcx.type_of(id).kind(), ty::Adt(adt, _) if ty_adt.did == adt.did))
|
.any(|&id| matches!(cx.tcx.type_of(id).kind(), ty::Adt(adt, _) if ty_adt.did() == adt.did()))
|
||||||
});
|
});
|
||||||
if !has_copy_impl {
|
if !has_copy_impl {
|
||||||
return;
|
return;
|
||||||
|
@ -357,10 +357,10 @@ fn check_unsafe_derive_deserialize<'tcx>(
|
||||||
if let Some(trait_def_id) = trait_ref.trait_def_id();
|
if let Some(trait_def_id) = trait_ref.trait_def_id();
|
||||||
if match_def_path(cx, trait_def_id, &paths::SERDE_DESERIALIZE);
|
if match_def_path(cx, trait_def_id, &paths::SERDE_DESERIALIZE);
|
||||||
if let ty::Adt(def, _) = ty.kind();
|
if let ty::Adt(def, _) = ty.kind();
|
||||||
if let Some(local_def_id) = def.did.as_local();
|
if let Some(local_def_id) = def.did().as_local();
|
||||||
let adt_hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
|
let adt_hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
|
||||||
if !is_lint_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id);
|
if !is_lint_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id);
|
||||||
if cx.tcx.inherent_impls(def.did)
|
if cx.tcx.inherent_impls(def.did())
|
||||||
.iter()
|
.iter()
|
||||||
.map(|imp_did| cx.tcx.hir().expect_item(imp_did.expect_local()))
|
.map(|imp_did| cx.tcx.hir().expect_item(imp_did.expect_local()))
|
||||||
.any(|imp| has_unsafe(cx, imp));
|
.any(|imp| has_unsafe(cx, imp));
|
||||||
|
|
|
@ -52,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for EmptyEnum {
|
||||||
if let ItemKind::Enum(..) = item.kind {
|
if let ItemKind::Enum(..) = item.kind {
|
||||||
let ty = cx.tcx.type_of(item.def_id);
|
let ty = cx.tcx.type_of(item.def_id);
|
||||||
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
|
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
|
||||||
if adt.variants.is_empty() {
|
if adt.variants().is_empty() {
|
||||||
span_lint_and_help(
|
span_lint_and_help(
|
||||||
cx,
|
cx,
|
||||||
EMPTY_ENUM,
|
EMPTY_ENUM,
|
||||||
|
|
|
@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
|
||||||
if let Some(Constant::Int(val)) = constant.and_then(miri_to_const) {
|
if let Some(Constant::Int(val)) = constant.and_then(miri_to_const) {
|
||||||
if let ty::Adt(adt, _) = ty.kind() {
|
if let ty::Adt(adt, _) = ty.kind() {
|
||||||
if adt.is_enum() {
|
if adt.is_enum() {
|
||||||
ty = adt.repr.discr_type().to_ty(cx.tcx);
|
ty = adt.repr().discr_type().to_ty(cx.tcx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
|
|
|
@ -306,7 +306,7 @@ fn in_impl<'tcx>(
|
||||||
fn are_equal<'tcx>(cx: &LateContext<'tcx>, middle_ty: Ty<'_>, hir_ty: &rustc_hir::Ty<'_>) -> bool {
|
fn are_equal<'tcx>(cx: &LateContext<'tcx>, middle_ty: Ty<'_>, hir_ty: &rustc_hir::Ty<'_>) -> bool {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let ty::Adt(adt_def, _) = middle_ty.kind();
|
if let ty::Adt(adt_def, _) = middle_ty.kind();
|
||||||
if let Some(local_did) = adt_def.did.as_local();
|
if let Some(local_did) = adt_def.did().as_local();
|
||||||
let item = cx.tcx.hir().expect_item(local_did);
|
let item = cx.tcx.hir().expect_item(local_did);
|
||||||
let middle_ty_id = item.def_id.to_def_id();
|
let middle_ty_id = item.def_id.to_def_id();
|
||||||
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind;
|
if let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind;
|
||||||
|
|
|
@ -224,7 +224,7 @@ fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: DefId) -> String {
|
||||||
ty::ImplContainer(def_id) => {
|
ty::ImplContainer(def_id) => {
|
||||||
let ty = cx.tcx.type_of(def_id);
|
let ty = cx.tcx.type_of(def_id);
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(adt, _) => cx.tcx.def_path_str(adt.did),
|
ty::Adt(adt, _) => cx.tcx.def_path_str(adt.did()),
|
||||||
_ => ty.to_string(),
|
_ => ty.to_string(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -72,7 +72,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if format_args.format_string_parts == [kw::Empty];
|
if format_args.format_string_parts == [kw::Empty];
|
||||||
if match cx.typeck_results().expr_ty(value).peel_refs().kind() {
|
if match cx.typeck_results().expr_ty(value).peel_refs().kind() {
|
||||||
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(sym::String, adt.did),
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(sym::String, adt.did()),
|
||||||
ty::Str => true,
|
ty::Str => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
|
|
@ -189,8 +189,8 @@ fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &m
|
||||||
// primitive types are never mutable
|
// primitive types are never mutable
|
||||||
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
|
||||||
ty::Adt(adt, substs) => {
|
ty::Adt(adt, substs) => {
|
||||||
tys.insert(adt.did) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
|
tys.insert(adt.did()) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
|
||||||
|| KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did, path))
|
|| KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did(), path))
|
||||||
&& substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
|
&& substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
|
||||||
},
|
},
|
||||||
ty::Tuple(substs) => substs.iter().any(|ty| is_mutable_ty(cx, ty, span, tys)),
|
ty::Tuple(substs) => substs.iter().any(|ty| is_mutable_ty(cx, ty, span, tys)),
|
||||||
|
|
|
@ -71,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
|
||||||
let ty = cx.typeck_results().expr_ty(expr);
|
let ty = cx.typeck_results().expr_ty(expr);
|
||||||
if let Some(adt_def) = ty.ty_adt_def();
|
if let Some(adt_def) = ty.ty_adt_def();
|
||||||
if adt_def.is_struct();
|
if adt_def.is_struct();
|
||||||
if let Some(variant) = adt_def.variants.iter().next();
|
if let Some(variant) = adt_def.variants().iter().next();
|
||||||
if fields.iter().all(|f| f.is_shorthand);
|
if fields.iter().all(|f| f.is_shorthand);
|
||||||
then {
|
then {
|
||||||
let mut def_order_map = FxHashMap::default();
|
let mut def_order_map = FxHashMap::default();
|
||||||
|
|
|
@ -81,11 +81,11 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
|
||||||
if let ItemKind::Enum(ref def, _) = item.kind {
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
||||||
let ty = cx.tcx.type_of(item.def_id);
|
let ty = cx.tcx.type_of(item.def_id);
|
||||||
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
|
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
|
||||||
if adt.variants.len() <= 1 {
|
if adt.variants().len() <= 1 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let mut variants_size: Vec<VariantInfo> = Vec::new();
|
let mut variants_size: Vec<VariantInfo> = Vec::new();
|
||||||
for (i, variant) in adt.variants.iter().enumerate() {
|
for (i, variant) in adt.variants().iter().enumerate() {
|
||||||
let mut fields_size = Vec::new();
|
let mut fields_size = Vec::new();
|
||||||
for (i, f) in variant.fields.iter().enumerate() {
|
for (i, f) in variant.fields.iter().enumerate() {
|
||||||
let ty = cx.tcx.type_of(f.did);
|
let ty = cx.tcx.type_of(f.did);
|
||||||
|
|
|
@ -248,13 +248,13 @@ enum LenOutput<'tcx> {
|
||||||
fn parse_len_output<'tcx>(cx: &LateContext<'_>, sig: FnSig<'tcx>) -> Option<LenOutput<'tcx>> {
|
fn parse_len_output<'tcx>(cx: &LateContext<'_>, sig: FnSig<'tcx>) -> Option<LenOutput<'tcx>> {
|
||||||
match *sig.output().kind() {
|
match *sig.output().kind() {
|
||||||
ty::Int(_) | ty::Uint(_) => Some(LenOutput::Integral),
|
ty::Int(_) | ty::Uint(_) => Some(LenOutput::Integral),
|
||||||
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Option, adt.did) => {
|
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) => {
|
||||||
subs.type_at(0).is_integral().then(|| LenOutput::Option(adt.did))
|
subs.type_at(0).is_integral().then(|| LenOutput::Option(adt.did()))
|
||||||
},
|
},
|
||||||
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did) => subs
|
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => subs
|
||||||
.type_at(0)
|
.type_at(0)
|
||||||
.is_integral()
|
.is_integral()
|
||||||
.then(|| LenOutput::Result(adt.did, subs.type_at(1))),
|
.then(|| LenOutput::Result(adt.did(), subs.type_at(1))),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -263,8 +263,8 @@ impl LenOutput<'_> {
|
||||||
fn matches_is_empty_output(self, ty: Ty<'_>) -> bool {
|
fn matches_is_empty_output(self, ty: Ty<'_>) -> bool {
|
||||||
match (self, ty.kind()) {
|
match (self, ty.kind()) {
|
||||||
(_, &ty::Bool) => true,
|
(_, &ty::Bool) => true,
|
||||||
(Self::Option(id), &ty::Adt(adt, subs)) if id == adt.did => subs.type_at(0).is_bool(),
|
(Self::Option(id), &ty::Adt(adt, subs)) if id == adt.did() => subs.type_at(0).is_bool(),
|
||||||
(Self::Result(id, err_ty), &ty::Adt(adt, subs)) if id == adt.did => {
|
(Self::Result(id, err_ty), &ty::Adt(adt, subs)) if id == adt.did() => {
|
||||||
subs.type_at(0).is_bool() && subs.type_at(1) == err_ty
|
subs.type_at(0).is_bool() && subs.type_at(1) == err_ty
|
||||||
},
|
},
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -488,7 +488,7 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
||||||
.any(|item| is_is_empty(cx, item))
|
.any(|item| is_is_empty(cx, item))
|
||||||
}),
|
}),
|
||||||
ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id),
|
ty::Projection(ref proj) => has_is_empty_impl(cx, proj.item_def_id),
|
||||||
ty::Adt(id, _) => has_is_empty_impl(cx, id.did),
|
ty::Adt(id, _) => has_is_empty_impl(cx, id.did()),
|
||||||
ty::Array(..) | ty::Slice(..) | ty::Str => true,
|
ty::Array(..) | ty::Slice(..) | ty::Str => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
|
|
@ -334,7 +334,7 @@ struct Start<'hir> {
|
||||||
|
|
||||||
fn get_slice_like_element_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
fn get_slice_like_element_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Vec, adt.did) => Some(subs.type_at(0)),
|
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Vec, adt.did()) => Some(subs.type_at(0)),
|
||||||
ty::Ref(_, subty, _) => get_slice_like_element_ty(cx, *subty),
|
ty::Ref(_, subty, _) => get_slice_like_element_ty(cx, *subty),
|
||||||
ty::Slice(ty) | ty::Array(ty, _) => Some(*ty),
|
ty::Slice(ty) | ty::Array(ty, _) => Some(*ty),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
|
@ -45,8 +45,8 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
|
||||||
|
|
||||||
// Accumulate the variants which should be put in place of the wildcard because they're not
|
// Accumulate the variants which should be put in place of the wildcard because they're not
|
||||||
// already covered.
|
// already covered.
|
||||||
let has_hidden = adt_def.variants.iter().any(|x| is_hidden(cx, x));
|
let has_hidden = adt_def.variants().iter().any(|x| is_hidden(cx, x));
|
||||||
let mut missing_variants: Vec<_> = adt_def.variants.iter().filter(|x| !is_hidden(cx, x)).collect();
|
let mut missing_variants: Vec<_> = adt_def.variants().iter().filter(|x| !is_hidden(cx, x)).collect();
|
||||||
|
|
||||||
let mut path_prefix = CommonPrefixSearcher::None;
|
let mut path_prefix = CommonPrefixSearcher::None;
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
|
@ -118,7 +118,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
|
||||||
}
|
}
|
||||||
s
|
s
|
||||||
} else {
|
} else {
|
||||||
let mut s = cx.tcx.def_path_str(adt_def.did);
|
let mut s = cx.tcx.def_path_str(adt_def.did());
|
||||||
s.push_str("::");
|
s.push_str("::");
|
||||||
s
|
s
|
||||||
},
|
},
|
||||||
|
|
|
@ -145,7 +145,7 @@ pub(crate) trait BindInsteadOfMap {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(adt) = cx.typeck_results().expr_ty(recv).ty_adt_def();
|
if let Some(adt) = cx.typeck_results().expr_ty(recv).ty_adt_def();
|
||||||
if let Ok(vid) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM);
|
if let Ok(vid) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM);
|
||||||
if Some(adt.did) == cx.tcx.parent(vid);
|
if Some(adt.did()) == cx.tcx.parent(vid);
|
||||||
then {} else { return false; }
|
then {} else { return false; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span,
|
||||||
let inner_ty = match recv_ty.kind() {
|
let inner_ty = match recv_ty.kind() {
|
||||||
// `Option<T>` -> `T`
|
// `Option<T>` -> `T`
|
||||||
ty::Adt(adt, subst)
|
ty::Adt(adt, subst)
|
||||||
if cx.tcx.is_diagnostic_item(sym::Option, adt.did) && meets_msrv(msrv, &msrvs::OPTION_COPIED) =>
|
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && meets_msrv(msrv, &msrvs::OPTION_COPIED) =>
|
||||||
{
|
{
|
||||||
subst.type_at(0)
|
subst.type_at(0)
|
||||||
},
|
},
|
||||||
|
|
|
@ -119,9 +119,9 @@ pub(super) fn check<'tcx>(
|
||||||
if let PatKind::Binding(_, filter_param_id, _, None) = filter_pat.kind;
|
if let PatKind::Binding(_, filter_param_id, _, None) = filter_pat.kind;
|
||||||
if let ExprKind::MethodCall(path, [filter_arg], _) = filter_body.value.kind;
|
if let ExprKind::MethodCall(path, [filter_arg], _) = filter_body.value.kind;
|
||||||
if let Some(opt_ty) = cx.typeck_results().expr_ty(filter_arg).ty_adt_def();
|
if let Some(opt_ty) = cx.typeck_results().expr_ty(filter_arg).ty_adt_def();
|
||||||
if let Some(is_result) = if cx.tcx.is_diagnostic_item(sym::Option, opt_ty.did) {
|
if let Some(is_result) = if cx.tcx.is_diagnostic_item(sym::Option, opt_ty.did()) {
|
||||||
Some(false)
|
Some(false)
|
||||||
} else if cx.tcx.is_diagnostic_item(sym::Result, opt_ty.did) {
|
} else if cx.tcx.is_diagnostic_item(sym::Result, opt_ty.did()) {
|
||||||
Some(true)
|
Some(true)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
|
@ -17,7 +17,7 @@ pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv
|
||||||
let return_type = cx.typeck_results().expr_ty(expr);
|
let return_type = cx.typeck_results().expr_ty(expr);
|
||||||
let input_type = cx.typeck_results().expr_ty(recv);
|
let input_type = cx.typeck_results().expr_ty(recv);
|
||||||
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
|
let (input_type, ref_count) = peel_mid_ty_refs(input_type);
|
||||||
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
|
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()));
|
||||||
if return_type == input_type;
|
if return_type == input_type;
|
||||||
then {
|
then {
|
||||||
let mut app = Applicability::MachineApplicable;
|
let mut app = Applicability::MachineApplicable;
|
||||||
|
|
|
@ -60,7 +60,7 @@ fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let ty::Adt(adt, substs) = ty.kind() {
|
if let ty::Adt(adt, substs) = ty.kind() {
|
||||||
match_def_path(cx, adt.did, &paths::COW) && substs.type_at(1).is_str()
|
match_def_path(cx, adt.did(), &paths::COW) && substs.type_at(1).is_str()
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,9 +55,9 @@ pub(super) fn check<'tcx>(
|
||||||
// lint if caller of `.map().flatten()` is an Option or Result
|
// lint if caller of `.map().flatten()` is an Option or Result
|
||||||
let caller_type = match cx.typeck_results().expr_ty(recv).kind() {
|
let caller_type = match cx.typeck_results().expr_ty(recv).kind() {
|
||||||
ty::Adt(adt, _) => {
|
ty::Adt(adt, _) => {
|
||||||
if cx.tcx.is_diagnostic_item(sym::Option, adt.did) {
|
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) {
|
||||||
"Option"
|
"Option"
|
||||||
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did) {
|
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
|
||||||
"Result"
|
"Result"
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -152,7 +152,7 @@ fn parse_iter_usage<'tcx>(
|
||||||
return if_chain! {
|
return if_chain! {
|
||||||
if match_def_path(cx, did, &paths::ITERTOOLS_NEXT_TUPLE);
|
if match_def_path(cx, did, &paths::ITERTOOLS_NEXT_TUPLE);
|
||||||
if let ty::Adt(adt_def, subs) = cx.typeck_results().expr_ty(e).kind();
|
if let ty::Adt(adt_def, subs) = cx.typeck_results().expr_ty(e).kind();
|
||||||
if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did);
|
if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did());
|
||||||
if let ty::Tuple(subs) = subs.type_at(0).kind();
|
if let ty::Tuple(subs) = subs.type_at(0).kind();
|
||||||
if subs.len() == 2;
|
if subs.len() == 2;
|
||||||
then {
|
then {
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<
|
||||||
} else if !found_mapping && !mutates_arg {
|
} else if !found_mapping && !mutates_arg {
|
||||||
let in_ty = cx.typeck_results().node_type(body.params[0].hir_id);
|
let in_ty = cx.typeck_results().node_type(body.params[0].hir_id);
|
||||||
match cx.typeck_results().expr_ty(&body.value).kind() {
|
match cx.typeck_results().expr_ty(&body.value).kind() {
|
||||||
ty::Adt(adt, subst) if cx.tcx.is_diagnostic_item(sym::Option, adt.did) && in_ty == subst.type_at(0) => {
|
ty::Adt(adt, subst) if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && in_ty == subst.type_at(0) => {
|
||||||
"filter"
|
"filter"
|
||||||
},
|
},
|
||||||
_ => return,
|
_ => return,
|
||||||
|
|
|
@ -125,7 +125,7 @@ fn check_ty<'tcx>(cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) {
|
||||||
if let Adt(def, substs) = ty.kind() {
|
if let Adt(def, substs) = ty.kind() {
|
||||||
let is_keyed_type = [sym::HashMap, sym::BTreeMap, sym::HashSet, sym::BTreeSet]
|
let is_keyed_type = [sym::HashMap, sym::BTreeMap, sym::HashSet, sym::BTreeSet]
|
||||||
.iter()
|
.iter()
|
||||||
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did));
|
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did()));
|
||||||
if is_keyed_type && is_interior_mutable_type(cx, substs.type_at(0), span) {
|
if is_keyed_type && is_interior_mutable_type(cx, substs.type_at(0), span) {
|
||||||
span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
|
span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");
|
||||||
}
|
}
|
||||||
|
@ -159,8 +159,8 @@ fn is_interior_mutable_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Sp
|
||||||
sym::Arc,
|
sym::Arc,
|
||||||
]
|
]
|
||||||
.iter()
|
.iter()
|
||||||
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did));
|
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did()));
|
||||||
let is_box = Some(def.did) == cx.tcx.lang_items().owned_box();
|
let is_box = Some(def.did()) == cx.tcx.lang_items().owned_box();
|
||||||
if is_std_collection || is_box {
|
if is_std_collection || is_box {
|
||||||
// The type is mutable if any of its type parameters are
|
// The type is mutable if any of its type parameters are
|
||||||
substs.types().any(|ty| is_interior_mutable_type(cx, ty, span))
|
substs.types().any(|ty| is_interior_mutable_type(cx, ty, span))
|
||||||
|
|
|
@ -198,7 +198,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
|
||||||
// Dereference suggestion
|
// Dereference suggestion
|
||||||
let sugg = |diag: &mut Diagnostic| {
|
let sugg = |diag: &mut Diagnostic| {
|
||||||
if let ty::Adt(def, ..) = ty.kind() {
|
if let ty::Adt(def, ..) = ty.kind() {
|
||||||
if let Some(span) = cx.tcx.hir().span_if_local(def.did) {
|
if let Some(span) = cx.tcx.hir().span_if_local(def.did()) {
|
||||||
if can_type_implement_copy(cx.tcx, cx.param_env, ty, traits::ObligationCause::dummy_with_span(span)).is_ok() {
|
if can_type_implement_copy(cx.tcx, cx.param_env, ty, traits::ObligationCause::dummy_with_span(span)).is_ok() {
|
||||||
diag.span_help(span, "consider marking this type as `Copy`");
|
diag.span_help(span, "consider marking this type as `Copy`");
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate {
|
||||||
let ty = cx.typeck_results().expr_ty(expr);
|
let ty = cx.typeck_results().expr_ty(expr);
|
||||||
if let ty::Adt(def, _) = ty.kind() {
|
if let ty::Adt(def, _) = ty.kind() {
|
||||||
if fields.len() == def.non_enum_variant().fields.len()
|
if fields.len() == def.non_enum_variant().fields.len()
|
||||||
&& !def.variants[0_usize.into()].is_field_list_non_exhaustive()
|
&& !def.variant(0_usize.into()).is_field_list_non_exhaustive()
|
||||||
{
|
{
|
||||||
span_lint(
|
span_lint(
|
||||||
cx,
|
cx,
|
||||||
|
|
|
@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
|
||||||
let mut impls = HirIdSet::default();
|
let mut impls = HirIdSet::default();
|
||||||
cx.tcx.for_each_impl(default_trait_id, |d| {
|
cx.tcx.for_each_impl(default_trait_id, |d| {
|
||||||
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() {
|
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() {
|
||||||
if let Some(local_def_id) = ty_def.did.as_local() {
|
if let Some(local_def_id) = ty_def.did().as_local() {
|
||||||
impls.insert(cx.tcx.hir().local_def_id_to_hir_id(local_def_id));
|
impls.insert(cx.tcx.hir().local_def_id_to_hir_id(local_def_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(ref impling_types) = self.impling_types;
|
if let Some(ref impling_types) = self.impling_types;
|
||||||
if let Some(self_def) = cx.tcx.type_of(self_def_id).ty_adt_def();
|
if let Some(self_def) = cx.tcx.type_of(self_def_id).ty_adt_def();
|
||||||
if let Some(self_local_did) = self_def.did.as_local();
|
if let Some(self_local_did) = self_def.did().as_local();
|
||||||
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
|
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
|
||||||
if impling_types.contains(&self_id);
|
if impling_types.contains(&self_id);
|
||||||
then {
|
then {
|
||||||
|
|
|
@ -140,7 +140,7 @@ fn is_value_unfrozen_raw<'tcx>(
|
||||||
match val.ty().kind() {
|
match val.ty().kind() {
|
||||||
// the fact that we have to dig into every structs to search enums
|
// the fact that we have to dig into every structs to search enums
|
||||||
// leads us to the point checking `UnsafeCell` directly is the only option.
|
// leads us to the point checking `UnsafeCell` directly is the only option.
|
||||||
ty::Adt(ty_def, ..) if Some(ty_def.did) == cx.tcx.lang_items().unsafe_cell_type() => true,
|
ty::Adt(ty_def, ..) if Some(ty_def.did()) == cx.tcx.lang_items().unsafe_cell_type() => true,
|
||||||
ty::Array(..) | ty::Adt(..) | ty::Tuple(..) => {
|
ty::Array(..) | ty::Adt(..) | ty::Tuple(..) => {
|
||||||
let val = cx.tcx.destructure_const(cx.param_env.and(val));
|
let val = cx.tcx.destructure_const(cx.param_env.and(val));
|
||||||
val.fields.iter().any(|field| inner(cx, *field))
|
val.fields.iter().any(|field| inner(cx, *field))
|
||||||
|
|
|
@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
|
||||||
let mut non_send_fields = Vec::new();
|
let mut non_send_fields = Vec::new();
|
||||||
|
|
||||||
let hir_map = cx.tcx.hir();
|
let hir_map = cx.tcx.hir();
|
||||||
for variant in &adt_def.variants {
|
for variant in adt_def.variants() {
|
||||||
for field in &variant.fields {
|
for field in &variant.fields {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(field_hir_id) = field
|
if let Some(field_hir_id) = field
|
||||||
|
@ -233,7 +233,7 @@ fn contains_pointer_like<'tcx>(cx: &LateContext<'tcx>, target_ty: Ty<'tcx>) -> b
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
ty::Adt(adt_def, _) => {
|
ty::Adt(adt_def, _) => {
|
||||||
if match_def_path(cx, adt_def.did, &paths::PTR_NON_NULL) {
|
if match_def_path(cx, adt_def.did(), &paths::PTR_NON_NULL) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -405,13 +405,13 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
|
||||||
// Check that the name as typed matches the actual name of the type.
|
// Check that the name as typed matches the actual name of the type.
|
||||||
// e.g. `fn foo(_: &Foo)` shouldn't trigger the lint when `Foo` is an alias for `Vec`
|
// e.g. `fn foo(_: &Foo)` shouldn't trigger the lint when `Foo` is an alias for `Vec`
|
||||||
if let [.., name] = path.segments;
|
if let [.., name] = path.segments;
|
||||||
if cx.tcx.item_name(adt.did) == name.ident.name;
|
if cx.tcx.item_name(adt.did()) == name.ident.name;
|
||||||
|
|
||||||
if !is_lint_allowed(cx, PTR_ARG, hir_ty.hir_id);
|
if !is_lint_allowed(cx, PTR_ARG, hir_ty.hir_id);
|
||||||
if params.get(i).map_or(true, |p| !is_lint_allowed(cx, PTR_ARG, p.hir_id));
|
if params.get(i).map_or(true, |p| !is_lint_allowed(cx, PTR_ARG, p.hir_id));
|
||||||
|
|
||||||
then {
|
then {
|
||||||
let (method_renames, deref_ty, deref_impl_id) = match cx.tcx.get_diagnostic_name(adt.did) {
|
let (method_renames, deref_ty, deref_impl_id) = match cx.tcx.get_diagnostic_name(adt.did()) {
|
||||||
Some(sym::Vec) => (
|
Some(sym::Vec) => (
|
||||||
[("clone", ".to_owned()")].as_slice(),
|
[("clone", ".to_owned()")].as_slice(),
|
||||||
DerefTy::Slice(
|
DerefTy::Slice(
|
||||||
|
@ -462,7 +462,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
|
||||||
return Some(PtrArg {
|
return Some(PtrArg {
|
||||||
idx: i,
|
idx: i,
|
||||||
span: hir_ty.span,
|
span: hir_ty.span,
|
||||||
ty_did: adt.did,
|
ty_did: adt.did(),
|
||||||
ty_name: name.ident.name,
|
ty_name: name.ident.name,
|
||||||
method_renames,
|
method_renames,
|
||||||
ref_prefix: RefPrefix {
|
ref_prefix: RefPrefix {
|
||||||
|
@ -570,7 +570,7 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
|
||||||
.map(|sig| sig.input(i).skip_binder().peel_refs())
|
.map(|sig| sig.input(i).skip_binder().peel_refs())
|
||||||
.map_or(true, |ty| match *ty.kind() {
|
.map_or(true, |ty| match *ty.kind() {
|
||||||
ty::Param(_) => true,
|
ty::Param(_) => true,
|
||||||
ty::Adt(def, _) => def.did == args.ty_did,
|
ty::Adt(def, _) => def.did() == args.ty_did,
|
||||||
_ => false,
|
_ => false,
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
|
@ -607,7 +607,7 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
|
||||||
// If the types match check for methods which exist on both types. e.g. `Vec::len` and
|
// If the types match check for methods which exist on both types. e.g. `Vec::len` and
|
||||||
// `slice::len`
|
// `slice::len`
|
||||||
ty::Adt(def, _)
|
ty::Adt(def, _)
|
||||||
if def.did == args.ty_did
|
if def.did() == args.ty_did
|
||||||
&& (i != 0
|
&& (i != 0
|
||||||
|| self.cx.tcx.trait_of_item(id).is_some()
|
|| self.cx.tcx.trait_of_item(id).is_some()
|
||||||
|| !args.deref_assoc_items.map_or(false, |(id, items)| {
|
|| !args.deref_assoc_items.map_or(false, |(id, items)| {
|
||||||
|
|
|
@ -72,7 +72,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
|
||||||
|
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Some(self_def) = self_ty.ty_adt_def();
|
if let Some(self_def) = self_ty.ty_adt_def();
|
||||||
if let Some(self_local_did) = self_def.did.as_local();
|
if let Some(self_local_did) = self_def.did().as_local();
|
||||||
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
|
let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did);
|
||||||
if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id);
|
if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id);
|
||||||
let type_name = x.ident.name.as_str().to_lowercase();
|
let type_name = x.ident.name.as_str().to_lowercase();
|
||||||
|
|
|
@ -141,7 +141,7 @@ pub(super) fn check<'tcx>(
|
||||||
then {
|
then {
|
||||||
diag.note(&format!(
|
diag.note(&format!(
|
||||||
"two instances of the same generic type (`{}`) may have different layouts",
|
"two instances of the same generic type (`{}`) may have different layouts",
|
||||||
cx.tcx.item_name(from_def.did)
|
cx.tcx.item_name(from_def.did())
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
if from_ty_orig.peel_refs() != from_ty {
|
if from_ty_orig.peel_refs() != from_ty {
|
||||||
|
@ -304,13 +304,13 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
|
||||||
ty = sized_ty;
|
ty = sized_ty;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if def.repr.inhibit_struct_field_reordering_opt() {
|
if def.repr().inhibit_struct_field_reordering_opt() {
|
||||||
ReducedTy::OrderedFields(ty)
|
ReducedTy::OrderedFields(ty)
|
||||||
} else {
|
} else {
|
||||||
ReducedTy::UnorderedFields(ty)
|
ReducedTy::UnorderedFields(ty)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ty::Adt(def, _) if def.is_enum() && (def.variants.is_empty() || is_c_void(cx, ty)) => {
|
ty::Adt(def, _) if def.is_enum() && (def.variants().is_empty() || is_c_void(cx, ty)) => {
|
||||||
ReducedTy::TypeErasure
|
ReducedTy::TypeErasure
|
||||||
},
|
},
|
||||||
ty::Foreign(_) => ReducedTy::TypeErasure,
|
ty::Foreign(_) => ReducedTy::TypeErasure,
|
||||||
|
|
|
@ -11,11 +11,11 @@ use rustc_span::symbol::sym;
|
||||||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
|
||||||
match (&from_ty.kind(), &to_ty.kind()) {
|
match (&from_ty.kind(), &to_ty.kind()) {
|
||||||
(ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
|
(ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
|
||||||
if from_adt.did != to_adt.did {
|
if from_adt.did() != to_adt.did() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if !matches!(
|
if !matches!(
|
||||||
cx.tcx.get_diagnostic_name(to_adt.did),
|
cx.tcx.get_diagnostic_name(to_adt.did()),
|
||||||
Some(
|
Some(
|
||||||
sym::BTreeMap
|
sym::BTreeMap
|
||||||
| sym::BTreeSet
|
| sym::BTreeSet
|
||||||
|
|
|
@ -151,11 +151,11 @@ fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'t
|
||||||
fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let ty::Adt(def, subst) = ty.kind();
|
if let ty::Adt(def, subst) = ty.kind();
|
||||||
if match_def_path(cx, def.did, &paths::POLL);
|
if match_def_path(cx, def.did(), &paths::POLL);
|
||||||
let ready_ty = subst.type_at(0);
|
let ready_ty = subst.type_at(0);
|
||||||
|
|
||||||
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
|
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
|
||||||
if cx.tcx.is_diagnostic_item(sym::Result, ready_def.did);
|
if cx.tcx.is_diagnostic_item(sym::Result, ready_def.did());
|
||||||
then {
|
then {
|
||||||
Some(ready_subst.type_at(1))
|
Some(ready_subst.type_at(1))
|
||||||
} else {
|
} else {
|
||||||
|
@ -168,15 +168,15 @@ fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<
|
||||||
fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let ty::Adt(def, subst) = ty.kind();
|
if let ty::Adt(def, subst) = ty.kind();
|
||||||
if match_def_path(cx, def.did, &paths::POLL);
|
if match_def_path(cx, def.did(), &paths::POLL);
|
||||||
let ready_ty = subst.type_at(0);
|
let ready_ty = subst.type_at(0);
|
||||||
|
|
||||||
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
|
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
|
||||||
if cx.tcx.is_diagnostic_item(sym::Option, ready_def.did);
|
if cx.tcx.is_diagnostic_item(sym::Option, ready_def.did());
|
||||||
let some_ty = ready_subst.type_at(0);
|
let some_ty = ready_subst.type_at(0);
|
||||||
|
|
||||||
if let ty::Adt(some_def, some_subst) = some_ty.kind();
|
if let ty::Adt(some_def, some_subst) = some_ty.kind();
|
||||||
if cx.tcx.is_diagnostic_item(sym::Result, some_def.did);
|
if cx.tcx.is_diagnostic_item(sym::Result, some_def.did());
|
||||||
then {
|
then {
|
||||||
Some(some_subst.type_at(1))
|
Some(some_subst.type_at(1))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -102,9 +102,9 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
|
||||||
|
|
||||||
// Get the wrapper and inner types, if can't, abort.
|
// Get the wrapper and inner types, if can't, abort.
|
||||||
let (return_type_label, lang_item, inner_type) = if let ty::Adt(adt_def, subst) = return_ty(cx, hir_id).kind() {
|
let (return_type_label, lang_item, inner_type) = if let ty::Adt(adt_def, subst) = return_ty(cx, hir_id).kind() {
|
||||||
if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did) {
|
if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did()) {
|
||||||
("Option", OptionSome, subst.type_at(0))
|
("Option", OptionSome, subst.type_at(0))
|
||||||
} else if cx.tcx.is_diagnostic_item(sym::Result, adt_def.did) {
|
} else if cx.tcx.is_diagnostic_item(sym::Result, adt_def.did()) {
|
||||||
("Result", ResultOk, subst.type_at(0))
|
("Result", ResultOk, subst.type_at(0))
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -73,7 +73,7 @@ fn fn_eagerness<'tcx>(
|
||||||
// than marker traits.
|
// than marker traits.
|
||||||
// Due to the limited operations on these types functions should be fairly cheap.
|
// Due to the limited operations on these types functions should be fairly cheap.
|
||||||
if def
|
if def
|
||||||
.variants
|
.variants()
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|v| v.fields.iter())
|
.flat_map(|v| v.fields.iter())
|
||||||
.any(|x| matches!(cx.tcx.type_of(x.did).peel_refs().kind(), ty::Param(_)))
|
.any(|x| matches!(cx.tcx.type_of(x.did).peel_refs().kind(), ty::Param(_)))
|
||||||
|
|
|
@ -268,7 +268,7 @@ pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str])
|
||||||
pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
|
pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
|
||||||
if let Some(impl_did) = cx.tcx.impl_of_method(def_id) {
|
if let Some(impl_did) = cx.tcx.impl_of_method(def_id) {
|
||||||
if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() {
|
if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() {
|
||||||
return cx.tcx.is_diagnostic_item(diag_item, adt.did);
|
return cx.tcx.is_diagnostic_item(diag_item, adt.did());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
|
@ -657,7 +657,7 @@ fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<
|
||||||
if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() {
|
if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() {
|
||||||
return std_types_symbols
|
return std_types_symbols
|
||||||
.iter()
|
.iter()
|
||||||
.any(|&symbol| cx.tcx.is_diagnostic_item(symbol, adt.did));
|
.any(|&symbol| cx.tcx.is_diagnostic_item(symbol, adt.did()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ pub fn contains_ty(ty: Ty<'_>, other_ty: Ty<'_>) -> bool {
|
||||||
|
|
||||||
/// Walks into `ty` and returns `true` if any inner type is an instance of the given adt
|
/// Walks into `ty` and returns `true` if any inner type is an instance of the given adt
|
||||||
/// constructor.
|
/// constructor.
|
||||||
pub fn contains_adt_constructor(ty: Ty<'_>, adt: &AdtDef) -> bool {
|
pub fn contains_adt_constructor(ty: Ty<'_>, adt: AdtDef<'_>) -> bool {
|
||||||
ty.walk().any(|inner| match inner.unpack() {
|
ty.walk().any(|inner| match inner.unpack() {
|
||||||
GenericArgKind::Type(inner_ty) => inner_ty.ty_adt_def() == Some(adt),
|
GenericArgKind::Type(inner_ty) => inner_ty.ty_adt_def() == Some(adt),
|
||||||
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
|
||||||
|
@ -112,7 +112,7 @@ pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<
|
||||||
let def_id = match ty_to_check.kind() {
|
let def_id = match ty_to_check.kind() {
|
||||||
ty::Array(..) => return Some(sym::array),
|
ty::Array(..) => return Some(sym::array),
|
||||||
ty::Slice(..) => return Some(sym::slice),
|
ty::Slice(..) => return Some(sym::slice),
|
||||||
ty::Adt(adt, _) => adt.did,
|
ty::Adt(adt, _) => adt.did(),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ pub fn has_drop<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
||||||
// Returns whether the type has #[must_use] attribute
|
// Returns whether the type has #[must_use] attribute
|
||||||
pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(adt, _) => must_use_attr(cx.tcx.get_attrs(adt.did)).is_some(),
|
ty::Adt(adt, _) => must_use_attr(cx.tcx.get_attrs(adt.did())).is_some(),
|
||||||
ty::Foreign(ref did) => must_use_attr(cx.tcx.get_attrs(*did)).is_some(),
|
ty::Foreign(ref did) => must_use_attr(cx.tcx.get_attrs(*did)).is_some(),
|
||||||
ty::Slice(ty) | ty::Array(ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => {
|
ty::Slice(ty) | ty::Array(ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) | ty::Ref(_, ty, _) => {
|
||||||
// for the Array case we don't need to care for the len == 0 case
|
// for the Array case we don't need to care for the len == 0 case
|
||||||
|
@ -220,7 +220,7 @@ fn is_normalizable_helper<'tcx>(
|
||||||
let cause = rustc_middle::traits::ObligationCause::dummy();
|
let cause = rustc_middle::traits::ObligationCause::dummy();
|
||||||
if infcx.at(&cause, param_env).normalize(ty).is_ok() {
|
if infcx.at(&cause, param_env).normalize(ty).is_ok() {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(def, substs) => def.variants.iter().all(|variant| {
|
ty::Adt(def, substs) => def.variants().iter().all(|variant| {
|
||||||
variant
|
variant
|
||||||
.fields
|
.fields
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -264,7 +264,7 @@ pub fn is_recursively_primitive_type(ty: Ty<'_>) -> bool {
|
||||||
pub fn is_type_ref_to_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
|
pub fn is_type_ref_to_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Ref(_, ref_ty, _) => match ref_ty.kind() {
|
ty::Ref(_, ref_ty, _) => match ref_ty.kind() {
|
||||||
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did()),
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -284,7 +284,7 @@ pub fn is_type_ref_to_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_ite
|
||||||
/// [Diagnostic Items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
|
/// [Diagnostic Items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
|
||||||
pub fn is_type_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
|
pub fn is_type_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did()),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -294,7 +294,7 @@ pub fn is_type_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symb
|
||||||
/// Returns `false` if the `LangItem` is not defined.
|
/// Returns `false` if the `LangItem` is not defined.
|
||||||
pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangItem) -> bool {
|
pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangItem) -> bool {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(adt, _) => cx.tcx.lang_items().require(lang_item).map_or(false, |li| li == adt.did),
|
ty::Adt(adt, _) => cx.tcx.lang_items().require(lang_item).map_or(false, |li| li == adt.did()),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -310,7 +310,7 @@ pub fn is_isize_or_usize(typ: Ty<'_>) -> bool {
|
||||||
/// If you change the signature, remember to update the internal lint `MatchTypeOnDiagItem`
|
/// If you change the signature, remember to update the internal lint `MatchTypeOnDiagItem`
|
||||||
pub fn match_type(cx: &LateContext<'_>, ty: Ty<'_>, path: &[&str]) -> bool {
|
pub fn match_type(cx: &LateContext<'_>, ty: Ty<'_>, path: &[&str]) -> bool {
|
||||||
match ty.kind() {
|
match ty.kind() {
|
||||||
ty::Adt(adt, _) => match_def_path(cx, adt.did, path),
|
ty::Adt(adt, _) => match_def_path(cx, adt.did(), path),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -398,7 +398,7 @@ pub fn is_uninit_value_valid_for_ty(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
||||||
match *ty.kind() {
|
match *ty.kind() {
|
||||||
ty::Array(component, _) => is_uninit_value_valid_for_ty(cx, component),
|
ty::Array(component, _) => is_uninit_value_valid_for_ty(cx, component),
|
||||||
ty::Tuple(types) => types.iter().all(|ty| is_uninit_value_valid_for_ty(cx, ty)),
|
ty::Tuple(types) => types.iter().all(|ty| is_uninit_value_valid_for_ty(cx, ty)),
|
||||||
ty::Adt(adt, _) => cx.tcx.lang_items().maybe_uninit() == Some(adt.did),
|
ty::Adt(adt, _) => cx.tcx.lang_items().maybe_uninit() == Some(adt.did()),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -562,11 +562,11 @@ pub fn read_explicit_enum_value(tcx: TyCtxt<'_>, id: DefId) -> Option<EnumValue>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the value of the given variant.
|
/// Gets the value of the given variant.
|
||||||
pub fn get_discriminant_value(tcx: TyCtxt<'_>, adt: &'_ AdtDef, i: VariantIdx) -> EnumValue {
|
pub fn get_discriminant_value(tcx: TyCtxt<'_>, adt: AdtDef<'_>, i: VariantIdx) -> EnumValue {
|
||||||
let variant = &adt.variants[i];
|
let variant = &adt.variant(i);
|
||||||
match variant.discr {
|
match variant.discr {
|
||||||
VariantDiscr::Explicit(id) => read_explicit_enum_value(tcx, id).unwrap(),
|
VariantDiscr::Explicit(id) => read_explicit_enum_value(tcx, id).unwrap(),
|
||||||
VariantDiscr::Relative(x) => match adt.variants[(i.as_usize() - x as usize).into()].discr {
|
VariantDiscr::Relative(x) => match adt.variant((i.as_usize() - x as usize).into()).discr {
|
||||||
VariantDiscr::Explicit(id) => read_explicit_enum_value(tcx, id).unwrap() + x,
|
VariantDiscr::Explicit(id) => read_explicit_enum_value(tcx, id).unwrap() + x,
|
||||||
VariantDiscr::Relative(_) => EnumValue::Unsigned(x.into()),
|
VariantDiscr::Relative(_) => EnumValue::Unsigned(x.into()),
|
||||||
},
|
},
|
||||||
|
@ -577,7 +577,7 @@ pub fn get_discriminant_value(tcx: TyCtxt<'_>, adt: &'_ AdtDef, i: VariantIdx) -
|
||||||
/// platform specific `libc::<platform>::c_void` types in libc.
|
/// platform specific `libc::<platform>::c_void` types in libc.
|
||||||
pub fn is_c_void(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
pub fn is_c_void(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
|
||||||
if let ty::Adt(adt, _) = ty.kind()
|
if let ty::Adt(adt, _) = ty.kind()
|
||||||
&& let &[krate, .., name] = &*cx.get_def_path(adt.did)
|
&& let &[krate, .., name] = &*cx.get_def_path(adt.did())
|
||||||
&& let sym::libc | sym::core | sym::std = krate
|
&& let sym::libc | sym::core | sym::std = krate
|
||||||
&& name.as_str() == "c_void"
|
&& name.as_str() == "c_void"
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue