Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation and tests for literal checking in print/println fo…
…rmat args
- Loading branch information
Michael Recachinas
committed
Apr 1, 2018
1 parent
83e2109
commit 62220ab
Showing
3 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
|
||
#![warn(print_literal)] | ||
|
||
fn main() { | ||
// these should be fine | ||
print!("Hello"); | ||
println!("Hello"); | ||
let world = "world"; | ||
println!("Hello {}", world); | ||
println!("3 in hex is {:X}", 3); | ||
|
||
// these should throw warnings | ||
print!("Hello {}", "world"); | ||
println!("Hello {} {}", world, "world"); | ||
println!("Hello {}", "world"); | ||
println!("10 / 4 is {}", 2.5); | ||
println!("2 + 1 = {}", 3); | ||
println!("2 + 1 = {:.4}", 3); | ||
println!("2 + 1 = {:5.4}", 3); | ||
println!("Debug test {:?}", "hello, world"); | ||
|
||
// positional args don't change the fact | ||
// that we're using a literal -- this should | ||
// throw a warning | ||
println!("{0} {1}", "hello", "world"); | ||
println!("{1} {0}", "hello", "world"); | ||
|
||
// named args shouldn't change anything either | ||
println!("{foo} {bar}", foo="hello", bar="world"); | ||
println!("{bar} {foo}", foo="hello", bar="world"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:14:24 | ||
| | ||
14 | print!("Hello {}", "world"); | ||
| ^^^^^^^ | ||
| | ||
= note: `-D print-literal` implied by `-D warnings` | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:15:36 | ||
| | ||
15 | println!("Hello {} {}", world, "world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:16:26 | ||
| | ||
16 | println!("Hello {}", "world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:17:30 | ||
| | ||
17 | println!("10 / 4 is {}", 2.5); | ||
| ^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:18:28 | ||
| | ||
18 | println!("2 + 1 = {}", 3); | ||
| ^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:19:31 | ||
| | ||
19 | println!("2 + 1 = {:.4}", 3); | ||
| ^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:20:32 | ||
| | ||
20 | println!("2 + 1 = {:5.4}", 3); | ||
| ^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:21:33 | ||
| | ||
21 | println!("Debug test {:?}", "hello, world"); | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:26:25 | ||
| | ||
26 | println!("{0} {1}", "hello", "world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:26:34 | ||
| | ||
26 | println!("{0} {1}", "hello", "world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:27:25 | ||
| | ||
27 | println!("{1} {0}", "hello", "world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:27:34 | ||
| | ||
27 | println!("{1} {0}", "hello", "world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:30:33 | ||
| | ||
30 | println!("{foo} {bar}", foo="hello", bar="world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:30:46 | ||
| | ||
30 | println!("{foo} {bar}", foo="hello", bar="world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:31:33 | ||
| | ||
31 | println!("{bar} {foo}", foo="hello", bar="world"); | ||
| ^^^^^^^ | ||
|
||
error: printing a literal with an empty format string | ||
--> $DIR/print_literal.rs:31:46 | ||
| | ||
31 | println!("{bar} {foo}", foo="hello", bar="world"); | ||
| ^^^^^^^ | ||
|
||
error: aborting due to 16 previous errors | ||
|