Skip to content

Commit 588dcc8

Browse files
committed
wip fix for rust-lang#4208
1 parent be5d17f commit 588dcc8

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

clippy_lints/src/write.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl EarlyLintPass for Write {
200200
} else if mac.node.path == sym!(print) {
201201
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
202202
if let (Some(fmt_str), _) = check_tts(cx, &mac.node.tts, false) {
203-
if check_newlines(&fmt_str) {
203+
if terminating_newline(&fmt_str) {
204204
span_lint_and_then(
205205
cx,
206206
PRINT_WITH_NEWLINE,
@@ -221,7 +221,7 @@ impl EarlyLintPass for Write {
221221
}
222222
} else if mac.node.path == sym!(write) {
223223
if let (Some(fmt_str), _) = check_tts(cx, &mac.node.tts, true) {
224-
if check_newlines(&fmt_str) {
224+
if terminating_newline(&fmt_str) {
225225
span_lint_and_then(
226226
cx,
227227
WRITE_WITH_NEWLINE,
@@ -439,10 +439,15 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (O
439439
/// Checks if the format string constains a single newline that terminates it.
440440
///
441441
/// Literal and escaped newlines are both checked (only literal for raw strings).
442-
fn check_newlines(fmt_str: &FmtStr) -> bool {
442+
fn terminating_newline(fmt_str: &FmtStr) -> bool {
443443
let s = &fmt_str.contents;
444444

445445
if s.ends_with('\n') {
446+
let last_but_one = s.chars().rev().nth(1);
447+
if last_but_one == Some('\r') {
448+
return false
449+
}
450+
446451
return true;
447452
} else if let StrStyle::Raw(_) = fmt_str.style {
448453
return false;

tests/ui/write_with_newline.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ fn main() {
2929
write!(&mut v, "Hello {} {}\n\n", "world", "#2");
3030
writeln!(&mut v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
3131
writeln!(&mut v, "\nbla\n\n"); // #3126
32+
write!(&mut v, "\r\n"); // 4208
33+
write!(&mut v, "foobar\r\n"); // 4208
3234

3335
// Escaping
3436
write!(&mut v, "\\n"); // #3514
@@ -49,4 +51,5 @@ fn main() {
4951
r"
5052
"
5153
);
54+
5255
}

0 commit comments

Comments
 (0)