about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-08-08 15:16:17 +0000
committerbors <bors@rust-lang.org>2019-08-08 15:16:17 +0000
commit4465e2fbb801697c74cb39308a18df26062ab1bb (patch)
tree97faa733d29a8f1b91825d0f8a815b1ce50d963a
parent7bd7d40789d510eba782fdf86c4a2fdd90084568 (diff)
parent90a7b6041319085634666d1ca1a28d79bd7ba6cd (diff)
downloadrust-4465e2fbb801697c74cb39308a18df26062ab1bb.tar.gz
rust-4465e2fbb801697c74cb39308a18df26062ab1bb.zip
Auto merge of #4355 - lzutao:macro_expn_try_err, r=flip1995
Fix macro expansion in try_err lint

Fixes #4309

changelog: none
-rw-r--r--clippy_lints/src/try_err.rs11
-rw-r--r--tests/ui/try_err.fixed19
-rw-r--r--tests/ui/try_err.rs19
-rw-r--r--tests/ui/try_err.stderr8
4 files changed, 53 insertions, 4 deletions
diff --git a/clippy_lints/src/try_err.rs b/clippy_lints/src/try_err.rs
index 7466221fb11..7eba331ae0f 100644
--- a/clippy_lints/src/try_err.rs
+++ b/clippy_lints/src/try_err.rs
@@ -1,4 +1,4 @@
-use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
+use crate::utils::{in_macro_or_desugar, match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg};
 use if_chain::if_chain;
 use rustc::hir::*;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -67,10 +67,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
 
             then {
                 let err_type = cx.tables.expr_ty(err_arg);
+                let origin_snippet = if in_macro_or_desugar(err_arg.span) {
+                    snippet_with_macro_callsite(cx, err_arg.span, "_")
+                } else {
+                    snippet(cx, err_arg.span, "_")
+                };
                 let suggestion = if err_type == return_type {
-                    format!("return Err({})", snippet(cx, err_arg.span, "_"))
+                    format!("return Err({})", origin_snippet)
                 } else {
-                    format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
+                    format!("return Err({}.into())", origin_snippet)
                 };
 
                 span_lint_and_sugg(
diff --git a/tests/ui/try_err.fixed b/tests/ui/try_err.fixed
index 117300e84a4..a2087316e37 100644
--- a/tests/ui/try_err.fixed
+++ b/tests/ui/try_err.fixed
@@ -78,3 +78,22 @@ fn main() {
     closure_matches_test().unwrap();
     closure_into_test().unwrap();
 }
+
+macro_rules! bar {
+    () => {
+        String::from("aasdfasdfasdfa")
+    };
+}
+
+macro_rules! foo {
+    () => {
+        bar!()
+    };
+}
+
+pub fn macro_inside(fail: bool) -> Result<i32, String> {
+    if fail {
+        return Err(foo!());
+    }
+    Ok(0)
+}
diff --git a/tests/ui/try_err.rs b/tests/ui/try_err.rs
index 828cf639a1b..5ef1b615dc7 100644
--- a/tests/ui/try_err.rs
+++ b/tests/ui/try_err.rs
@@ -78,3 +78,22 @@ fn main() {
     closure_matches_test().unwrap();
     closure_into_test().unwrap();
 }
+
+macro_rules! bar {
+    () => {
+        String::from("aasdfasdfasdfa")
+    };
+}
+
+macro_rules! foo {
+    () => {
+        bar!()
+    };
+}
+
+pub fn macro_inside(fail: bool) -> Result<i32, String> {
+    if fail {
+        Err(foo!())?;
+    }
+    Ok(0)
+}
diff --git a/tests/ui/try_err.stderr b/tests/ui/try_err.stderr
index dbe05ce5178..b915d6b601d 100644
--- a/tests/ui/try_err.stderr
+++ b/tests/ui/try_err.stderr
@@ -28,5 +28,11 @@ error: returning an `Err(_)` with the `?` operator
 LL |                 Err(err)?;
    |                 ^^^^^^^^^ help: try this: `return Err(err.into())`
 
-error: aborting due to 4 previous errors
+error: returning an `Err(_)` with the `?` operator
+  --> $DIR/try_err.rs:96:9
+   |
+LL |         Err(foo!())?;
+   |         ^^^^^^^^^^^^ help: try this: `return Err(foo!())`
+
+error: aborting due to 5 previous errors