@@ -2,7 +2,6 @@ use std::borrow::Cow;
2
2
use std:: ops:: Range ;
3
3
4
4
use crate :: utils:: { snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then} ;
5
- use if_chain:: if_chain;
6
5
use rustc_ast:: ast:: { Expr , ExprKind , Item , ItemKind , MacCall , StrLit , StrStyle } ;
7
6
use rustc_ast:: token;
8
7
use rustc_ast:: tokenstream:: TokenStream ;
@@ -12,7 +11,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
12
11
use rustc_parse:: parser;
13
12
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
14
13
use rustc_span:: symbol:: Symbol ;
15
- use rustc_span:: { BytePos , FileName , Span } ;
14
+ use rustc_span:: { BytePos , Span } ;
16
15
17
16
declare_clippy_lint ! {
18
17
/// **What it does:** This lint warns when you use `println!("")` to
@@ -236,15 +235,19 @@ impl EarlyLintPass for Write {
236
235
}
237
236
238
237
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
+
239
248
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!`" ) ;
248
251
}
249
252
if let ( Some ( fmt_str) , _) = self . check_tts ( cx, mac. args . inner_tokens ( ) , false ) {
250
253
if fmt_str. symbol == Symbol :: intern ( "" ) {
@@ -260,14 +263,8 @@ impl EarlyLintPass for Write {
260
263
}
261
264
}
262
265
} 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!`" ) ;
271
268
}
272
269
if let ( Some ( fmt_str) , _) = self . check_tts ( cx, mac. args . inner_tokens ( ) , false ) {
273
270
if check_newlines ( & fmt_str) {
0 commit comments