about summary refs log tree commit diff
diff options
context:
space:
mode:
authorflip1995 <hello@philkrones.com>2019-08-26 18:34:30 +0200
committerflip1995 <hello@philkrones.com>2020-05-31 18:47:48 +0200
commit0ab823c509897ce2f516feb760fe1bf02cf77443 (patch)
treec8df46c0b647a5a9e4d4a8074afcdd107e7f436f
parent8aa8f425565975e283461dbfdc2c4ac0b4273934 (diff)
downloadrust-0ab823c509897ce2f516feb760fe1bf02cf77443.tar.gz
rust-0ab823c509897ce2f516feb760fe1bf02cf77443.zip
Rework suggestion generation of `unit_arg` lint
-rw-r--r--clippy_lints/src/types.rs42
1 files changed, 33 insertions, 9 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index 3ac99e24684..8fcca4b7bb9 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -779,6 +779,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
 
         match expr.kind {
             ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args) => {
+                let mut args_to_recover = vec![];
                 for arg in args {
                     if is_unit(cx.tables.expr_ty(arg)) && !is_unit_literal(arg) {
                         if let ExprKind::Match(.., match_source) = &arg.kind {
@@ -787,17 +788,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
                             }
                         }
 
-                        span_lint_and_sugg(
-                            cx,
-                            UNIT_ARG,
-                            arg.span,
-                            "passing a unit value to a function",
-                            "if you intended to pass a unit value, use a unit literal instead",
-                            "()".to_string(),
-                            Applicability::MaybeIncorrect,
-                        );
+                        args_to_recover.push(arg);
                     }
                 }
+                if !args_to_recover.is_empty() {
+                    let mut applicability = Applicability::MachineApplicable;
+                    span_lint_and_then(cx, UNIT_ARG, expr.span, "passing a unit value to a function", |db| {
+                        db.span_suggestion(
+                            expr.span.with_hi(expr.span.lo()),
+                            "move the expressions in front of the call...",
+                            format!(
+                                "{} ",
+                                args_to_recover
+                                    .iter()
+                                    .map(|arg| {
+                                        format!(
+                                            "{};",
+                                            snippet_with_applicability(cx, arg.span, "..", &mut applicability)
+                                        )
+                                    })
+                                    .collect::<Vec<String>>()
+                                    .join(" ")
+                            ),
+                            applicability,
+                        );
+                        db.multipart_suggestion(
+                            "...and use unit literals instead",
+                            args_to_recover
+                                .iter()
+                                .map(|arg| (arg.span, "()".to_string()))
+                                .collect::<Vec<_>>(),
+                            applicability,
+                        );
+                    });
+                }
             },
             _ => (),
         }