Skip to content

Commit a9cde3a

Browse files
committed
Don't suggest to move empty blocks
1 parent 6d15a14 commit a9cde3a

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

clippy_lints/src/types.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
1010
use rustc_hir as hir;
1111
use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisitorMap, Visitor};
1212
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,
1414
ImplItemKind, Item, ItemKind, Lifetime, Local, MatchSource, MutTy, Mutability, QPath, Stmt, StmtKind, TraitFn,
1515
TraitItem, TraitItemKind, TyKind, UnOp,
1616
};
@@ -29,10 +29,10 @@ use rustc_typeck::hir_ty_to_ty;
2929
use crate::consts::{constant, Constant};
3030
use crate::utils::paths;
3131
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,
3333
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,
3636
};
3737

3838
declare_clippy_lint! {
@@ -847,6 +847,7 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
847847
});
848848
let sugg = args_to_recover
849849
.iter()
850+
.filter(|arg| !is_empty_block(arg))
850851
.enumerate()
851852
.map(|(i, arg)| {
852853
let indent = if i == 0 {
@@ -860,16 +861,20 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
860861
snippet_block_with_applicability(cx, arg.span, "..", Some(expr.span), &mut applicability)
861862
)
862863
})
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+
}
871876
db.multipart_suggestion(
872-
&format!("...and use {}unit literal{} instead", singular, plural),
877+
&format!("{}use {}unit literal{} instead", and, singular, plural),
873878
args_to_recover
874879
.iter()
875880
.map(|arg| (arg.span, "()".to_string()))
@@ -880,6 +885,18 @@ fn lint_unit_args(cx: &LateContext<'_, '_>, expr: &Expr<'_>, args_to_recover: &[
880885
);
881886
}
882887

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+
883900
fn is_questionmark_desugar_marked_call(expr: &Expr<'_>) -> bool {
884901
use rustc_span::hygiene::DesugaringKind;
885902
if let ExprKind::Call(ref callee, _) = expr.kind {

0 commit comments

Comments
 (0)