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} ;
2
2
use if_chain:: if_chain;
3
3
use rustc:: hir:: * ;
4
4
use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
5
5
use rustc:: ty:: Ty ;
6
6
use rustc:: { declare_lint_pass, declare_tool_lint} ;
7
7
use rustc_errors:: Applicability ;
8
- use syntax:: source_map:: Span ;
9
8
10
9
declare_clippy_lint ! {
11
10
/// **What it does:** Checks for usages of `Err(x)?`.
@@ -68,15 +67,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
68
67
69
68
then {
70
69
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, "_" )
73
72
} else {
74
- err_arg. span
73
+ snippet ( cx , err_arg. span, "_" )
75
74
} ;
76
75
let suggestion = if err_type == return_type {
77
- format!( "return Err({})" , snippet ( cx , span , "_" ) )
76
+ format!( "return Err({})" , origin_snippet )
78
77
} else {
79
- format!( "return Err({}.into())" , snippet ( cx , span , "_" ) )
78
+ format!( "return Err({}.into())" , origin_snippet )
80
79
} ;
81
80
82
81
span_lint_and_sugg(
@@ -93,14 +92,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
93
92
}
94
93
}
95
94
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
-
104
95
// In order to determine whether to suggest `.into()` or not, we need to find the error type the
105
96
// function returns. To do that, we look for the From::from call (see tree above), and capture
106
97
// its output type.
0 commit comments