Skip to content

Commit 47c2271

Browse files
committed
Fix FP with print_with_newline and final arguments
1 parent a4035aa commit 47c2271

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clippy_lints/src/print.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,27 @@ impl LateLintPass for Pass {
8484
// Check print! with format string ending in "\n".
8585
if_let_chain!{[
8686
name == "print",
87+
8788
// ensure we're calling Arguments::new_v1
8889
args.len() == 1,
8990
let ExprCall(ref args_fun, ref args_args) = args[0].node,
9091
let ExprPath(_, ref args_path) = args_fun.node,
9192
match_path(args_path, &paths::FMT_ARGUMENTS_NEWV1),
9293
args_args.len() == 2,
94+
let ExprAddrOf(_, ref match_expr) = args_args[1].node,
95+
let ExprMatch(ref args, _, _) = match_expr.node,
96+
let ExprTup(ref args) = args.node,
97+
9398
// collect the format string parts and check the last one
9499
let Some(fmtstrs) = get_argument_fmtstr_parts(cx, &args_args[0]),
95100
let Some(last_str) = fmtstrs.last(),
96-
let Some(last_chr) = last_str.chars().last(),
97-
last_chr == '\n'
101+
let Some('\n') = last_str.chars().last(),
102+
103+
// "foo{}bar" is made into two strings + one argument,
104+
// if the format string starts with `{}` (eg. "{}foo"),
105+
// the string array is prepended an empty string "".
106+
// We only want to check the last string after any `{}`:
107+
args.len() < fmtstrs.len(),
98108
], {
99109
span_lint(cx, PRINT_WITH_NEWLINE, span,
100110
"using `print!()` with a format string that ends in a \

tests/compile-fail/print_with_newline.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
#![deny(print_with_newline)]
44

55
fn main() {
6-
print!("Hello");
76
print!("Hello\n"); //~ERROR using `print!()` with a format string
87
print!("Hello {}\n", "world"); //~ERROR using `print!()` with a format string
98
print!("Hello {} {}\n\n", "world", "#2"); //~ERROR using `print!()` with a format string
9+
print!("{}\n", 1265); //~ERROR using `print!()` with a format string
1010

1111
// these are all fine
12+
print!("");
13+
print!("Hello");
1214
println!("Hello");
1315
println!("Hello\n");
1416
println!("Hello {}\n", "world");
17+
print!("Issue\n{}", 1265);
18+
print!("{}", 1265);
1519
}

0 commit comments

Comments
 (0)