@@ -660,6 +660,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
660
660
661
661
match expr. kind {
662
662
ExprKind :: Call ( _, args) | ExprKind :: MethodCall ( _, _, args) => {
663
+ let mut args_to_recover = vec ! [ ] ;
663
664
for arg in args {
664
665
if is_unit ( cx. tables . expr_ty ( arg) ) && !is_unit_literal ( arg) {
665
666
if let ExprKind :: Match ( .., match_source) = & arg. kind {
@@ -668,17 +669,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
668
669
}
669
670
}
670
671
671
- span_lint_and_sugg (
672
- cx,
673
- UNIT_ARG ,
674
- arg. span ,
675
- "passing a unit value to a function" ,
676
- "if you intended to pass a unit value, use a unit literal instead" ,
677
- "()" . to_string ( ) ,
678
- Applicability :: MachineApplicable ,
679
- ) ;
672
+ args_to_recover. push ( arg) ;
680
673
}
681
674
}
675
+ if !args_to_recover. is_empty ( ) {
676
+ let mut applicability = Applicability :: MachineApplicable ;
677
+ span_lint_and_then ( cx, UNIT_ARG , expr. span , "passing a unit value to a function" , |db| {
678
+ db. span_suggestion (
679
+ expr. span . with_hi ( expr. span . lo ( ) ) ,
680
+ "move the expressions in front of the call..." ,
681
+ format ! (
682
+ "{} " ,
683
+ args_to_recover
684
+ . iter( )
685
+ . map( |arg| {
686
+ format!(
687
+ "{};" ,
688
+ snippet_with_applicability( cx, arg. span, ".." , & mut applicability)
689
+ )
690
+ } )
691
+ . collect:: <Vec <String >>( )
692
+ . join( " " )
693
+ ) ,
694
+ applicability,
695
+ ) ;
696
+ db. multipart_suggestion (
697
+ "...and use unit literals instead" ,
698
+ args_to_recover
699
+ . iter ( )
700
+ . map ( |arg| ( arg. span , "()" . to_string ( ) ) )
701
+ . collect :: < Vec < _ > > ( ) ,
702
+ applicability,
703
+ ) ;
704
+ } ) ;
705
+ }
682
706
} ,
683
707
_ => ( ) ,
684
708
}
0 commit comments