]> git.proxmox.com Git - rustc.git/blob - src/test/ui/fmt/ifmt-bad-arg.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / src / test / ui / fmt / ifmt-bad-arg.rs
1 fn main() {
2 // bad arguments to the format! call
3
4 // bad number of arguments, see #44954 (originally #15780)
5
6 format!("{}");
7 //~^ ERROR: 1 positional argument in format string, but no arguments were given
8
9 format!("{1}", 1);
10 //~^ ERROR: invalid reference to positional argument 1 (there is 1 argument)
11 //~^^ ERROR: argument never used
12
13 format!("{} {}");
14 //~^ ERROR: 2 positional arguments in format string, but no arguments were given
15
16 format!("{0} {1}", 1);
17 //~^ ERROR: invalid reference to positional argument 1 (there is 1 argument)
18
19 format!("{0} {1} {2}", 1, 2);
20 //~^ ERROR: invalid reference to positional argument 2 (there are 2 arguments)
21
22 format!("{} {value} {} {}", 1, value=2);
23 //~^ ERROR: invalid reference to positional argument 2 (there are 2 arguments)
24 format!("{name} {value} {} {} {} {} {} {}", 0, name=1, value=2);
25 //~^ ERROR: invalid reference to positional arguments 3, 4 and 5 (there are 3 arguments)
26
27 format!("{} {foo} {} {bar} {}", 1, 2, 3);
28 //~^ ERROR: cannot find value `foo` in this scope
29 //~^^ ERROR: cannot find value `bar` in this scope
30
31 format!("{foo}"); //~ ERROR: cannot find value `foo` in this scope
32 format!("", 1, 2); //~ ERROR: multiple unused formatting arguments
33 format!("{}", 1, 2); //~ ERROR: argument never used
34 format!("{1}", 1, 2); //~ ERROR: argument never used
35 format!("{}", 1, foo=2); //~ ERROR: named argument never used
36 format!("{foo}", 1, foo=2); //~ ERROR: argument never used
37 format!("", foo=2); //~ ERROR: named argument never used
38 format!("{} {}", 1, 2, foo=1, bar=2); //~ ERROR: multiple unused formatting arguments
39
40 format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
41 format!("{foo} {} {}", foo=1, 2); //~ ERROR: positional arguments cannot follow
42
43 // bad named arguments, #35082
44
45 format!("{valuea} {valueb}", valuea=5, valuec=7);
46 //~^ ERROR cannot find value `valueb` in this scope
47 //~^^ ERROR named argument never used
48
49 // bad syntax of the format string
50
51 format!("{"); //~ ERROR: expected `'}'` but string was terminated
52
53 format!("foo } bar"); //~ ERROR: unmatched `}` found
54 format!("foo }"); //~ ERROR: unmatched `}` found
55
56 format!("foo %s baz", "bar"); //~ ERROR: argument never used
57
58 format!(r##"
59
60 {foo}
61
62 "##);
63 //~^^^ ERROR: cannot find value `foo` in this scope
64
65 // bad syntax in format string with multiple newlines, #53836
66 format!("first number: {}
67 second number: {}
68 third number: {}
69 fourth number: {}
70 fifth number: {}
71 sixth number: {}
72 seventh number: {}
73 eighth number: {}
74 ninth number: {
75 tenth number: {}",
76 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
77 //~^^ ERROR: invalid format string
78 println!("{} {:.*} {}", 1, 3.2, 4);
79 //~^ ERROR 4 positional arguments in format string, but there are 3 arguments
80 //~| ERROR mismatched types
81 println!("{} {:07$.*} {}", 1, 3.2, 4);
82 //~^ ERROR 4 positional arguments in format string, but there are 3 arguments
83 //~| ERROR mismatched types
84 println!("{} {:07$} {}", 1, 3.2, 4);
85 //~^ ERROR invalid reference to positional argument 7 (there are 3 arguments)
86 println!("{:foo}", 1); //~ ERROR unknown format trait `foo`
87 println!("{5} {:4$} {6:7$}", 1);
88 //~^ ERROR invalid reference to positional arguments 4, 5, 6 and 7 (there is 1 argument)
89
90 // We used to ICE here because we tried to unconditionally access the first argument, which
91 // doesn't exist.
92 println!("{:.*}");
93 //~^ ERROR 2 positional arguments in format string, but no arguments were given
94 }