Skip to content

Commit 2abaa96

Browse files
committed
Rework suggestion generation of unit_arg lint
1 parent a6f310e commit 2abaa96

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
@@ -660,6 +660,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
660660

661661
match expr.kind {
662662
ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args) => {
663+
let mut args_to_recover = vec![];
663664
for arg in args {
664665
if is_unit(cx.tables.expr_ty(arg)) && !is_unit_literal(arg) {
665666
if let ExprKind::Match(.., match_source) = &arg.kind {
@@ -668,17 +669,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
668669
}
669670
}
670671

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);
680673
}
681674
}
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+
}
682706
},
683707
_ => (),
684708
}

0 commit comments

Comments
 (0)