Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0ab823c

Browse files
committed
Rework suggestion generation of unit_arg lint
1 parent 8aa8f42 commit 0ab823c

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

780780
match expr.kind {
781781
ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args) => {
782+
let mut args_to_recover = vec![];
782783
for arg in args {
783784
if is_unit(cx.tables.expr_ty(arg)) && !is_unit_literal(arg) {
784785
if let ExprKind::Match(.., match_source) = &arg.kind {
@@ -787,17 +788,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
787788
}
788789
}
789790

790-
span_lint_and_sugg(
791-
cx,
792-
UNIT_ARG,
793-
arg.span,
794-
"passing a unit value to a function",
795-
"if you intended to pass a unit value, use a unit literal instead",
796-
"()".to_string(),
797-
Applicability::MaybeIncorrect,
798-
);
791+
args_to_recover.push(arg);
799792
}
800793
}
794+
if !args_to_recover.is_empty() {
795+
let mut applicability = Applicability::MachineApplicable;
796+
span_lint_and_then(cx, UNIT_ARG, expr.span, "passing a unit value to a function", |db| {
797+
db.span_suggestion(
798+
expr.span.with_hi(expr.span.lo()),
799+
"move the expressions in front of the call...",
800+
format!(
801+
"{} ",
802+
args_to_recover
803+
.iter()
804+
.map(|arg| {
805+
format!(
806+
"{};",
807+
snippet_with_applicability(cx, arg.span, "..", &mut applicability)
808+
)
809+
})
810+
.collect::<Vec<String>>()
811+
.join(" ")
812+
),
813+
applicability,
814+
);
815+
db.multipart_suggestion(
816+
"...and use unit literals instead",
817+
args_to_recover
818+
.iter()
819+
.map(|arg| (arg.span, "()".to_string()))
820+
.collect::<Vec<_>>(),
821+
applicability,
822+
);
823+
});
824+
}
801825
},
802826
_ => (),
803827
}

0 commit comments

Comments
 (0)