Skip to content

Commit b64d21d

Browse files
committed
Auto merge of #6079 - giraffate:print_stdout_in_build_rs, r=ebroto
Fix FP in `print_stdout` Fix #6041 This lint shouldn't be emitted in `build.rs` as `println!` and `print!` are used for the build script. changelog: none
2 parents 6e5306d + 1214a85 commit b64d21d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

clippy_lints/src/write.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,19 @@ impl EarlyLintPass for Write {
235235
}
236236

237237
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
238+
fn is_build_script(cx: &EarlyContext<'_>) -> bool {
239+
// Cargo sets the crate name for build scripts to `build_script_build`
240+
cx.sess
241+
.opts
242+
.crate_name
243+
.as_ref()
244+
.map_or(false, |crate_name| crate_name == "build_script_build")
245+
}
246+
238247
if mac.path == sym!(println) {
239-
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
248+
if !is_build_script(cx) {
249+
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
250+
}
240251
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
241252
if fmt_str.symbol == Symbol::intern("") {
242253
span_lint_and_sugg(
@@ -251,7 +262,9 @@ impl EarlyLintPass for Write {
251262
}
252263
}
253264
} else if mac.path == sym!(print) {
254-
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
265+
if !is_build_script(cx) {
266+
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
267+
}
255268
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
256269
if check_newlines(&fmt_str) {
257270
span_lint_and_then(

clippy_workspace_tests/build.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![deny(clippy::print_stdout)]
2+
3+
fn main() {
4+
// Test for #6041
5+
println!("Hello");
6+
print!("Hello");
7+
}

tests/ui/print_stdout_build_script.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags: --crate-name=build_script_build
2+
3+
#![warn(clippy::print_stdout)]
4+
5+
fn main() {
6+
// Fix #6041
7+
//
8+
// The `print_stdout` lint shouldn't emit in `build.rs`
9+
// as these methods are used for the build script.
10+
println!("Hello");
11+
print!("Hello");
12+
}

0 commit comments

Comments
 (0)