Skip to content

Commit ff0e5f9

Browse files
committed
Rewrite the print/write macro checks as a PreExpansionPass
1 parent 8085ed7 commit ff0e5f9

14 files changed

+251
-359
lines changed

clippy_lints/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use toml;
1818
use rustc_plugin;
19+
use rustc;
1920

2021

2122
macro_rules! declare_clippy_lint {
@@ -175,6 +176,10 @@ mod reexport {
175176
crate use syntax::ast::{Name, NodeId};
176177
}
177178

179+
pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &mut rustc::lint::LintStore) {
180+
store.register_pre_expansion_pass(Some(session), box write::Pass);
181+
}
182+
178183
#[cfg_attr(rustfmt, rustfmt_skip)]
179184
pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
180185
let conf = match utils::conf::file_from_args(reg.args()) {
@@ -320,7 +325,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
320325
reg.register_late_lint_pass(box strings::StringLitAsBytes);
321326
reg.register_late_lint_pass(box derive::Derive);
322327
reg.register_late_lint_pass(box types::CharLitAsU8);
323-
reg.register_late_lint_pass(box write::Pass);
324328
reg.register_late_lint_pass(box vec::Pass);
325329
reg.register_early_lint_pass(box non_expressive_names::NonExpressiveNames {
326330
single_char_binding_names_threshold: conf.single_char_binding_names_threshold,

clippy_lints/src/write.rs

Lines changed: 130 additions & 304 deletions
Large diffs are not rendered by default.

src/driver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub fn main() {
118118
for (name, to) in lint_groups {
119119
ls.register_group(Some(sess), true, name, to);
120120
}
121+
clippy_lints::register_pre_expansion_lints(sess, &mut ls);
121122

122123
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
123124
sess.plugin_attributes.borrow_mut().extend(attributes);

tests/ui/excessive_precision.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() {
2222
const BAD64_3: f64 = 0.100_000_000_000_000_000_1;
2323

2424
// Literal as param
25-
println!("{}", 8.888_888_888_888_888_888_888);
25+
println!("{:?}", 8.888_888_888_888_888_888_888);
2626

2727
// // TODO add inferred type tests for f32
2828
// Locals

tests/ui/excessive_precision.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ error: float has excessive precision
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
4444

4545
error: float has excessive precision
46-
--> $DIR/excessive_precision.rs:25:20
46+
--> $DIR/excessive_precision.rs:25:22
4747
|
48-
25 | println!("{}", 8.888_888_888_888_888_888_888);
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `8.888_888_888_888_89`
48+
25 | println!("{:?}", 8.888_888_888_888_888_888_888);
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `8.888_888_888_888_89`
5050

5151
error: float has excessive precision
5252
--> $DIR/excessive_precision.rs:36:22

tests/ui/matches.stderr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ help: instead of prefixing all patterns with `&`, you can dereference the expres
2626
32 | None => println!("none"),
2727
|
2828

29+
error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
30+
--> $DIR/matches.rs:40:5
31+
|
32+
40 | / match tup {
33+
41 | | &(v, 1) => println!("{}", v),
34+
42 | | _ => println!("none"),
35+
43 | | }
36+
| |_____^
37+
help: try this
38+
|
39+
40 | if let &(v, 1) = tup {
40+
41 | # [ cfg ( not ( stage0 ) ) ] {
41+
42 | ( $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ) ; } # [
42+
43 | cfg ( stage0 ) ] { print ! ( "{}/n" , format_args ! ( $ ( $ arg ) * ) ) } } else {
43+
44 | ( $ crate :: io :: _print ( format_args_nl ! ( $ ( $ arg ) * ) ) ) ; }
44+
|
45+
2946
error: you don't need to add `&` to all patterns
3047
--> $DIR/matches.rs:40:5
3148
|
@@ -350,5 +367,5 @@ error: use as_mut() instead
350367
221 | | };
351368
| |_____^ help: try this: `mut_owned.as_mut()`
352369

353-
error: aborting due to 25 previous errors
370+
error: aborting due to 26 previous errors
354371

tests/ui/non_expressive_names.stderr

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
error: using `println!("")`
2+
--> $DIR/non_expressive_names.rs:60:14
3+
|
4+
60 | _ => println!(""),
5+
| ^^^^^^^^^^^^ help: replace it with: `println!()`
6+
|
7+
= note: `-D println-empty-string` implied by `-D warnings`
8+
9+
error: using `println!("")`
10+
--> $DIR/non_expressive_names.rs:128:18
11+
|
12+
128 | 1 => println!(""),
13+
| ^^^^^^^^^^^^ help: replace it with: `println!()`
14+
15+
error: using `println!("")`
16+
--> $DIR/non_expressive_names.rs:132:18
17+
|
18+
132 | 1 => println!(""),
19+
| ^^^^^^^^^^^^ help: replace it with: `println!()`
20+
121
error: binding's name is too similar to existing binding
222
--> $DIR/non_expressive_names.rs:18:9
323
|
@@ -167,5 +187,5 @@ error: consider choosing a more descriptive name
167187
151 | let __1___2 = 12;
168188
| ^^^^^^^
169189

170-
error: aborting due to 17 previous errors
190+
error: aborting due to 20 previous errors
171191

tests/ui/print.stderr

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
11
error: use of `Debug`-based formatting
2-
--> $DIR/print.rs:13:27
2+
--> $DIR/print.rs:13:19
33
|
44
13 | write!(f, "{:?}", 43.1415)
5-
| ^^^^^^^
5+
| ^^^^^^
66
|
77
= note: `-D use-debug` implied by `-D warnings`
88

9+
error: use of `Debug`-based formatting
10+
--> $DIR/print.rs:20:19
11+
|
12+
20 | write!(f, "{:?}", 42.718)
13+
| ^^^^^^
14+
915
error: use of `println!`
1016
--> $DIR/print.rs:25:5
1117
|
1218
25 | println!("Hello");
13-
| ^^^^^^^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^^^^^
1420
|
1521
= note: `-D print-stdout` implied by `-D warnings`
1622

1723
error: use of `print!`
1824
--> $DIR/print.rs:26:5
1925
|
2026
26 | print!("Hello");
21-
| ^^^^^^^^^^^^^^^^
27+
| ^^^^^^^^^^^^^^^
2228

2329
error: use of `print!`
2430
--> $DIR/print.rs:28:5
2531
|
2632
28 | print!("Hello {}", "World");
27-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2834

2935
error: use of `print!`
3036
--> $DIR/print.rs:30:5
3137
|
3238
30 | print!("Hello {:?}", "World");
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3440

3541
error: use of `Debug`-based formatting
36-
--> $DIR/print.rs:30:26
42+
--> $DIR/print.rs:30:12
3743
|
3844
30 | print!("Hello {:?}", "World");
39-
| ^^^^^^^
45+
| ^^^^^^^^^^^^
4046

4147
error: use of `print!`
4248
--> $DIR/print.rs:32:5
4349
|
4450
32 | print!("Hello {:#?}", "#orld");
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4652

4753
error: use of `Debug`-based formatting
48-
--> $DIR/print.rs:32:27
54+
--> $DIR/print.rs:32:12
4955
|
5056
32 | print!("Hello {:#?}", "#orld");
51-
| ^^^^^^^
57+
| ^^^^^^^^^^^^^
5258

53-
error: aborting due to 8 previous errors
59+
error: aborting due to 9 previous errors
5460

tests/ui/print_literal.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
1-
error: printing a literal with an empty format string
1+
error: literal with an empty format string
22
--> $DIR/print_literal.rs:23:71
33
|
44
23 | println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
55
| ^
66
|
77
= note: `-D print-literal` implied by `-D warnings`
88

9-
error: printing a literal with an empty format string
9+
error: literal with an empty format string
1010
--> $DIR/print_literal.rs:24:24
1111
|
1212
24 | print!("Hello {}", "world");
1313
| ^^^^^^^
1414

15-
error: printing a literal with an empty format string
15+
error: literal with an empty format string
1616
--> $DIR/print_literal.rs:25:36
1717
|
1818
25 | println!("Hello {} {}", world, "world");
1919
| ^^^^^^^
2020

21-
error: printing a literal with an empty format string
21+
error: literal with an empty format string
2222
--> $DIR/print_literal.rs:26:26
2323
|
2424
26 | println!("Hello {}", "world");
2525
| ^^^^^^^
2626

27-
error: printing a literal with an empty format string
27+
error: literal with an empty format string
2828
--> $DIR/print_literal.rs:27:30
2929
|
3030
27 | println!("10 / 4 is {}", 2.5);
3131
| ^^^
3232

33-
error: printing a literal with an empty format string
33+
error: literal with an empty format string
3434
--> $DIR/print_literal.rs:28:28
3535
|
3636
28 | println!("2 + 1 = {}", 3);
3737
| ^
3838

39-
error: printing a literal with an empty format string
39+
error: literal with an empty format string
4040
--> $DIR/print_literal.rs:33:25
4141
|
4242
33 | println!("{0} {1}", "hello", "world");
4343
| ^^^^^^^
4444

45-
error: printing a literal with an empty format string
45+
error: literal with an empty format string
4646
--> $DIR/print_literal.rs:33:34
4747
|
4848
33 | println!("{0} {1}", "hello", "world");
4949
| ^^^^^^^
5050

51-
error: printing a literal with an empty format string
51+
error: literal with an empty format string
5252
--> $DIR/print_literal.rs:34:25
5353
|
5454
34 | println!("{1} {0}", "hello", "world");
5555
| ^^^^^^^
5656

57-
error: printing a literal with an empty format string
57+
error: literal with an empty format string
5858
--> $DIR/print_literal.rs:34:34
5959
|
6060
34 | println!("{1} {0}", "hello", "world");
6161
| ^^^^^^^
6262

63-
error: printing a literal with an empty format string
63+
error: literal with an empty format string
6464
--> $DIR/print_literal.rs:37:33
6565
|
6666
37 | println!("{foo} {bar}", foo="hello", bar="world");
6767
| ^^^^^^^
6868

69-
error: printing a literal with an empty format string
69+
error: literal with an empty format string
7070
--> $DIR/print_literal.rs:37:46
7171
|
7272
37 | println!("{foo} {bar}", foo="hello", bar="world");
7373
| ^^^^^^^
7474

75-
error: printing a literal with an empty format string
75+
error: literal with an empty format string
7676
--> $DIR/print_literal.rs:38:33
7777
|
7878
38 | println!("{bar} {foo}", foo="hello", bar="world");
7979
| ^^^^^^^
8080

81-
error: printing a literal with an empty format string
81+
error: literal with an empty format string
8282
--> $DIR/print_literal.rs:38:46
8383
|
8484
38 | println!("{bar} {foo}", foo="hello", bar="world");

tests/ui/print_with_newline.stderr

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@ error: using `print!()` with a format string that ends in a newline, consider us
22
--> $DIR/print_with_newline.rs:7:5
33
|
44
7 | print!("Hello/n");
5-
| ^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D print-with-newline` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
10+
--> $DIR/print_with_newline.rs:8:5
11+
|
12+
8 | print!("Hello {}/n", "world");
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
16+
--> $DIR/print_with_newline.rs:9:5
17+
|
18+
9 | print!("Hello {} {}/n/n", "world", "#2");
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
22+
--> $DIR/print_with_newline.rs:10:5
23+
|
24+
10 | print!("{}/n", 1265);
25+
| ^^^^^^^^^^^^^^^^^^^^
26+
27+
error: aborting due to 4 previous errors
1028

0 commit comments

Comments
 (0)