Skip to content

Multi-lined pattern of if let #3648

@topecongiro

Description

@topecongiro
Contributor

From rust-lang/rust-clippy#4123 (comment):

     F: FnMut(&ast::Block, Option<&ast::Label>),
 {
     if let ast::ExprKind::While(_, loop_block, label)
-        | ast::ExprKind::ForLoop(_, _, loop_block, label)
-        | ast::ExprKind::Loop(loop_block, label)
-        = &expr.node
+    | ast::ExprKind::ForLoop(_, _, loop_block, label)
+    | ast::ExprKind::Loop(loop_block, label) = &expr.node
     {
         func(loop_block, label.as_ref());
     }

In this case,

  1. Should we add indentation to each clause that appears after the first one?
  2. Should we keep the expression (&expr.node) on the same line as the last clause, or put it on the next line?

Note that for multi-lined patterns of match's arm, rustfmt does not add indentation.

match some_value {
    ast::ExprKind::While(_, loop_block, label)
    | ast::ExprKind::ForLoop(_, _, loop_block, label)
    | ast::ExprKind::Loop(loop_block, label) = &expr.node
    => {
        // ...
    }

Activity

rchaser53

rchaser53 commented on Jun 24, 2019

@rchaser53
Contributor

I think that the pattern like multi-lined patterns of match's arm is good.

  1. No.
  2. keep the expression on the same line as the last clause.
ytmimi

ytmimi commented on Jul 20, 2022

@ytmimi
Contributor

confirming I can reproduce this behavior with rustfmt 1.5.1-nightly (f2c31ba0 2022-07-19)

Input

fn foo(f: F)
where
    F: FnMut(&ast::Block, Option<&ast::Label>),
{
    if let ast::ExprKind::While(_, loop_block, label)
        | ast::ExprKind::ForLoop(_, _, loop_block, label)
        | ast::ExprKind::Loop(loop_block, label)
        = &expr.node
    {
        func(loop_block, label.as_ref());
    }
}

Output

fn foo(f: F)
where
    F: FnMut(&ast::Block, Option<&ast::Label>),
{
    if let ast::ExprKind::While(_, loop_block, label)
    | ast::ExprKind::ForLoop(_, _, loop_block, label)
    | ast::ExprKind::Loop(loop_block, label) = &expr.node
    {
        func(loop_block, label.as_ref());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-matchesmatch arms, patterns, blocks, etcp-low

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rchaser53@topecongiro@ytmimi

        Issue actions

          Multi-lined pattern of if let · Issue #3648 · rust-lang/rustfmt