@@ -2,7 +2,6 @@ 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;
65use rustc_ast:: ast:: { Expr , ExprKind , Item , ItemKind , MacCall , StrLit , StrStyle } ;
76use rustc_ast:: token;
87use rustc_ast:: tokenstream:: TokenStream ;
@@ -12,7 +11,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
1211use rustc_parse:: parser;
1312use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
1413use rustc_span:: symbol:: Symbol ;
15- use rustc_span:: { BytePos , FileName , Span } ;
14+ use rustc_span:: { BytePos , Span } ;
1615
1716declare_clippy_lint ! {
1817 /// **What it does:** This lint warns when you use `println!("")` to
@@ -236,15 +235,19 @@ impl EarlyLintPass for Write {
236235 }
237236
238237 fn check_mac ( & mut self , cx : & EarlyContext < ' _ > , mac : & MacCall ) {
238+ fn is_build_scripts ( cx : & EarlyContext < ' _ > ) -> bool {
239+ // We could leverage the fact that Cargo sets the crate name
240+ // for build scripts to `build_script_build`.
241+ cx. sess
242+ . opts
243+ . crate_name
244+ . as_ref ( )
245+ . map_or ( false , |crate_name| crate_name == "build_script_build" )
246+ }
247+
239248 if mac. path == sym ! ( 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- }
249+ if !is_build_scripts ( cx) {
250+ span_lint ( cx, PRINT_STDOUT , mac. span ( ) , "use of `println!`" ) ;
248251 }
249252 if let ( Some ( fmt_str) , _) = self . check_tts ( cx, mac. args . inner_tokens ( ) , false ) {
250253 if fmt_str. symbol == Symbol :: intern ( "" ) {
@@ -260,14 +263,8 @@ impl EarlyLintPass for Write {
260263 }
261264 }
262265 } else if mac. path == sym ! ( 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- }
266+ if !is_build_scripts ( cx) {
267+ span_lint ( cx, PRINT_STDOUT , mac. span ( ) , "use of `print!`" ) ;
271268 }
272269 if let ( Some ( fmt_str) , _) = self . check_tts ( cx, mac. args . inner_tokens ( ) , false ) {
273270 if check_newlines ( & fmt_str) {
0 commit comments