Rollup merge of #128762 - fmease:use-more-slice-pats, r=compiler-errors
Use more slice patterns inside the compiler Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'. r? ghost
This commit is contained in:
commit
32e0fe129d
40 changed files with 191 additions and 221 deletions
|
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -808,7 +808,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()))),
|
||||
|
@ -1544,14 +1544,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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue