Skip to content

Commit 197a3ea

Browse files
committed
Rework suggestion generation of unit_arg lint
1 parent 949b347 commit 197a3ea

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

clippy_lints/src/types.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
598598

599599
match expr.node {
600600
ExprKind::Call(_, ref args) | ExprKind::MethodCall(_, _, ref args) => {
601+
let mut args_to_recover = vec![];
601602
for arg in args {
602603
if is_unit(cx.tables.expr_ty(arg)) && !is_unit_literal(arg) {
603604
if let ExprKind::Match(.., match_source) = &arg.node {
@@ -606,17 +607,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
606607
}
607608
}
608609

609-
span_lint_and_sugg(
610-
cx,
611-
UNIT_ARG,
612-
arg.span,
613-
"passing a unit value to a function",
614-
"if you intended to pass a unit value, use a unit literal instead",
615-
"()".to_string(),
616-
Applicability::MachineApplicable,
617-
);
610+
args_to_recover.push(arg);
618611
}
619612
}
613+
if !args_to_recover.is_empty() {
614+
let mut applicability = Applicability::MachineApplicable;
615+
span_lint_and_then(cx, UNIT_ARG, expr.span, "passing a unit value to a function", |db| {
616+
db.span_suggestion(
617+
expr.span.with_hi(expr.span.lo()),
618+
"move the expressions in front of the call...",
619+
format!(
620+
"{} ",
621+
args_to_recover
622+
.iter()
623+
.map(|arg| {
624+
format!(
625+
"{};",
626+
snippet_with_applicability(cx, arg.span, "..", &mut applicability)
627+
)
628+
})
629+
.collect::<Vec<String>>()
630+
.join(" ")
631+
),
632+
applicability,
633+
);
634+
db.multipart_suggestion(
635+
"...and use unit literals instead",
636+
args_to_recover
637+
.iter()
638+
.map(|arg| (arg.span, "()".to_string()))
639+
.collect::<Vec<_>>(),
640+
applicability,
641+
);
642+
});
643+
}
620644
},
621645
_ => (),
622646
}

0 commit comments

Comments
 (0)