@@ -2,16 +2,16 @@ use clippy_config::Conf;
22use clippy_utils:: diagnostics:: span_lint_and_then;
33use clippy_utils:: eager_or_lazy:: switch_to_eager_eval;
44use 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 } ;
66use clippy_utils:: sugg:: Sugg ;
77use clippy_utils:: {
88 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 ,
1010} ;
1111use rustc_errors:: Applicability ;
1212use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
1313use rustc_hir:: { Expr , ExprKind } ;
14- use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
14+ use rustc_lint:: { LateContext , LateLintPass } ;
1515use rustc_session:: impl_lint_pass;
1616
1717declare_clippy_lint ! {
@@ -71,21 +71,21 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7171 && let ExprKind :: Block ( then_block, _) = then. kind
7272 && let Some ( then_expr) = then_block. expr
7373 && 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 ( )
7676 && is_res_lang_ctor ( cx, path_res ( cx, then_call) , OptionSome )
7777 && is_res_lang_ctor ( cx, path_res ( cx, peel_blocks ( els) ) , OptionNone )
7878 && !is_else_clause ( cx. tcx , expr)
7979 && !is_in_const_context ( cx)
80- && !expr. span . in_external_macro ( cx. sess ( ) . source_map ( ) )
8180 && self . msrv . meets ( cx, msrvs:: BOOL_THEN )
8281 && !contains_return ( then_block. stmts )
8382 {
8483 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
8685 } else {
87- " then"
86+ sym :: then
8887 } ;
88+ let ctxt = expr. span . ctxt ( ) ;
8989
9090 span_lint_and_then (
9191 cx,
@@ -98,16 +98,18 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
9898 }
9999
100100 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)
102102 . maybe_paren ( )
103103 . to_string ( ) ;
104104 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 {
111113 ( std:: borrow:: Cow :: Borrowed ( "|| " ) + arg_snip) . into_owned ( )
112114 } else {
113115 arg_snip. into_owned ( )
0 commit comments