Skip to content

Commit 90a7b60

Browse files
committed
Use snippet_with_macro_callsite suggested by flip1995
1 parent 0487b58 commit 90a7b60

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

clippy_lints/src/try_err.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::utils::{in_macro_or_desugar, match_qpath, paths, snippet, span_lint_and_sugg};
1+
use crate::utils::{in_macro_or_desugar, match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc::hir::*;
44
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
55
use rustc::ty::Ty;
66
use rustc::{declare_lint_pass, declare_tool_lint};
77
use rustc_errors::Applicability;
8-
use syntax::source_map::Span;
98

109
declare_clippy_lint! {
1110
/// **What it does:** Checks for usages of `Err(x)?`.
@@ -68,15 +67,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
6867

6968
then {
7069
let err_type = cx.tables.expr_ty(err_arg);
71-
let span = if in_macro_or_desugar(err_arg.span) {
72-
span_to_outer_expn(err_arg.span)
70+
let origin_snippet = if in_macro_or_desugar(err_arg.span) {
71+
snippet_with_macro_callsite(cx, err_arg.span, "_")
7372
} else {
74-
err_arg.span
73+
snippet(cx, err_arg.span, "_")
7574
};
7675
let suggestion = if err_type == return_type {
77-
format!("return Err({})", snippet(cx, span, "_"))
76+
format!("return Err({})", origin_snippet)
7877
} else {
79-
format!("return Err({}.into())", snippet(cx, span, "_"))
78+
format!("return Err({}.into())", origin_snippet)
8079
};
8180

8281
span_lint_and_sugg(
@@ -93,14 +92,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
9392
}
9493
}
9594

96-
fn span_to_outer_expn(span: Span) -> Span {
97-
let mut span = span;
98-
while let Some(expr) = span.ctxt().outer_expn_info() {
99-
span = expr.call_site;
100-
}
101-
span
102-
}
103-
10495
// In order to determine whether to suggest `.into()` or not, we need to find the error type the
10596
// function returns. To do that, we look for the From::from call (see tree above), and capture
10697
// its output type.

0 commit comments

Comments
 (0)