@@ -29,10 +29,10 @@ use rustc_typeck::hir_ty_to_ty;
2929use crate :: consts:: { constant, Constant } ;
3030use crate :: utils:: paths;
3131use crate :: utils:: {
32- clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, is_type_diagnostic_item,
32+ clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of , int_bits, is_type_diagnostic_item,
3333 last_path_segment, match_def_path, match_path, method_chain_args, multispan_sugg, numeric_literal:: NumericLiteral ,
3434 qpath_res, sext, snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
35- span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
35+ span_lint_and_help, span_lint_and_sugg, span_lint_and_then, trim_multiline , unsext,
3636} ;
3737
3838declare_clippy_lint ! {
@@ -802,6 +802,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
802802 }
803803}
804804
805+ #[ allow( clippy:: too_many_lines) ]
805806fn lint_unit_args ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , args_to_recover : & [ & Expr < ' _ > ] ) {
806807 let mut applicability = Applicability :: MachineApplicable ;
807808 let ( singular, plural) = if args_to_recover. len ( ) > 1 {
@@ -856,18 +857,38 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
856857 . filter ( |arg| !is_empty_block ( arg) )
857858 . filter_map ( |arg| snippet_opt ( cx, arg. span ) )
858859 . collect ( ) ;
860+ let indent = indent_of ( cx, expr. span ) . unwrap_or ( 0 ) ;
859861
860- if let Some ( mut sugg) = snippet_opt ( cx, expr. span ) {
861- arg_snippets. iter ( ) . for_each ( |arg| {
862- sugg = sugg. replacen ( arg, "()" , 1 ) ;
863- } ) ;
864- sugg = format ! ( "{}{}{}" , arg_snippets_without_empty_blocks. join( "; " ) , "; " , sugg) ;
862+ if let Some ( expr_str) = snippet_opt ( cx, expr. span ) {
863+ let expr_with_replacements = arg_snippets
864+ . iter ( )
865+ . fold ( expr_str, |acc, arg| acc. replacen ( arg, "()" , 1 ) ) ;
866+
867+ // expr is not in a block statement or result expression position, wrap in a block
865868 let parent_node = cx. tcx . hir ( ) . find ( cx. tcx . hir ( ) . get_parent_node ( expr. hir_id ) ) ;
866- if !matches ! ( parent_node, Some ( Node :: Block ( _) ) ) && !matches ! ( parent_node, Some ( Node :: Stmt ( _) ) ) {
867- // expr is not in a block statement or result expression position, wrap in a block
868- sugg = format ! ( "{{ {} }}" , sugg) ;
869+ let wrap_in_block =
870+ !matches ! ( parent_node, Some ( Node :: Block ( _) ) ) && !matches ! ( parent_node, Some ( Node :: Stmt ( _) ) ) ;
871+
872+ let stmts_indent = if wrap_in_block { indent + 4 } else { indent } ;
873+ let mut stmts_and_call = arg_snippets_without_empty_blocks. clone ( ) ;
874+ stmts_and_call. push ( expr_with_replacements) ;
875+ let mut stmts_and_call_str = stmts_and_call
876+ . into_iter ( )
877+ . enumerate ( )
878+ . map ( |( i, v) | {
879+ let with_indent_prefix = if i > 0 { " " . repeat ( stmts_indent) + & v } else { v } ;
880+ trim_multiline ( with_indent_prefix. into ( ) , true , Some ( stmts_indent) ) . into_owned ( )
881+ } )
882+ . collect :: < Vec < String > > ( )
883+ . join ( ";\n " ) ;
884+
885+ if wrap_in_block {
886+ stmts_and_call_str = " " . repeat ( stmts_indent) + & stmts_and_call_str;
887+ stmts_and_call_str = format ! ( "{{\n {}\n {}}}" , & stmts_and_call_str, " " . repeat( indent) ) ;
869888 }
870889
890+ let sugg = stmts_and_call_str;
891+
871892 if arg_snippets_without_empty_blocks. is_empty ( ) {
872893 db. multipart_suggestion (
873894 & format ! ( "use {}unit literal{} instead" , singular, plural) ,
0 commit comments