6 - Make more use of let_chains

Continuation of #94376.

cc #53667
This commit is contained in:
Caio
2022-02-28 21:12:52 -03:00
parent 4ce3749235
commit fe94f78b9b
8 changed files with 97 additions and 118 deletions

View File

@@ -315,47 +315,43 @@ fn check_for_bindings_named_same_as_variants(
rf: RefutableFlag,
) {
pat.walk_always(|p| {
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
if let Some(ty::BindByValue(hir::Mutability::Not)) =
if let hir::PatKind::Binding(_, _, ident, None) = p.kind
&& let Some(ty::BindByValue(hir::Mutability::Not)) =
cx.typeck_results.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
{
let pat_ty = cx.typeck_results.pat_ty(p).peel_refs();
if let ty::Adt(edef, _) = pat_ty.kind() {
if edef.is_enum()
&& edef.variants.iter().any(|variant| {
variant.ident(cx.tcx) == ident && variant.ctor_kind == CtorKind::Const
})
{
let variant_count = edef.variants.len();
cx.tcx.struct_span_lint_hir(
BINDINGS_WITH_VARIANT_NAME,
p.hir_id,
&& let pat_ty = cx.typeck_results.pat_ty(p).peel_refs()
&& let ty::Adt(edef, _) = pat_ty.kind()
&& edef.is_enum()
&& edef.variants.iter().any(|variant| {
variant.ident(cx.tcx) == ident && variant.ctor_kind == CtorKind::Const
})
{
let variant_count = edef.variants.len();
cx.tcx.struct_span_lint_hir(
BINDINGS_WITH_VARIANT_NAME,
p.hir_id,
p.span,
|lint| {
let ty_path = cx.tcx.def_path_str(edef.did);
let mut err = lint.build(&format!(
"pattern binding `{}` is named the same as one \
of the variants of the type `{}`",
ident, ty_path
));
err.code(error_code!(E0170));
// If this is an irrefutable pattern, and there's > 1 variant,
// then we can't actually match on this. Applying the below
// suggestion would produce code that breaks on `check_irrefutable`.
if rf == Refutable || variant_count == 1 {
err.span_suggestion(
p.span,
|lint| {
let ty_path = cx.tcx.def_path_str(edef.did);
let mut err = lint.build(&format!(
"pattern binding `{}` is named the same as one \
of the variants of the type `{}`",
ident, ty_path
));
err.code(error_code!(E0170));
// If this is an irrefutable pattern, and there's > 1 variant,
// then we can't actually match on this. Applying the below
// suggestion would produce code that breaks on `check_irrefutable`.
if rf == Refutable || variant_count == 1 {
err.span_suggestion(
p.span,
"to match on the variant, qualify the path",
format!("{}::{}", ty_path, ident),
Applicability::MachineApplicable,
);
}
err.emit();
},
)
"to match on the variant, qualify the path",
format!("{}::{}", ty_path, ident),
Applicability::MachineApplicable,
);
}
}
}
err.emit();
},
)
}
});
}
@@ -622,10 +618,8 @@ fn maybe_point_at_variant<'a, 'p: 'a, 'tcx: 'a>(
let mut covered = vec![];
for pattern in patterns {
if let Variant(variant_index) = pattern.ctor() {
if let ty::Adt(this_def, _) = pattern.ty().kind() {
if this_def.did != def.did {
continue;
}
if let ty::Adt(this_def, _) = pattern.ty().kind() && this_def.did != def.did {
continue;
}
let sp = def.variants[*variant_index].ident(cx.tcx).span;
if covered.contains(&sp) {

View File

@@ -680,15 +680,13 @@ impl<'tcx> Constructor<'tcx> {
///
/// This means that the variant has a stdlib unstable feature marking it.
pub(super) fn is_unstable_variant(&self, pcx: PatCtxt<'_, '_, 'tcx>) -> bool {
if let Constructor::Variant(idx) = self {
if let ty::Adt(adt, _) = pcx.ty.kind() {
let variant_def_id = adt.variants[*idx].def_id;
// Filter variants that depend on a disabled unstable feature.
return matches!(
pcx.cx.tcx.eval_stability(variant_def_id, None, DUMMY_SP, None),
EvalResult::Deny { .. }
);
}
if let Constructor::Variant(idx) = self && let ty::Adt(adt, _) = pcx.ty.kind() {
let variant_def_id = adt.variants[*idx].def_id;
// Filter variants that depend on a disabled unstable feature.
return matches!(
pcx.cx.tcx.eval_stability(variant_def_id, None, DUMMY_SP, None),
EvalResult::Deny { .. }
);
}
false
}
@@ -696,11 +694,9 @@ impl<'tcx> Constructor<'tcx> {
/// Checks if the `Constructor` is a `Constructor::Variant` with a `#[doc(hidden)]`
/// attribute.
pub(super) fn is_doc_hidden_variant(&self, pcx: PatCtxt<'_, '_, 'tcx>) -> bool {
if let Constructor::Variant(idx) = self {
if let ty::Adt(adt, _) = pcx.ty.kind() {
let variant_def_id = adt.variants[*idx].def_id;
return pcx.cx.tcx.is_doc_hidden(variant_def_id);
}
if let Constructor::Variant(idx) = self && let ty::Adt(adt, _) = pcx.ty.kind() {
let variant_def_id = adt.variants[*idx].def_id;
return pcx.cx.tcx.is_doc_hidden(variant_def_id);
}
false
}

View File

@@ -790,16 +790,14 @@ crate fn compare_const_vals<'tcx>(
};
}
if let ty::Str = ty.kind() {
if let (
ty::ConstKind::Value(a_val @ ConstValue::Slice { .. }),
ty::ConstKind::Value(b_val @ ConstValue::Slice { .. }),
) = (a.val(), b.val())
{
let a_bytes = get_slice_bytes(&tcx, a_val);
let b_bytes = get_slice_bytes(&tcx, b_val);
return from_bool(a_bytes == b_bytes);
}
if let ty::Str = ty.kind() && let (
ty::ConstKind::Value(a_val @ ConstValue::Slice { .. }),
ty::ConstKind::Value(b_val @ ConstValue::Slice { .. }),
) = (a.val(), b.val())
{
let a_bytes = get_slice_bytes(&tcx, a_val);
let b_bytes = get_slice_bytes(&tcx, b_val);
return from_bool(a_bytes == b_bytes);
}
fallback()
}