Skip to content

Commit 3187cad

Browse files
committed
Factor out some code in write.rs
Get rid of the too-many-lines error.
1 parent 7063c36 commit 3187cad

File tree

4 files changed

+77
-61
lines changed

4 files changed

+77
-61
lines changed

clippy_lints/src/write.rs

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -262,71 +262,22 @@ impl EarlyLintPass for Write {
262262
.map_or(false, |crate_name| crate_name == "build_script_build")
263263
}
264264

265-
if mac.path == sym!(println) {
265+
if mac.path == sym!(print) {
266266
if !is_build_script(cx) {
267-
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
267+
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
268268
}
269-
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
270-
if fmt_str.symbol == Symbol::intern("") {
271-
span_lint_and_sugg(
272-
cx,
273-
PRINTLN_EMPTY_STRING,
274-
mac.span(),
275-
"using `println!(\"\")`",
276-
"replace it with",
277-
"println!()".to_string(),
278-
Applicability::MachineApplicable,
279-
);
280-
}
269+
self.lint_print_with_newline(cx, mac);
270+
} else if mac.path == sym!(println) {
271+
if !is_build_script(cx) {
272+
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
281273
}
282-
} else if mac.path == sym!(eprintln) {
283-
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
274+
self.lint_println_empty_string(cx, mac);
284275
} else if mac.path == sym!(eprint) {
285276
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprint!`");
286-
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
287-
if check_newlines(&fmt_str) {
288-
span_lint_and_then(
289-
cx,
290-
PRINT_WITH_NEWLINE,
291-
mac.span(),
292-
"using `eprint!()` with a format string that ends in a single newline",
293-
|err| {
294-
err.multipart_suggestion(
295-
"use `eprintln!` instead",
296-
vec![
297-
(mac.path.span, String::from("eprintln")),
298-
(newline_span(&fmt_str), String::new()),
299-
],
300-
Applicability::MachineApplicable,
301-
);
302-
},
303-
);
304-
}
305-
}
306-
} else if mac.path == sym!(print) {
307-
if !is_build_script(cx) {
308-
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
309-
}
310-
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
311-
if check_newlines(&fmt_str) {
312-
span_lint_and_then(
313-
cx,
314-
PRINT_WITH_NEWLINE,
315-
mac.span(),
316-
"using `print!()` with a format string that ends in a single newline",
317-
|err| {
318-
err.multipart_suggestion(
319-
"use `println!` instead",
320-
vec![
321-
(mac.path.span, String::from("println")),
322-
(newline_span(&fmt_str), String::new()),
323-
],
324-
Applicability::MachineApplicable,
325-
);
326-
},
327-
);
328-
}
329-
}
277+
self.lint_print_with_newline(cx, mac);
278+
} else if mac.path == sym!(eprintln) {
279+
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
280+
self.lint_println_empty_string(cx, mac);
330281
} else if mac.path == sym!(write) {
331282
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), true) {
332283
if check_newlines(&fmt_str) {
@@ -530,6 +481,45 @@ impl Write {
530481
}
531482
}
532483
}
484+
485+
fn lint_println_empty_string(&self, cx: &EarlyContext<'_>, mac: &MacCall) {
486+
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
487+
if fmt_str.symbol == Symbol::intern("") {
488+
let name = mac.path.segments[0].ident.name;
489+
span_lint_and_sugg(
490+
cx,
491+
PRINTLN_EMPTY_STRING,
492+
mac.span(),
493+
&format!("using `{}!(\"\")`", name),
494+
"replace it with",
495+
format!("{}!()", name),
496+
Applicability::MachineApplicable,
497+
);
498+
}
499+
}
500+
}
501+
502+
fn lint_print_with_newline(&self, cx: &EarlyContext<'_>, mac: &MacCall) {
503+
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
504+
if check_newlines(&fmt_str) {
505+
let name = mac.path.segments[0].ident.name;
506+
let suggested = format!("{}ln", name);
507+
span_lint_and_then(
508+
cx,
509+
PRINT_WITH_NEWLINE,
510+
mac.span(),
511+
&format!("using `{}!()` with a format string that ends in a single newline", name),
512+
|err| {
513+
err.multipart_suggestion(
514+
&format!("use `{}!` instead", suggested),
515+
vec![(mac.path.span, suggested), (newline_span(&fmt_str), String::new())],
516+
Applicability::MachineApplicable,
517+
);
518+
},
519+
);
520+
}
521+
}
522+
}
533523
}
534524

535525
/// Checks if the format string contains a single newline that terminates it.

tests/ui/println_empty_string.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ fn main() {
88
match "a" {
99
_ => println!(),
1010
}
11+
12+
eprintln!();
13+
eprintln!();
14+
15+
match "a" {
16+
_ => eprintln!(),
17+
}
1118
}

tests/ui/println_empty_string.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ fn main() {
88
match "a" {
99
_ => println!(""),
1010
}
11+
12+
eprintln!();
13+
eprintln!("");
14+
15+
match "a" {
16+
_ => eprintln!(""),
17+
}
1118
}

tests/ui/println_empty_string.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,17 @@ error: using `println!("")`
1212
LL | _ => println!(""),
1313
| ^^^^^^^^^^^^ help: replace it with: `println!()`
1414

15-
error: aborting due to 2 previous errors
15+
error: using `eprintln!("")`
16+
--> $DIR/println_empty_string.rs:13:5
17+
|
18+
LL | eprintln!("");
19+
| ^^^^^^^^^^^^^ help: replace it with: `eprintln!()`
20+
21+
error: using `eprintln!("")`
22+
--> $DIR/println_empty_string.rs:16:14
23+
|
24+
LL | _ => eprintln!(""),
25+
| ^^^^^^^^^^^^^ help: replace it with: `eprintln!()`
26+
27+
error: aborting due to 4 previous errors
1628

0 commit comments

Comments
 (0)