Closed
Description
I think that this error message is unintuitive. I would have expected something more along the lines of " line 38 error: expected conditional but found '{' ".
I guess what happens is that the parser interprets the if-body as the condtional expression; the program compiles fine if I write the conditional expression inside its own curly braces, which is then followed by the same if-body expression.
Compiler used:
rustc 0.11-pre-nightly (540c2a2 2014-04-04 01:01:51 -0700)
host: x86_64-unknown-linux-gnu
Code:
use std::io::println;
fn is_three(num: int) -> bool {
num % 3 == 0
}
fn is_five(num: int) -> bool {
num % 5 == 0
}
fn is_fftn(num: int) -> bool {
num % 15 == 0
}
#[test]
fn test_is_three_with_not_three() {
if is_three(1) {
fail!("One is not three");
}
}
#[test]
fn test_is_three() {
if is_three(1) {
fail!("One is not three");
}
}
fn main() {
for num in range(1,101) {
let answer =
if is_fftn(num) {
"FizzBuzz"
}
else if is_three(num) {
"Fizz"
}
else if { //ERROR: MISSING CONDITIONAL
"Buzz"
}
else {
""
};
println(answer);
}
}
Compiler output:
~/Dropbox/rust$ rustc fizzbuzz.rs
fizzbuzz.rs:41:7: 41:11 error: expected `{` but found `else`
fizzbuzz.rs:41 else {
^~~~
(the missing conditional error is at line 38)