Skip to content

Commit 9be97eb

Browse files
committed
Rework suggestion generation of unit_arg lint
1 parent cc35165 commit 9be97eb

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

633633
match expr.kind {
634634
ExprKind::Call(_, ref args) | ExprKind::MethodCall(_, _, ref args) => {
635+
let mut args_to_recover = vec![];
635636
for arg in args {
636637
if is_unit(cx.tables.expr_ty(arg)) && !is_unit_literal(arg) {
637638
if let ExprKind::Match(.., match_source) = &arg.kind {
@@ -640,17 +641,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
640641
}
641642
}
642643

643-
span_lint_and_sugg(
644-
cx,
645-
UNIT_ARG,
646-
arg.span,
647-
"passing a unit value to a function",
648-
"if you intended to pass a unit value, use a unit literal instead",
649-
"()".to_string(),
650-
Applicability::MachineApplicable,
651-
);
644+
args_to_recover.push(arg);
652645
}
653646
}
647+
if !args_to_recover.is_empty() {
648+
let mut applicability = Applicability::MachineApplicable;
649+
span_lint_and_then(cx, UNIT_ARG, expr.span, "passing a unit value to a function", |db| {
650+
db.span_suggestion(
651+
expr.span.with_hi(expr.span.lo()),
652+
"move the expressions in front of the call...",
653+
format!(
654+
"{} ",
655+
args_to_recover
656+
.iter()
657+
.map(|arg| {
658+
format!(
659+
"{};",
660+
snippet_with_applicability(cx, arg.span, "..", &mut applicability)
661+
)
662+
})
663+
.collect::<Vec<String>>()
664+
.join(" ")
665+
),
666+
applicability,
667+
);
668+
db.multipart_suggestion(
669+
"...and use unit literals instead",
670+
args_to_recover
671+
.iter()
672+
.map(|arg| (arg.span, "()".to_string()))
673+
.collect::<Vec<_>>(),
674+
applicability,
675+
);
676+
});
677+
}
654678
},
655679
_ => (),
656680
}

0 commit comments

Comments
 (0)