Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt does not format deeply nested functional code #6461

Closed
evlogiy opened this issue Feb 3, 2025 · 1 comment
Closed

rustfmt does not format deeply nested functional code #6461

evlogiy opened this issue Feb 3, 2025 · 1 comment
Labels
a-chains a-matches match arms, patterns, blocks, etc duplicate

Comments

@evlogiy
Copy link

evlogiy commented Feb 3, 2025

rustfmt does not apply any formatting to the contents of the following Rust function (which compiles and works as expected):

fn determinant(matrix: &[Vec<i64>]) -> i64 {
    match matrix.len(){1=>matrix[0][0],2=>matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0],_=>matrix[0].iter().enumerate().map(|(column,e)|if column%2==0{1}else{-1}*e*determinant(&matrix.iter().skip(1).map(|row|row.iter().enumerate().filter(|(i,_)|*i !=column).map(|(_,&e)|e).collect()).collect::<Vec<Vec<i64>>>())).sum()}
}

I formatted my code as a single line after facing some issues with the formatter. I thought that it might help me see where exactly the problem is. However, when running rustfmt, the only changes it makes are around the function declaration and {}. The internals of the function are left entirely untouched.

Expected behavior: rustfmt should format the function, introducing appropriate indentation and line breaks for readability.

The version currently used in Rust Playground: 1.8.0-beta (2025-02-01 0277061b9a)

Permalink to the playground with unedited one-liner.

Hope this helps.

@ytmimi
Copy link
Contributor

ytmimi commented Feb 7, 2025

The last match arm is the issue. It's way too long, and rustfmt can't format the expression within the max width and as a result formatting the match expression fails.

fn determinant(matrix: &[Vec<i64>]) -> i64 {
    match matrix.len() {
        1 => matrix[0][0],
        2 => matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0],
        _ => matrix[0].iter().enumerate().map(|(column, e)| if column % 2 == 0 { 1 } else { -1 } * e * determinant(&matrix.iter().skip(1).map(|row| row.iter().enumerate().filter(|(i, _)| *i != column).map(|(_, &e)| e).collect()).collect::<Vec<Vec<i64>>>())).sum(),
    }
}

@ytmimi ytmimi closed this as completed Feb 7, 2025
@ytmimi ytmimi added duplicate a-chains a-matches match arms, patterns, blocks, etc labels Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-chains a-matches match arms, patterns, blocks, etc duplicate
Projects
None yet
Development

No branches or pull requests

2 participants