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
This commit is contained in:
bors 2020-09-26 20:34:35 +00:00
commit b64d21d980
3 changed files with 34 additions and 2 deletions

View File

@ -235,8 +235,19 @@ impl EarlyLintPass for Write {
}
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
fn is_build_script(cx: &EarlyContext<'_>) -> bool {
// Cargo sets the crate name for build scripts to `build_script_build`
cx.sess
.opts
.crate_name
.as_ref()
.map_or(false, |crate_name| crate_name == "build_script_build")
}
if mac.path == sym!(println) {
if !is_build_script(cx) {
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
}
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
if fmt_str.symbol == Symbol::intern("") {
span_lint_and_sugg(
@ -251,7 +262,9 @@ impl EarlyLintPass for Write {
}
}
} else if mac.path == sym!(print) {
if !is_build_script(cx) {
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
}
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
if check_newlines(&fmt_str) {
span_lint_and_then(

View File

@ -0,0 +1,7 @@
#![deny(clippy::print_stdout)]
fn main() {
// Test for #6041
println!("Hello");
print!("Hello");
}

View File

@ -0,0 +1,12 @@
// compile-flags: --crate-name=build_script_build
#![warn(clippy::print_stdout)]
fn main() {
// Fix #6041
//
// The `print_stdout` lint shouldn't emit in `build.rs`
// as these methods are used for the build script.
println!("Hello");
print!("Hello");
}