@@ -29,10 +29,10 @@ use rustc_typeck::hir_ty_to_ty;
29
29
use crate :: consts:: { constant, Constant } ;
30
30
use crate :: utils:: paths;
31
31
use 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,
33
33
last_path_segment, match_def_path, match_path, method_chain_args, multispan_sugg, numeric_literal:: NumericLiteral ,
34
34
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,
36
36
} ;
37
37
38
38
declare_clippy_lint ! {
@@ -802,6 +802,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
802
802
}
803
803
}
804
804
805
+ #[ allow( clippy:: too_many_lines) ]
805
806
fn lint_unit_args ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , args_to_recover : & [ & Expr < ' _ > ] ) {
806
807
let mut applicability = Applicability :: MachineApplicable ;
807
808
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
856
857
. filter ( |arg| !is_empty_block ( arg) )
857
858
. filter_map ( |arg| snippet_opt ( cx, arg. span ) )
858
859
. collect ( ) ;
860
+ let indent = indent_of ( cx, expr. span ) . unwrap_or ( 0 ) ;
859
861
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
865
868
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) ) ;
869
888
}
870
889
890
+ let sugg = stmts_and_call_str;
891
+
871
892
if arg_snippets_without_empty_blocks. is_empty ( ) {
872
893
db. multipart_suggestion (
873
894
& format ! ( "use {}unit literal{} instead" , singular, plural) ,
0 commit comments