about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2019-08-08 19:33:34 +0700
committerLzu Tao <taolzu@gmail.com>2019-08-08 21:21:59 +0700
commit90a7b6041319085634666d1ca1a28d79bd7ba6cd (patch)
tree72cd855c34841c02acb64bbe04aaf75ff70168a4
parent0487b58f9afc249e850dc1bf2c8ae6d660237244 (diff)
downloadrust-90a7b6041319085634666d1ca1a28d79bd7ba6cd.tar.gz
rust-90a7b6041319085634666d1ca1a28d79bd7ba6cd.zip
Use snippet_with_macro_callsite suggested by flip1995
-rw-r--r--clippy_lints/src/try_err.rs21
1 files changed, 6 insertions, 15 deletions
diff --git a/clippy_lints/src/try_err.rs b/clippy_lints/src/try_err.rs
index 3734b609861..7eba331ae0f 100644
--- a/clippy_lints/src/try_err.rs
+++ b/clippy_lints/src/try_err.rs
@@ -1,11 +1,10 @@
-use crate::utils::{in_macro_or_desugar, 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};
 use rustc::ty::Ty;
 use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
-use syntax::source_map::Span;
 
 declare_clippy_lint! {
     /// **What it does:** Checks for usages of `Err(x)?`.
@@ -68,15 +67,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
 
             then {
                 let err_type = cx.tables.expr_ty(err_arg);
-                let span = if in_macro_or_desugar(err_arg.span) {
-                    span_to_outer_expn(err_arg.span)
+                let origin_snippet = if in_macro_or_desugar(err_arg.span) {
+                    snippet_with_macro_callsite(cx, err_arg.span, "_")
                 } else {
-                    err_arg.span
+                    snippet(cx, err_arg.span, "_")
                 };
                 let suggestion = if err_type == return_type {
-                    format!("return Err({})", snippet(cx, span, "_"))
+                    format!("return Err({})", origin_snippet)
                 } else {
-                    format!("return Err({}.into())", snippet(cx, span, "_"))
+                    format!("return Err({}.into())", origin_snippet)
                 };
 
                 span_lint_and_sugg(
@@ -93,14 +92,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
     }
 }
 
-fn span_to_outer_expn(span: Span) -> Span {
-    let mut span = span;
-    while let Some(expr) = span.ctxt().outer_expn_info() {
-        span = expr.call_site;
-    }
-    span
-}
-
 // In order to determine whether to suggest `.into()` or not, we need to find the error type the
 // function returns. To do that, we look for the From::from call (see tree above), and capture
 // its output type.