]> git.lizzy.rs Git - rust.git/commitdiff
Fix FP in `print_stdout`
authorTakayuki Nakata <f.seasons017@gmail.com>
Thu, 24 Sep 2020 14:22:54 +0000 (23:22 +0900)
committerTakayuki Nakata <f.seasons017@gmail.com>
Thu, 24 Sep 2020 14:22:54 +0000 (23:22 +0900)
This lint shouldn't be emitted in `build.rs` as `println!` and `print!` are used for the build script.

clippy_lints/src/write.rs
tests/ui/build.rs [new file with mode: 0644]
tests/ui/build.stderr [new file with mode: 0644]

index fac63bcb9937838cb2020409275ec7f889d44db5..780d474ee96999948d5e2371ac9e98f06bda638c 100644 (file)
@@ -2,6 +2,7 @@
 use std::ops::Range;
 
 use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
+use if_chain::if_chain;
 use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, MacCall, StrLit, StrStyle};
 use rustc_ast::token;
 use rustc_ast::tokenstream::TokenStream;
@@ -11,7 +12,7 @@
 use rustc_parse::parser;
 use rustc_session::{declare_tool_lint, impl_lint_pass};
 use rustc_span::symbol::Symbol;
-use rustc_span::{BytePos, Span};
+use rustc_span::{BytePos, FileName, Span};
 
 declare_clippy_lint! {
     /// **What it does:** This lint warns when you use `println!("")` to
@@ -236,7 +237,15 @@ fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &Item) {
 
     fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
         if mac.path == sym!(println) {
-            span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
+            let filename = cx.sess.source_map().span_to_filename(mac.span());
+            if_chain! {
+                if let FileName::Real(filename) = filename;
+                if let Some(filename) = filename.local_path().file_name();
+                if filename != "build.rs";
+                then {
+                    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 +260,15 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
                 }
             }
         } else if mac.path == sym!(print) {
-            span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
+            if_chain! {
+                let filename = cx.sess.source_map().span_to_filename(mac.span());
+                if let FileName::Real(filename) = filename;
+                if let Some(filename) = filename.local_path().file_name();
+                if filename != "build.rs";
+                then {
+                    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(
diff --git a/tests/ui/build.rs b/tests/ui/build.rs
new file mode 100644 (file)
index 0000000..2d43d45
--- /dev/null
@@ -0,0 +1,10 @@
+#![warn(clippy::print_stdout)]
+
+fn main() {
+    // Fix #6041
+    //
+    // The `print_stdout` shouldn't be linted in `build.rs`
+    // as these methods are used for the build script.
+    println!("Hello");
+    print!("Hello");
+}
diff --git a/tests/ui/build.stderr b/tests/ui/build.stderr
new file mode 100644 (file)
index 0000000..e69de29