1
Fork 0

Use more slice patterns inside the compiler

This commit is contained in:
León Orell Valerian Liehr 2024-08-07 12:41:49 +02:00
parent 60d146580c
commit c4c518d2d4
No known key found for this signature in database
GPG key ID: D17A07215F68E713
40 changed files with 191 additions and 221 deletions

View file

@ -527,11 +527,11 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
// Lint for constants that look like binding identifiers (#7526)
if let PatKind::Path(hir::QPath::Resolved(None, path)) = p.kind {
if let Res::Def(DefKind::Const, _) = path.res {
if path.segments.len() == 1 {
if let [segment] = path.segments {
NonUpperCaseGlobals::check_upper_case(
cx,
"constant in pattern",
&path.segments[0].ident,
&segment.ident,
);
}
}

View file

@ -211,15 +211,12 @@ fn lint_overflowing_range_endpoint<'tcx>(
if !is_range_literal(struct_expr) {
return false;
};
let ExprKind::Struct(_, eps, _) = &struct_expr.kind else { return false };
if eps.len() != 2 {
return false;
}
let ExprKind::Struct(_, [start, end], _) = &struct_expr.kind else { return false };
// We can suggest using an inclusive range
// (`..=`) instead only if it is the `end` that is
// overflowing and only by 1.
if !(eps[1].expr.hir_id == expr.hir_id && lit_val - 1 == max) {
if !(end.expr.hir_id == expr.hir_id && lit_val - 1 == max) {
return false;
};
@ -232,7 +229,7 @@ fn lint_overflowing_range_endpoint<'tcx>(
};
let sub_sugg = if expr.span.lo() == lit_span.lo() {
let Ok(start) = cx.sess().source_map().span_to_snippet(eps[0].span) else { return false };
let Ok(start) = cx.sess().source_map().span_to_snippet(start.span) else { return false };
UseInclusiveRange::WithoutParen {
sugg: struct_expr.span.shrink_to_lo().to(lit_span.shrink_to_hi()),
start,

View file

@ -801,7 +801,7 @@ trait UnusedDelimLint {
return;
}
let spans = match value.kind {
ast::ExprKind::Block(ref block, None) if block.stmts.len() == 1 => block.stmts[0]
ast::ExprKind::Block(ref block, None) if let [stmt] = block.stmts.as_slice() => stmt
.span
.find_ancestor_inside(value.span)
.map(|span| (value.span.with_hi(span.lo()), value.span.with_lo(span.hi()))),
@ -1537,14 +1537,12 @@ impl UnusedImportBraces {
}
// Trigger the lint only if there is one nested item
if items.len() != 1 {
return;
}
let [(tree, _)] = items.as_slice() else { return };
// Trigger the lint if the nested item is a non-self single item
let node_name = match items[0].0.kind {
let node_name = match tree.kind {
ast::UseTreeKind::Simple(rename) => {
let orig_ident = items[0].0.prefix.segments.last().unwrap().ident;
let orig_ident = tree.prefix.segments.last().unwrap().ident;
if orig_ident.name == kw::SelfLower {
return;
}