E0018

E0018#

Warning name: useless_loop

There is no continue in this loop expression, so loop is useless here.

This error occurs when using a loop expression that contains no continue statement. In MoonBit, a loop without any continue statements is equivalent to an block expression that will be executed only once, making the loop keyword redundant in such cases. You should either add a continue statement if you need to restart the loop from the beginning, or use a simpler control flow construct like match or if.

Erroneous example#

///|
fn main {
  let count = 2
  loop count {
    _..<0 => break
    _ => println(count)
  }
}

Output:

2

Suggestion#

Either add a continue statement if you need to restart the loop:

///|
fn main {
  let count = 2
  loop count {
    _..<0 => break
    count => {
      println(count)
      continue count - 1
    }
  }
}

Output:

2
1
0

Or use if/match you don't need to use continue:

///|
fn main {
  let count = 2
  match count {
    _..<0 => ()
    _ => println(count)
  }
}

Output:

2