Skip to content

Commit

Permalink
Special case for 0 arguments given in format!
Browse files Browse the repository at this point in the history
  • Loading branch information
treeman committed Jul 18, 2014
1 parent 18717fc commit 820a558
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ impl<'a, 'b> Context<'a, 'b> {
}

fn describe_num_args(&self) -> String {
if self.args.len() == 1 {
"there is 1 argument".to_string()
} else {
format!("there are {} arguments", self.args.len())
match self.args.len() {
0 => "no arguments given".to_string(),
1 => "there is 1 argument".to_string(),
x => format!("there are {} arguments", x),
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/test/compile-fail/ifmt-bad-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ fn main() {
// bad number of arguments, see #15780

format!("{0}");
//^~ ERROR invalid reference to argument `0` (there are 0 arguments)
//~^ ERROR invalid reference to argument `0` (no arguments given)

format!("{0} {1}", 1);
//^~ ERROR invalid reference to argument `1` (there is 1 argument)
//~^ ERROR invalid reference to argument `1` (there is 1 argument)

format!("{0} {1} {2}", 1, 2);
//^~ ERROR invalid reference to argument `2` (there are 2 arguments)
//~^ ERROR invalid reference to argument `2` (there are 2 arguments)

format!("{0} {1}");
//~^ ERROR invalid reference to argument `0` (no arguments given)
//~^^ ERROR invalid reference to argument `1` (no arguments given)

// bad syntax of the format string

Expand Down

5 comments on commit 820a558

@bors
Copy link
Contributor

@bors bors commented on 820a558 Jul 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 820a558 Jul 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging treeman/rust/fix-15780 = 820a558 into auto

@bors
Copy link
Contributor

@bors bors commented on 820a558 Jul 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

treeman/rust/fix-15780 = 820a558 merged ok, testing candidate = 4f55b52

@bors
Copy link
Contributor

@bors bors commented on 820a558 Jul 20, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 4f55b52

Please sign in to comment.