Skip to content

Unintuitive error message for missing conditional in if-else expression #13483

Closed
@LemmingAvalanche

Description

@LemmingAvalanche

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions