Fix FP with `print_with_newline` and final arguments

This commit is contained in:
mcarton 2016-10-06 22:30:03 +02:00
parent a4035aabf3
commit 47c2271497
No known key found for this signature in database
GPG Key ID: 5E427C794CBA45E8
2 changed files with 17 additions and 3 deletions

View File

@ -84,17 +84,27 @@ impl LateLintPass for Pass {
// Check print! with format string ending in "\n".
if_let_chain!{[
name == "print",
// ensure we're calling Arguments::new_v1
args.len() == 1,
let ExprCall(ref args_fun, ref args_args) = args[0].node,
let ExprPath(_, ref args_path) = args_fun.node,
match_path(args_path, &paths::FMT_ARGUMENTS_NEWV1),
args_args.len() == 2,
let ExprAddrOf(_, ref match_expr) = args_args[1].node,
let ExprMatch(ref args, _, _) = match_expr.node,
let ExprTup(ref args) = args.node,
// collect the format string parts and check the last one
let Some(fmtstrs) = get_argument_fmtstr_parts(cx, &args_args[0]),
let Some(last_str) = fmtstrs.last(),
let Some(last_chr) = last_str.chars().last(),
last_chr == '\n'
let Some('\n') = last_str.chars().last(),
// "foo{}bar" is made into two strings + one argument,
// if the format string starts with `{}` (eg. "{}foo"),
// the string array is prepended an empty string "".
// We only want to check the last string after any `{}`:
args.len() < fmtstrs.len(),
], {
span_lint(cx, PRINT_WITH_NEWLINE, span,
"using `print!()` with a format string that ends in a \

View File

@ -3,13 +3,17 @@
#![deny(print_with_newline)]
fn main() {
print!("Hello");
print!("Hello\n"); //~ERROR using `print!()` with a format string
print!("Hello {}\n", "world"); //~ERROR using `print!()` with a format string
print!("Hello {} {}\n\n", "world", "#2"); //~ERROR using `print!()` with a format string
print!("{}\n", 1265); //~ERROR using `print!()` with a format string
// these are all fine
print!("");
print!("Hello");
println!("Hello");
println!("Hello\n");
println!("Hello {}\n", "world");
print!("Issue\n{}", 1265);
print!("{}", 1265);
}