Skip to content

print_with_newline / write_with_newline: don't warn about string with several \ns in them. #3138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::{snippet, span_lint, span_lint_and_sugg};
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
use std::borrow::Cow;
use syntax::ast::*;
use syntax::parse::{parser, token};
use syntax::tokenstream::{ThinTokenStream, TokenStream};
use syntax::parse::{token, parser};
use std::borrow::Cow;
use crate::utils::{span_lint, span_lint_and_sugg, snippet};

/// **What it does:** This lint warns when you use `println!("")` to
/// print a newline.
Expand Down Expand Up @@ -195,25 +195,41 @@ impl EarlyLintPass for Pass {
} else if mac.node.path == "print" {
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
span_lint(cx, PRINT_WITH_NEWLINE, mac.span,
"using `print!()` with a format string that ends in a \
single newline, consider using `println!()` instead");
if fmtstr.ends_with("\\n") &&
// don't warn about strings with several `\n`s (#3126)
fmtstr.matches("\\n").count() == 1
{
span_lint(
cx,
PRINT_WITH_NEWLINE,
mac.span,
"using `print!()` with a format string that ends in a \
single newline, consider using `println!()` instead",
);
}
}
} else if mac.node.path == "write" {
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true).0 {
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
span_lint(cx, WRITE_WITH_NEWLINE, mac.span,
"using `write!()` with a format string that ends in a \
single newline, consider using `writeln!()` instead");
if fmtstr.ends_with("\\n") &&
// don't warn about strings with several `\n`s (#3126)
fmtstr.matches("\\n").count() == 1
{
span_lint(
cx,
WRITE_WITH_NEWLINE,
mac.span,
"using `write!()` with a format string that ends in a \
single newline, consider using `writeln!()` instead",
);
}
}
} else if mac.node.path == "writeln" {
let check_tts = check_tts(cx, &mac.node.tts, true);
if let Some(fmtstr) = check_tts.0 {
if fmtstr == "" {
let suggestion = check_tts.1.map_or(Cow::Borrowed("v"), |expr| snippet(cx, expr.span, "v"));
let suggestion = check_tts
.1
.map_or(Cow::Borrowed("v"), |expr| snippet(cx, expr.span, "v"));

span_lint_and_sugg(
cx,
Expand All @@ -231,13 +247,7 @@ impl EarlyLintPass for Pass {

fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -> (Option<String>, Option<Expr>) {
let tts = TokenStream::from(tts.clone());
let mut parser = parser::Parser::new(
&cx.sess.parse_sess,
tts,
None,
false,
false,
);
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false);
let mut expr: Option<Expr> = None;
if is_write {
expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
Expand Down Expand Up @@ -270,11 +280,7 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -
args.push(arg);
}
}
let lint = if is_write {
WRITE_LITERAL
} else {
PRINT_LITERAL
};
let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
let mut idx = 0;
loop {
if !parser.eat(&token::Comma) {
Expand All @@ -299,9 +305,7 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -
let mut seen = false;
for arg in &args {
match arg.position {
| ArgumentImplicitlyIs(n)
| ArgumentIs(n)
=> if n == idx {
ArgumentImplicitlyIs(n) | ArgumentIs(n) => if n == idx {
all_simple &= arg.format == SIMPLE;
seen = true;
},
Expand All @@ -320,9 +324,7 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -
let mut seen = false;
for arg in &args {
match arg.position {
| ArgumentImplicitlyIs(_)
| ArgumentIs(_)
=> {},
ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
ArgumentNamed(name) => if *p == name {
seen = true;
all_simple &= arg.format == SIMPLE;
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/print_with_newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ fn main() {
print!("\n\n");
print!("like eof\n\n");
print!("Hello {} {}\n\n", "world", "#2");
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
println!("\nbla\n\n"); // #3126
}
2 changes: 2 additions & 0 deletions tests/ui/write_with_newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ fn main() {
write!(&mut v, "\n\n");
write!(&mut v, "like eof\n\n");
write!(&mut v, "Hello {} {}\n\n", "world", "#2");
writeln!(&mut v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
writeln!(&mut v, "\nbla\n\n"); // #3126
}