@@ -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.
0 commit comments