Skip to content

Commit 2892a2b

Browse files
committed
Fix FP in print_stdout
This lint shouldn't be emitted in `build.rs` as `println!` and `print!` are used for the build script.
1 parent 78fbb04 commit 2892a2b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

clippy_lints/src/write.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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;
56
use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, MacCall, StrLit, StrStyle};
67
use rustc_ast::token;
78
use rustc_ast::tokenstream::TokenStream;
@@ -11,7 +12,7 @@ use rustc_lint::{EarlyContext, EarlyLintPass};
1112
use rustc_parse::parser;
1213
use rustc_session::{declare_tool_lint, impl_lint_pass};
1314
use rustc_span::symbol::Symbol;
14-
use rustc_span::{BytePos, Span};
15+
use rustc_span::{BytePos, FileName, Span};
1516

1617
declare_clippy_lint! {
1718
/// **What it does:** This lint warns when you use `println!("")` to
@@ -236,7 +237,15 @@ impl EarlyLintPass for Write {
236237

237238
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
238239
if mac.path == sym!(println) {
239-
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `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+
}
248+
}
240249
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
241250
if fmt_str.symbol == Symbol::intern("") {
242251
span_lint_and_sugg(
@@ -251,7 +260,15 @@ impl EarlyLintPass for Write {
251260
}
252261
}
253262
} else if mac.path == sym!(print) {
254-
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `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+
}
271+
}
255272
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
256273
if check_newlines(&fmt_str) {
257274
span_lint_and_then(

tests/ui/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::print_stdout)]
2+
3+
fn main() {
4+
// Fix #6041
5+
//
6+
// The `print_stdout` shouldn't be linted in `build.rs`
7+
// as these methods are used for the build script.
8+
println!("Hello");
9+
print!("Hello");
10+
}

tests/ui/build.stderr

Whitespace-only changes.

0 commit comments

Comments
 (0)