@@ -2,16 +2,16 @@ use clippy_config::Conf;
2
2
use clippy_utils:: diagnostics:: span_lint_and_then;
3
3
use clippy_utils:: eager_or_lazy:: switch_to_eager_eval;
4
4
use clippy_utils:: msrvs:: { self , Msrv } ;
5
- use clippy_utils:: source:: snippet_with_context;
5
+ use clippy_utils:: source:: { snippet_with_applicability , snippet_with_context, walk_span_to_context } ;
6
6
use clippy_utils:: sugg:: Sugg ;
7
7
use clippy_utils:: {
8
8
contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context, is_res_lang_ctor,
9
- path_res, peel_blocks,
9
+ path_res, peel_blocks, sym ,
10
10
} ;
11
11
use rustc_errors:: Applicability ;
12
12
use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
13
13
use rustc_hir:: { Expr , ExprKind } ;
14
- use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
14
+ use rustc_lint:: { LateContext , LateLintPass } ;
15
15
use rustc_session:: impl_lint_pass;
16
16
17
17
declare_clippy_lint ! {
@@ -71,21 +71,21 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
71
71
&& let ExprKind :: Block ( then_block, _) = then. kind
72
72
&& let Some ( then_expr) = then_block. expr
73
73
&& let ExprKind :: Call ( then_call, [ then_arg] ) = then_expr. kind
74
- && let ctxt = expr. span . ctxt ( )
75
- && then_expr. span . ctxt ( ) == ctxt
74
+ && ! expr. span . from_expansion ( )
75
+ && ! then_expr. span . from_expansion ( )
76
76
&& is_res_lang_ctor ( cx, path_res ( cx, then_call) , OptionSome )
77
77
&& is_res_lang_ctor ( cx, path_res ( cx, peel_blocks ( els) ) , OptionNone )
78
78
&& !is_else_clause ( cx. tcx , expr)
79
79
&& !is_in_const_context ( cx)
80
- && !expr. span . in_external_macro ( cx. sess ( ) . source_map ( ) )
81
80
&& self . msrv . meets ( cx, msrvs:: BOOL_THEN )
82
81
&& !contains_return ( then_block. stmts )
83
82
{
84
83
let method_name = if switch_to_eager_eval ( cx, expr) && self . msrv . meets ( cx, msrvs:: BOOL_THEN_SOME ) {
85
- " then_some"
84
+ sym :: then_some
86
85
} else {
87
- " then"
86
+ sym :: then
88
87
} ;
88
+ let ctxt = expr. span . ctxt ( ) ;
89
89
90
90
span_lint_and_then (
91
91
cx,
@@ -98,16 +98,18 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
98
98
}
99
99
100
100
let mut app = Applicability :: MachineApplicable ;
101
- let cond_snip = Sugg :: hir_with_context ( cx, cond, expr . span . ctxt ( ) , "[condition]" , & mut app)
101
+ let cond_snip = Sugg :: hir_with_context ( cx, cond, ctxt, "[condition]" , & mut app)
102
102
. maybe_paren ( )
103
103
. to_string ( ) ;
104
104
let arg_snip = snippet_with_context ( cx, then_arg. span , ctxt, "[body]" , & mut app) . 0 ;
105
- let method_body = if let Some ( first_stmt) = then_block. stmts . first ( ) {
106
- let ( block_snippet, _) =
107
- snippet_with_context ( cx, first_stmt. span . until ( then_arg. span ) , ctxt, ".." , & mut app) ;
108
- let closure = if method_name == "then" { "|| " } else { "" } ;
109
- format ! ( "{closure} {{ {block_snippet}; {arg_snip} }}" )
110
- } else if method_name == "then" {
105
+ let method_body = if let Some ( first_stmt) = then_block. stmts . first ( )
106
+ && let Some ( first_stmt_span) = walk_span_to_context ( first_stmt. span , ctxt)
107
+ {
108
+ let block_snippet =
109
+ snippet_with_applicability ( cx, first_stmt_span. until ( then_expr. span ) , ".." , & mut app) ;
110
+ let closure = if method_name == sym:: then { "|| " } else { "" } ;
111
+ format ! ( "{closure} {{ {} {arg_snip} }}" , block_snippet. trim_end( ) )
112
+ } else if method_name == sym:: then {
111
113
( std:: borrow:: Cow :: Borrowed ( "|| " ) + arg_snip) . into_owned ( )
112
114
} else {
113
115
arg_snip. into_owned ( )
0 commit comments