Skip to main content

fix_loop_break

Function fix_loop_break 

Source
pub fn fix_loop_break<'db>(
    db: &'db dyn Database,
    node: SyntaxNode<'db>,
) -> Option<InternalFix<'db>>
Expand description

Converts a loop with a conditionally-breaking if statement into a while loop.

This function transforms loops that have a conditional if statement followed by a break into a while loop, which can simplify the logic and improve readability.

§Arguments

  • db - Reference to the SyntaxGroup for syntax tree access.
  • node - The SyntaxNode representing the loop expression.

§Returns

A String containing the transformed loop as a while loop, preserving the original formatting and indentation.

§Example

let mut x = 0;
loop {
    if x > 5 {
        break;
    }
    x += 1;
}

Would be converted to:

let mut x = 0;
while x <= 5 {
    x += 1;
}