Use let_else in some more places in rustc_lint

This commit is contained in:
est31 2021-12-03 03:25:11 +01:00
parent ff23ad3179
commit bdc4b46221
6 changed files with 52 additions and 69 deletions

View File

@ -79,9 +79,8 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
let receiver_ty = cx.typeck_results().expr_ty(receiver_arg); let receiver_ty = cx.typeck_results().expr_ty(receiver_arg);
let adjustments = cx.typeck_results().expr_adjustments(receiver_arg); let adjustments = cx.typeck_results().expr_adjustments(receiver_arg);
let target = match adjustments.last() { let Some(Adjustment { kind: Adjust::Borrow(_), target }) = adjustments.last() else {
Some(Adjustment { kind: Adjust::Borrow(_), target }) => target, return
_ => return,
}; };
let types = let types =

View File

@ -609,14 +609,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
// If the trait is private, add the impl items to `private_traits` so they don't get // If the trait is private, add the impl items to `private_traits` so they don't get
// reported for missing docs. // reported for missing docs.
let real_trait = trait_ref.path.res.def_id(); let real_trait = trait_ref.path.res.def_id();
if let Some(def_id) = real_trait.as_local() { let Some(def_id) = real_trait.as_local() else { return };
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) { let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) else { return };
if let hir::VisibilityKind::Inherited = item.vis.node { if let hir::VisibilityKind::Inherited = item.vis.node {
for impl_item_ref in items { for impl_item_ref in items {
self.private_traits.insert(impl_item_ref.id.hir_id()); self.private_traits.insert(impl_item_ref.id.hir_id());
}
}
} }
} }
return; return;
@ -829,9 +827,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
_ => return, _ => return,
} }
let debug = match cx.tcx.get_diagnostic_item(sym::Debug) { let Some(debug) = cx.tcx.get_diagnostic_item(sym::Debug) else {
Some(debug) => debug, return
None => return,
}; };
if self.impling_types.is_none() { if self.impling_types.is_none() {
@ -1509,9 +1506,8 @@ impl TypeAliasBounds {
impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds { impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) { fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
let (ty, type_alias_generics) = match item.kind { let hir::ItemKind::TyAlias(ty, type_alias_generics) = &item.kind else {
hir::ItemKind::TyAlias(ref ty, ref generics) => (&*ty, generics), return
_ => return,
}; };
if let hir::TyKind::OpaqueDef(..) = ty.kind { if let hir::TyKind::OpaqueDef(..) = ty.kind {
// Bounds are respected for `type X = impl Trait` // Bounds are respected for `type X = impl Trait`
@ -2266,16 +2262,15 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
// and should check for them here. // and should check for them here.
match predicate.bounded_ty.kind { match predicate.bounded_ty.kind {
hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => { hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
if let Res::Def(DefKind::TyParam, def_id) = path.res { let Res::Def(DefKind::TyParam, def_id) = path.res else {
let index = ty_generics.param_def_id_to_index[&def_id]; continue
( };
Self::lifetimes_outliving_type(inferred_outlives, index), let index = ty_generics.param_def_id_to_index[&def_id];
&predicate.bounds, (
predicate.span, Self::lifetimes_outliving_type(inferred_outlives, index),
) &predicate.bounds,
} else { predicate.span,
continue; )
}
} }
_ => { _ => {
continue; continue;
@ -3216,18 +3211,17 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
for (idx, _) in statement.match_indices(':') { for (idx, _) in statement.match_indices(':') {
let possible_label = statement[start_idx..idx].trim(); let possible_label = statement[start_idx..idx].trim();
let mut chars = possible_label.chars(); let mut chars = possible_label.chars();
if let Some(c) = chars.next() { let Some(c) = chars.next() else {
// A label starts with an alphabetic character or . or _ and continues with alphanumeric characters, _, or $
if (c.is_alphabetic() || matches!(c, '.' | '_'))
&& chars.all(|c| c.is_alphanumeric() || matches!(c, '_' | '$'))
{
found_labels.push(possible_label);
} else {
// If we encounter a non-label, there cannot be any further labels, so stop checking
break;
}
} else {
// Empty string means a leading ':' in this section, which is not a label // Empty string means a leading ':' in this section, which is not a label
break
};
// A label starts with an alphabetic character or . or _ and continues with alphanumeric characters, _, or $
if (c.is_alphabetic() || matches!(c, '.' | '_'))
&& chars.all(|c| c.is_alphanumeric() || matches!(c, '_' | '$'))
{
found_labels.push(possible_label);
} else {
// If we encounter a non-label, there cannot be any further labels, so stop checking
break; break;
} }

View File

@ -227,14 +227,12 @@ impl<'s> LintLevelsBuilder<'s> {
let sess = self.sess; let sess = self.sess;
let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input"); let bad_attr = |span| struct_span_err!(sess, span, E0452, "malformed lint attribute input");
for attr in attrs { for attr in attrs {
let level = match Level::from_symbol(attr.name_or_empty()) { let Some(level) = Level::from_symbol(attr.name_or_empty()) else {
None => continue, continue
Some(lvl) => lvl,
}; };
let mut metas = match attr.meta_item_list() { let Some(mut metas) = attr.meta_item_list() else {
Some(x) => x, continue
None => continue,
}; };
if metas.is_empty() { if metas.is_empty() {
@ -481,9 +479,8 @@ impl<'s> LintLevelsBuilder<'s> {
continue; continue;
} }
let (lint_attr_name, lint_attr_span) = match *src { let LintLevelSource::Node(lint_attr_name, lint_attr_span, _) = *src else {
LintLevelSource::Node(name, span, _) => (name, span), continue
_ => continue,
}; };
let lint = builtin::UNUSED_ATTRIBUTES; let lint = builtin::UNUSED_ATTRIBUTES;

View File

@ -40,9 +40,8 @@ declare_lint_pass!(NoopMethodCall => [NOOP_METHOD_CALL]);
impl<'tcx> LateLintPass<'tcx> for NoopMethodCall { impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// We only care about method calls. // We only care about method calls.
let (call, elements) = match expr.kind { let ExprKind::MethodCall(call, _, elements, _) = &expr.kind else {
ExprKind::MethodCall(call, _, elements, _) => (call, elements), return
_ => return,
}; };
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow` // We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
// traits and ignore any other method call. // traits and ignore any other method call.
@ -70,9 +69,8 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
} }
let param_env = cx.tcx.param_env(trait_id); let param_env = cx.tcx.param_env(trait_id);
// Resolve the trait method instance. // Resolve the trait method instance.
let i = match ty::Instance::resolve(cx.tcx, param_env, did, substs) { let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, param_env, did, substs) else {
Ok(Some(i)) => i, return
_ => return,
}; };
// (Re)check that it implements the noop diagnostic. // (Re)check that it implements the noop diagnostic.
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return }; let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };

View File

@ -91,9 +91,8 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
let predicates = cx.tcx.explicit_predicates_of(item.def_id); let predicates = cx.tcx.explicit_predicates_of(item.def_id);
for &(predicate, span) in predicates.predicates { for &(predicate, span) in predicates.predicates {
let trait_predicate = match predicate.kind().skip_binder() { let Trait(trait_predicate) = predicate.kind().skip_binder() else {
Trait(trait_predicate) => trait_predicate, continue
_ => continue,
}; };
if trait_predicate.constness == ty::BoundConstness::ConstIfConst { if trait_predicate.constness == ty::BoundConstness::ConstIfConst {
// `~const Drop` definitely have meanings so avoid linting here. // `~const Drop` definitely have meanings so avoid linting here.
@ -106,9 +105,8 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
continue; continue;
} }
cx.struct_span_lint(DROP_BOUNDS, span, |lint| { cx.struct_span_lint(DROP_BOUNDS, span, |lint| {
let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) { let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
Some(needs_drop) => needs_drop, return
None => return,
}; };
let msg = format!( let msg = format!(
"bounds on `{}` are most likely incorrect, consider instead \ "bounds on `{}` are most likely incorrect, consider instead \
@ -123,17 +121,15 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
} }
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) { fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
let bounds = match &ty.kind { let hir::TyKind::TraitObject(bounds, _lifetime, _syntax) = &ty.kind else {
hir::TyKind::TraitObject(bounds, _lifetime, _syntax) => bounds, return
_ => return,
}; };
for bound in &bounds[..] { for bound in &bounds[..] {
let def_id = bound.trait_ref.trait_def_id(); let def_id = bound.trait_ref.trait_def_id();
if cx.tcx.lang_items().drop_trait() == def_id { if cx.tcx.lang_items().drop_trait() == def_id {
cx.struct_span_lint(DYN_DROP, bound.span, |lint| { cx.struct_span_lint(DYN_DROP, bound.span, |lint| {
let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) { let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
Some(needs_drop) => needs_drop, return
None => return,
}; };
let msg = format!( let msg = format!(
"types that do not implement `Drop` can still have drop glue, consider \ "types that do not implement `Drop` can still have drop glue, consider \

View File

@ -1342,11 +1342,10 @@ impl<'tcx> LateLintPass<'tcx> for VariantSizeDifferences {
| ty::layout::LayoutError::NormalizationFailure(_, _), | ty::layout::LayoutError::NormalizationFailure(_, _),
) => return, ) => return,
}; };
let (variants, tag) = match layout.variants { let Variants::Multiple {
Variants::Multiple {
tag_encoding: TagEncoding::Direct, tag, ref variants, .. tag_encoding: TagEncoding::Direct, tag, ref variants, ..
} => (variants, tag), } = &layout.variants else {
_ => return, return
}; };
let tag_size = tag.value.size(&cx.tcx).bytes(); let tag_size = tag.value.size(&cx.tcx).bytes();