1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir::{self as hir, Block, Expr, ExprKind, MatchSource, Node, StmtKind};
6 use rustc_lint::LateContext;
8 use super::{utils, UNIT_ARG};
10 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
11 if expr.span.from_expansion() {
15 // apparently stuff in the desugaring of `?` can trigger this
16 // so check for that here
17 // only the calls to `Try::from_error` is marked as desugared,
18 // so we need to check both the current Expr and its parent.
19 if is_questionmark_desugar_marked_call(expr) {
23 let map = &cx.tcx.hir();
24 let opt_parent_node = map.find(map.get_parent_node(expr.hir_id));
25 if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node;
26 if is_questionmark_desugar_marked_call(parent_expr);
33 ExprKind::Call(_, args) | ExprKind::MethodCall(_, _, args, _) => {
34 let args_to_recover = args
37 if cx.typeck_results().expr_ty(arg).is_unit() && !utils::is_unit_literal(arg) {
40 ExprKind::Match(.., MatchSource::TryDesugar) | ExprKind::Path(..)
47 if !args_to_recover.is_empty() {
48 lint_unit_args(cx, expr, &args_to_recover);
55 fn is_questionmark_desugar_marked_call(expr: &Expr<'_>) -> bool {
56 use rustc_span::hygiene::DesugaringKind;
57 if let ExprKind::Call(ref callee, _) = expr.kind {
58 callee.span.is_desugaring(DesugaringKind::QuestionMark)
64 fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Expr<'_>]) {
65 let mut applicability = Applicability::MachineApplicable;
66 let (singular, plural) = if args_to_recover.len() > 1 {
75 &format!("passing {}unit value{} to a function", singular, plural),
82 if let ExprKind::Block(block, _) = arg.kind;
83 if block.expr.is_none();
84 if let Some(last_stmt) = block.stmts.iter().last();
85 if let StmtKind::Semi(last_expr) = last_stmt.kind;
86 if let Some(snip) = snippet_opt(cx, last_expr.span);
98 .for_each(|(span, sugg)| {
101 "remove the semicolon from the last statement in the block",
103 Applicability::MaybeIncorrect,
106 applicability = Applicability::MaybeIncorrect;
109 let arg_snippets: Vec<String> = args_to_recover
111 .filter_map(|arg| snippet_opt(cx, arg.span))
113 let arg_snippets_without_empty_blocks: Vec<String> = args_to_recover
115 .filter(|arg| !is_empty_block(arg))
116 .filter_map(|arg| snippet_opt(cx, arg.span))
119 if let Some(call_snippet) = snippet_opt(cx, expr.span) {
120 let sugg = fmt_stmts_and_call(
125 &arg_snippets_without_empty_blocks,
128 if arg_snippets_without_empty_blocks.is_empty() {
129 db.multipart_suggestion(
130 &format!("use {}unit literal{} instead", singular, plural),
133 .map(|arg| (arg.span, "()".to_string()))
134 .collect::<Vec<_>>(),
138 let plural = arg_snippets_without_empty_blocks.len() > 1;
139 let empty_or_s = if plural { "s" } else { "" };
140 let it_or_them = if plural { "them" } else { "it" };
144 "{}move the expression{} in front of the call and replace {} with the unit literal `()`",
145 or, empty_or_s, it_or_them
156 fn is_empty_block(expr: &Expr<'_>) -> bool {
170 fn fmt_stmts_and_call(
171 cx: &LateContext<'_>,
172 call_expr: &Expr<'_>,
174 args_snippets: &[impl AsRef<str>],
175 non_empty_block_args_snippets: &[impl AsRef<str>],
177 let call_expr_indent = indent_of(cx, call_expr.span).unwrap_or(0);
178 let call_snippet_with_replacements = args_snippets
180 .fold(call_snippet.to_owned(), |acc, arg| acc.replacen(arg.as_ref(), "()", 1));
182 let mut stmts_and_call = non_empty_block_args_snippets
184 .map(|it| it.as_ref().to_owned())
185 .collect::<Vec<_>>();
186 stmts_and_call.push(call_snippet_with_replacements);
187 stmts_and_call = stmts_and_call
189 .map(|v| reindent_multiline(v.into(), true, Some(call_expr_indent)).into_owned())
192 let mut stmts_and_call_snippet = stmts_and_call.join(&format!("{}{}", ";\n", " ".repeat(call_expr_indent)));
193 // expr is not in a block statement or result expression position, wrap in a block
194 let parent_node = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(call_expr.hir_id));
195 if !matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_))) {
196 let block_indent = call_expr_indent + 4;
197 stmts_and_call_snippet =
198 reindent_multiline(stmts_and_call_snippet.into(), true, Some(block_indent)).into_owned();
199 stmts_and_call_snippet = format!(
201 " ".repeat(block_indent),
202 &stmts_and_call_snippet,
203 " ".repeat(call_expr_indent)
206 stmts_and_call_snippet