Skip to content

Commit 820a558

Browse files
committed
Special case for 0 arguments given in format!
1 parent 18717fc commit 820a558

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/libsyntax/ext/format.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ impl<'a, 'b> Context<'a, 'b> {
216216
}
217217

218218
fn describe_num_args(&self) -> String {
219-
if self.args.len() == 1 {
220-
"there is 1 argument".to_string()
221-
} else {
222-
format!("there are {} arguments", self.args.len())
219+
match self.args.len() {
220+
0 => "no arguments given".to_string(),
221+
1 => "there is 1 argument".to_string(),
222+
x => format!("there are {} arguments", x),
223223
}
224224
}
225225

src/test/compile-fail/ifmt-bad-arg.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ fn main() {
3232
// bad number of arguments, see #15780
3333

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

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

4040
format!("{0} {1} {2}", 1, 2);
41-
//^~ ERROR invalid reference to argument `2` (there are 2 arguments)
41+
//~^ ERROR invalid reference to argument `2` (there are 2 arguments)
42+
43+
format!("{0} {1}");
44+
//~^ ERROR invalid reference to argument `0` (no arguments given)
45+
//~^^ ERROR invalid reference to argument `1` (no arguments given)
4246

4347
// bad syntax of the format string
4448

0 commit comments

Comments
 (0)