@@ -2,6 +2,7 @@ use std::borrow::Cow;
22use std:: ops:: Range ;
33
44use crate :: utils:: { snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then} ;
5+ use if_chain:: if_chain;
56use rustc_ast:: ast:: { Expr , ExprKind , Item , ItemKind , MacCall , StrLit , StrStyle } ;
67use rustc_ast:: token;
78use rustc_ast:: tokenstream:: TokenStream ;
@@ -11,7 +12,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
1112use rustc_parse:: parser;
1213use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
1314use rustc_span:: symbol:: Symbol ;
14- use rustc_span:: { BytePos , Span } ;
15+ use rustc_span:: { BytePos , FileName , Span } ;
1516
1617declare_clippy_lint ! {
1718 /// **What it does:** This lint warns when you use `println!("")` to
@@ -236,7 +237,15 @@ impl EarlyLintPass for Write {
236237
237238 fn check_mac ( & mut self , cx : & EarlyContext < ' _ > , mac : & MacCall ) {
238239 if mac. path == sym ! ( println) {
239- span_lint ( cx, PRINT_STDOUT , mac. span ( ) , "use of `println!`" ) ;
240+ let filename = cx. sess . source_map ( ) . span_to_filename ( mac. span ( ) ) ;
241+ if_chain ! {
242+ if let FileName :: Real ( filename) = filename;
243+ if let Some ( filename) = filename. local_path( ) . file_name( ) ;
244+ if filename != "build.rs" ;
245+ then {
246+ span_lint( cx, PRINT_STDOUT , mac. span( ) , "use of `println!`" ) ;
247+ }
248+ }
240249 if let ( Some ( fmt_str) , _) = self . check_tts ( cx, mac. args . inner_tokens ( ) , false ) {
241250 if fmt_str. symbol == Symbol :: intern ( "" ) {
242251 span_lint_and_sugg (
@@ -251,7 +260,15 @@ impl EarlyLintPass for Write {
251260 }
252261 }
253262 } else if mac. path == sym ! ( print) {
254- span_lint ( cx, PRINT_STDOUT , mac. span ( ) , "use of `print!`" ) ;
263+ if_chain ! {
264+ let filename = cx. sess. source_map( ) . span_to_filename( mac. span( ) ) ;
265+ if let FileName :: Real ( filename) = filename;
266+ if let Some ( filename) = filename. local_path( ) . file_name( ) ;
267+ if filename != "build.rs" ;
268+ then {
269+ span_lint( cx, PRINT_STDOUT , mac. span( ) , "use of `print!`" ) ;
270+ }
271+ }
255272 if let ( Some ( fmt_str) , _) = self . check_tts ( cx, mac. args . inner_tokens ( ) , false ) {
256273 if check_newlines ( & fmt_str) {
257274 span_lint_and_then (
0 commit comments