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

@ -694,12 +694,12 @@ impl<'a> Parser<'a> {
// `foo: `
ExprKind::Path(None, ast::Path { segments, .. }),
token::Ident(kw::For | kw::Loop | kw::While, IdentIsRaw::No),
) if segments.len() == 1 => {
) if let [segment] = segments.as_slice() => {
let snapshot = self.create_snapshot_for_diagnostic();
let label = Label {
ident: Ident::from_str_and_span(
&format!("'{}", segments[0].ident),
segments[0].ident.span,
&format!("'{}", segment.ident),
segment.ident.span,
),
};
match self.parse_expr_labeled(label, false) {

View file

@ -471,9 +471,8 @@ impl<'a> Parser<'a> {
Err(mut err) => {
// Maybe the user misspelled `macro_rules` (issue #91227)
if self.token.is_ident()
&& path.segments.len() == 1
&& edit_distance("macro_rules", &path.segments[0].ident.to_string(), 2)
.is_some()
&& let [segment] = path.segments.as_slice()
&& edit_distance("macro_rules", &segment.ident.to_string(), 2).is_some()
{
err.span_suggestion(
path.span,

View file

@ -826,7 +826,8 @@ impl<'a> Parser<'a> {
// We can only resolve single-segment paths at the moment, because multi-segment paths
// require type-checking: see `visit_generic_arg` in `src/librustc_resolve/late.rs`.
ast::ExprKind::Path(None, path)
if path.segments.len() == 1 && path.segments[0].args.is_none() =>
if let [segment] = path.segments.as_slice()
&& segment.args.is_none() =>
{
true
}

View file

@ -672,7 +672,7 @@ impl<'a> Parser<'a> {
match &expr.kind {
ExprKind::Path(None, ast::Path { segments, .. })
if segments.len() == 1 =>
if let [segment] = segments.as_slice() =>
{
if self.token == token::Colon
&& self.look_ahead(1, |token| {
@ -689,8 +689,8 @@ impl<'a> Parser<'a> {
let snapshot = self.create_snapshot_for_diagnostic();
let label = Label {
ident: Ident::from_str_and_span(
&format!("'{}", segments[0].ident),
segments[0].ident.span,
&format!("'{}", segment.ident),
segment.ident.span,
),
};
match self.parse_expr_labeled(label, false) {