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} ;
22use if_chain:: if_chain;
33use rustc:: hir:: * ;
44use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
55use rustc:: ty:: Ty ;
66use rustc:: { declare_lint_pass, declare_tool_lint} ;
77use rustc_errors:: Applicability ;
8- use syntax:: source_map:: Span ;
98
109declare_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