@@ -6,14 +6,15 @@ use clippy_utils::{
6
6
SpanlessEq , can_move_expr_to_closure_no_visit, higher, is_expr_final_block_expr, is_expr_used_or_unified,
7
7
peel_hir_expr_while,
8
8
} ;
9
- use core:: fmt:: { self , Write } ;
9
+ use core:: fmt:: Write ;
10
10
use rustc_errors:: Applicability ;
11
11
use rustc_hir:: hir_id:: HirIdSet ;
12
12
use rustc_hir:: intravisit:: { Visitor , walk_body, walk_expr} ;
13
13
use rustc_hir:: { Block , Expr , ExprKind , HirId , Pat , Stmt , StmtKind , UnOp } ;
14
14
use rustc_lint:: { LateContext , LateLintPass } ;
15
15
use rustc_session:: declare_lint_pass;
16
16
use rustc_span:: { DUMMY_SP , Span , SyntaxContext , sym} ;
17
+ use std:: fmt;
17
18
use std:: ops:: ControlFlow ;
18
19
19
20
declare_clippy_lint ! {
@@ -628,11 +629,8 @@ impl<'tcx> InsertSearchResults<'tcx> {
628
629
if is_expr_used_or_unified ( cx. tcx , insertion. call ) {
629
630
write_wrapped ( & mut res, insertion, ctxt, app) ;
630
631
} else {
631
- let _: fmt:: Result = write ! (
632
- res,
633
- "e.insert({})" ,
634
- snippet_with_context( cx, insertion. value. span, ctxt, ".." , app) . 0
635
- ) ;
632
+ let value_str = snippet_with_context ( cx, insertion. value . span , ctxt, ".." , app) . 0 ;
633
+ let _: fmt:: Result = write ! ( res, "e.insert({value_str})" ) ;
636
634
}
637
635
span = span. trim_start ( insertion. call . span ) . unwrap_or ( DUMMY_SP ) ;
638
636
}
@@ -644,11 +642,8 @@ impl<'tcx> InsertSearchResults<'tcx> {
644
642
(
645
643
self . snippet ( cx, span, app, |res, insertion, ctxt, app| {
646
644
// Insertion into a map would return `Some(&mut value)`, but the entry returns `&mut value`
647
- let _: fmt:: Result = write ! (
648
- res,
649
- "Some(e.insert({}))" ,
650
- snippet_with_context( cx, insertion. value. span, ctxt, ".." , app) . 0
651
- ) ;
645
+ let value_str = snippet_with_context ( cx, insertion. value . span , ctxt, ".." , app) . 0 ;
646
+ let _: fmt:: Result = write ! ( res, "Some(e.insert({value_str}))" ) ;
652
647
} ) ,
653
648
"Occupied(mut e)" ,
654
649
)
@@ -658,19 +653,15 @@ impl<'tcx> InsertSearchResults<'tcx> {
658
653
(
659
654
self . snippet ( cx, span, app, |res, insertion, ctxt, app| {
660
655
// Insertion into a map would return `None`, but the entry returns a mutable reference.
656
+ let value_str = snippet_with_context ( cx, insertion. value . span , ctxt, ".." , app) . 0 ;
661
657
let _: fmt:: Result = if is_expr_final_block_expr ( cx. tcx , insertion. call ) {
662
658
write ! (
663
659
res,
664
- "e.insert({});\n {}None" ,
665
- snippet_with_context( cx, insertion. value. span, ctxt, ".." , app) . 0 ,
666
- snippet_indent( cx, insertion. call. span) . as_deref( ) . unwrap_or( "" ) ,
660
+ "e.insert({value_str});\n {indent}None" ,
661
+ indent = snippet_indent( cx, insertion. call. span) . as_deref( ) . unwrap_or( "" ) ,
667
662
)
668
663
} else {
669
- write ! (
670
- res,
671
- "{{ e.insert({}); None }}" ,
672
- snippet_with_context( cx, insertion. value. span, ctxt, ".." , app) . 0 ,
673
- )
664
+ write ! ( res, "{{ e.insert({value_str}); None }}" )
674
665
} ;
675
666
} ) ,
676
667
"Vacant(e)" ,
@@ -690,7 +681,8 @@ impl<'tcx> InsertSearchResults<'tcx> {
690
681
".." ,
691
682
app,
692
683
) ) ;
693
- res. push_str ( & snippet_with_context ( cx, insertion. value . span , ctxt, ".." , app) . 0 ) ;
684
+ let value_str = snippet_with_context ( cx, insertion. value . span , ctxt, ".." , app) . 0 ;
685
+ let _: fmt:: Result = write ! ( res, "{value_str}" ) ;
694
686
span = span. trim_start ( insertion. call . span ) . unwrap_or ( DUMMY_SP ) ;
695
687
} ,
696
688
Edit :: RemoveSemi ( semi_span) => {
0 commit comments