Skip to content

Commit 5b484b4

Browse files
committed
Fix the detection of build scripts
1 parent 2892a2b commit 5b484b4

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

clippy_lints/src/write.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22
use std::ops::Range;
33

44
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
5-
use if_chain::if_chain;
65
use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, MacCall, StrLit, StrStyle};
76
use rustc_ast::token;
87
use rustc_ast::tokenstream::TokenStream;
@@ -12,7 +11,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
1211
use rustc_parse::parser;
1312
use rustc_session::{declare_tool_lint, impl_lint_pass};
1413
use rustc_span::symbol::Symbol;
15-
use rustc_span::{BytePos, FileName, Span};
14+
use rustc_span::{BytePos, Span};
1615

1716
declare_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) {

tests/ui/build.rs renamed to tests/ui/print_stdout_build_script.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// compile-flags: --crate-name=build_script_build
2+
13
#![warn(clippy::print_stdout)]
24

35
fn main() {
File renamed without changes.

0 commit comments

Comments
 (0)