@@ -10,7 +10,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
10
10
use rustc_hir as hir;
11
11
use rustc_hir:: intravisit:: { walk_body, walk_expr, walk_ty, FnKind , NestedVisitorMap , Visitor } ;
12
12
use rustc_hir:: {
13
- BinOpKind , Body , Expr , ExprKind , FnDecl , FnRetTy , FnSig , GenericArg , GenericParamKind , HirId , ImplItem ,
13
+ BinOpKind , Block , Body , Expr , ExprKind , FnDecl , FnRetTy , FnSig , GenericArg , GenericParamKind , HirId , ImplItem ,
14
14
ImplItemKind , Item , ItemKind , Lifetime , Local , MatchSource , MutTy , Mutability , QPath , Stmt , StmtKind , TraitFn ,
15
15
TraitItem , TraitItemKind , TyKind , UnOp ,
16
16
} ;
@@ -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
- qpath_res, same_tys, sext, snippet, snippet_opt , snippet_with_applicability , snippet_with_macro_callsite ,
35
- span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
34
+ qpath_res, same_tys, sext, snippet, snippet_block_with_applicability , snippet_opt , snippet_with_applicability ,
35
+ snippet_with_macro_callsite , span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
36
36
} ;
37
37
38
38
declare_clippy_lint ! {
@@ -847,6 +847,7 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
847
847
} ) ;
848
848
let sugg = args_to_recover
849
849
. iter ( )
850
+ . filter ( |arg| !is_empty_block ( arg) )
850
851
. enumerate ( )
851
852
. map ( |( i, arg) | {
852
853
let indent = if i == 0 {
@@ -860,16 +861,20 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
860
861
snippet_block_with_applicability( cx, arg. span, ".." , Some ( expr. span) , & mut applicability)
861
862
)
862
863
} )
863
- . collect :: < Vec < String > > ( )
864
- . join ( "\n " ) ;
865
- db. span_suggestion (
866
- expr. span . with_hi ( expr. span . lo ( ) ) ,
867
- & format ! ( "{}move the expression{} in front of the call..." , or, plural) ,
868
- format ! ( "{}\n " , sugg) ,
869
- applicability,
870
- ) ;
864
+ . collect :: < Vec < String > > ( ) ;
865
+ let mut and = "" ;
866
+ if !sugg. is_empty ( ) {
867
+ let plural = if sugg. len ( ) > 1 { "s" } else { "" } ;
868
+ db. span_suggestion (
869
+ expr. span . with_hi ( expr. span . lo ( ) ) ,
870
+ & format ! ( "{}move the expression{} in front of the call..." , or, plural) ,
871
+ format ! ( "{}\n " , sugg. join( "\n " ) ) ,
872
+ applicability,
873
+ ) ;
874
+ and = "...and "
875
+ }
871
876
db. multipart_suggestion (
872
- & format ! ( "...and use {}unit literal{} instead" , singular, plural) ,
877
+ & format ! ( "{} use {}unit literal{} instead" , and , singular, plural) ,
873
878
args_to_recover
874
879
. iter ( )
875
880
. map ( |arg| ( arg. span , "()" . to_string ( ) ) )
@@ -880,6 +885,18 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
880
885
) ;
881
886
}
882
887
888
+ fn is_empty_block ( expr : & Expr < ' _ > ) -> bool {
889
+ matches ! (
890
+ expr. kind,
891
+ ExprKind :: Block (
892
+ Block {
893
+ stmts: & [ ] , expr: None , ..
894
+ } ,
895
+ _,
896
+ )
897
+ )
898
+ }
899
+
883
900
fn is_questionmark_desugar_marked_call ( expr : & Expr < ' _ > ) -> bool {
884
901
use rustc_span:: hygiene:: DesugaringKind ;
885
902
if let ExprKind :: Call ( ref callee, _) = expr. kind {
0 commit comments