Skip to content

Commit f3ccbef

Browse files
committed
unit-arg - pr comments
1 parent 2ecc2ac commit f3ccbef

File tree

5 files changed

+88
-53
lines changed

5 files changed

+88
-53
lines changed

clippy_lints/src/types.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ use rustc_typeck::hir_ty_to_ty;
2929
use crate::consts::{constant, Constant};
3030
use crate::utils::paths;
3131
use crate::utils::{
32-
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, is_type_diagnostic_item,
32+
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_type_diagnostic_item,
3333
last_path_segment, match_def_path, match_path, method_chain_args, multispan_sugg, numeric_literal::NumericLiteral,
3434
qpath_res, sext, snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
35-
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
35+
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, trim_multiline, unsext,
3636
};
3737

3838
declare_clippy_lint! {
@@ -802,6 +802,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
802802
}
803803
}
804804

805+
#[allow(clippy::too_many_lines)]
805806
fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Expr<'_>]) {
806807
let mut applicability = Applicability::MachineApplicable;
807808
let (singular, plural) = if args_to_recover.len() > 1 {
@@ -856,18 +857,38 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
856857
.filter(|arg| !is_empty_block(arg))
857858
.filter_map(|arg| snippet_opt(cx, arg.span))
858859
.collect();
860+
let indent = indent_of(cx, expr.span).unwrap_or(0);
859861

860-
if let Some(mut sugg) = snippet_opt(cx, expr.span) {
861-
arg_snippets.iter().for_each(|arg| {
862-
sugg = sugg.replacen(arg, "()", 1);
863-
});
864-
sugg = format!("{}{}{}", arg_snippets_without_empty_blocks.join("; "), "; ", sugg);
862+
if let Some(expr_str) = snippet_opt(cx, expr.span) {
863+
let expr_with_replacements = arg_snippets
864+
.iter()
865+
.fold(expr_str, |acc, arg| acc.replacen(arg, "()", 1));
866+
867+
// expr is not in a block statement or result expression position, wrap in a block
865868
let parent_node = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(expr.hir_id));
866-
if !matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_))) {
867-
// expr is not in a block statement or result expression position, wrap in a block
868-
sugg = format!("{{ {} }}", sugg);
869+
let wrap_in_block =
870+
!matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_)));
871+
872+
let stmts_indent = if wrap_in_block { indent + 4 } else { indent };
873+
let mut stmts_and_call = arg_snippets_without_empty_blocks.clone();
874+
stmts_and_call.push(expr_with_replacements);
875+
let mut stmts_and_call_str = stmts_and_call
876+
.into_iter()
877+
.enumerate()
878+
.map(|(i, v)| {
879+
let with_indent_prefix = if i > 0 { " ".repeat(stmts_indent) + &v } else { v };
880+
trim_multiline(with_indent_prefix.into(), true, Some(stmts_indent)).into_owned()
881+
})
882+
.collect::<Vec<String>>()
883+
.join(";\n");
884+
885+
if wrap_in_block {
886+
stmts_and_call_str = " ".repeat(stmts_indent) + &stmts_and_call_str;
887+
stmts_and_call_str = format!("{{\n{}\n{}}}", &stmts_and_call_str, " ".repeat(indent));
869888
}
870889

890+
let sugg = stmts_and_call_str;
891+
871892
if arg_snippets_without_empty_blocks.is_empty() {
872893
db.multipart_suggestion(
873894
&format!("use {}unit literal{} instead", singular, plural),

clippy_lints/src/utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ pub fn expr_block<'a, T: LintContext>(
662662

663663
/// Trim indentation from a multiline string with possibility of ignoring the
664664
/// first line.
665-
fn trim_multiline(s: Cow<'_, str>, ignore_first: bool, indent: Option<usize>) -> Cow<'_, str> {
665+
pub fn trim_multiline(s: Cow<'_, str>, ignore_first: bool, indent: Option<usize>) -> Cow<'_, str> {
666666
let s_space = trim_multiline_inner(s, ignore_first, indent, ' ');
667667
let s_tab = trim_multiline_inner(s_space, ignore_first, indent, '\t');
668668
trim_multiline_inner(s_tab, ignore_first, indent, ' ')

tests/ui/unit_arg.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#![warn(clippy::unit_arg)]
2-
#![allow(clippy::no_effect, unused_must_use, unused_variables, clippy::unused_unit)]
2+
#![allow(
3+
clippy::no_effect,
4+
unused_must_use,
5+
unused_variables,
6+
clippy::unused_unit,
7+
clippy::or_fun_call
8+
)]
39

410
use std::fmt::Debug;
511

tests/ui/unit_arg.stderr

+42-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: passing a unit value to a function
2-
--> $DIR/unit_arg.rs:23:5
2+
--> $DIR/unit_arg.rs:29:5
33
|
44
LL | / foo({
55
LL | | 1;
@@ -15,22 +15,24 @@ help: or move the expression in front of the call and replace it with the unit l
1515
|
1616
LL | {
1717
LL | 1;
18-
LL | }; foo(());
18+
LL | };
19+
LL | foo(());
1920
|
2021

2122
error: passing a unit value to a function
22-
--> $DIR/unit_arg.rs:26:5
23+
--> $DIR/unit_arg.rs:32:5
2324
|
2425
LL | foo(foo(1));
2526
| ^^^^^^^^^^^
2627
|
2728
help: move the expression in front of the call and replace it with the unit literal `()`
2829
|
29-
LL | foo(1); foo(());
30-
| ^^^^^^^^^^^^^^^
30+
LL | foo(1);
31+
LL | foo(());
32+
|
3133

3234
error: passing a unit value to a function
33-
--> $DIR/unit_arg.rs:27:5
35+
--> $DIR/unit_arg.rs:33:5
3436
|
3537
LL | / foo({
3638
LL | | foo(1);
@@ -47,11 +49,12 @@ help: or move the expression in front of the call and replace it with the unit l
4749
LL | {
4850
LL | foo(1);
4951
LL | foo(2);
50-
LL | }; foo(());
52+
LL | };
53+
LL | foo(());
5154
|
5255

5356
error: passing a unit value to a function
54-
--> $DIR/unit_arg.rs:32:5
57+
--> $DIR/unit_arg.rs:38:5
5558
|
5659
LL | / b.bar({
5760
LL | | 1;
@@ -66,22 +69,25 @@ help: or move the expression in front of the call and replace it with the unit l
6669
|
6770
LL | {
6871
LL | 1;
69-
LL | }; b.bar(());
72+
LL | };
73+
LL | b.bar(());
7074
|
7175

7276
error: passing unit values to a function
73-
--> $DIR/unit_arg.rs:35:5
77+
--> $DIR/unit_arg.rs:41:5
7478
|
7579
LL | taking_multiple_units(foo(0), foo(1));
7680
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7781
|
7882
help: move the expressions in front of the call and replace them with the unit literal `()`
7983
|
80-
LL | foo(0); foo(1); taking_multiple_units((), ());
81-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
LL | foo(0);
85+
LL | foo(1);
86+
LL | taking_multiple_units((), ());
87+
|
8288

8389
error: passing unit values to a function
84-
--> $DIR/unit_arg.rs:36:5
90+
--> $DIR/unit_arg.rs:42:5
8591
|
8692
LL | / taking_multiple_units(foo(0), {
8793
LL | | foo(1);
@@ -95,14 +101,16 @@ LL | foo(2)
95101
|
96102
help: or move the expressions in front of the call and replace them with the unit literal `()`
97103
|
98-
LL | foo(0); {
104+
LL | foo(0);
105+
LL | {
99106
LL | foo(1);
100107
LL | foo(2);
101-
LL | }; taking_multiple_units((), ());
108+
LL | };
109+
LL | taking_multiple_units((), ());
102110
|
103111

104112
error: passing unit values to a function
105-
--> $DIR/unit_arg.rs:40:5
113+
--> $DIR/unit_arg.rs:46:5
106114
|
107115
LL | / taking_multiple_units(
108116
LL | | {
@@ -124,53 +132,50 @@ LL | foo(3)
124132
help: or move the expressions in front of the call and replace them with the unit literal `()`
125133
|
126134
LL | {
127-
LL | foo(0);
128-
LL | foo(1);
129-
LL | }; {
130-
LL | foo(2);
131-
LL | foo(3);
135+
LL | foo(0);
136+
LL | foo(1);
137+
LL | };
138+
LL | {
139+
LL | foo(2);
132140
...
133141

134-
error: use of `or` followed by a function call
135-
--> $DIR/unit_arg.rs:51:10
136-
|
137-
LL | None.or(Some(foo(2)));
138-
| ^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some(foo(2)))`
139-
|
140-
= note: `-D clippy::or-fun-call` implied by `-D warnings`
141-
142142
error: passing a unit value to a function
143-
--> $DIR/unit_arg.rs:51:13
143+
--> $DIR/unit_arg.rs:57:13
144144
|
145145
LL | None.or(Some(foo(2)));
146146
| ^^^^^^^^^^^^
147147
|
148148
help: move the expression in front of the call and replace it with the unit literal `()`
149149
|
150-
LL | None.or({ foo(2); Some(()) });
151-
| ^^^^^^^^^^^^^^^^^^^^
150+
LL | None.or({
151+
LL | foo(2);
152+
LL | Some(())
153+
LL | });
154+
|
152155

153156
error: passing a unit value to a function
154-
--> $DIR/unit_arg.rs:54:5
157+
--> $DIR/unit_arg.rs:60:5
155158
|
156159
LL | foo(foo(()))
157160
| ^^^^^^^^^^^^
158161
|
159162
help: move the expression in front of the call and replace it with the unit literal `()`
160163
|
161-
LL | foo(()); foo(())
164+
LL | foo(());
165+
LL | foo(())
162166
|
163167

164168
error: passing a unit value to a function
165-
--> $DIR/unit_arg.rs:87:5
169+
--> $DIR/unit_arg.rs:93:5
166170
|
167171
LL | Some(foo(1))
168172
| ^^^^^^^^^^^^
169173
|
170174
help: move the expression in front of the call and replace it with the unit literal `()`
171175
|
172-
LL | foo(1); Some(())
176+
LL | foo(1);
177+
LL | Some(())
173178
|
174179

175-
error: aborting due to 11 previous errors
180+
error: aborting due to 10 previous errors
176181

tests/ui/unit_arg_empty_blocks.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ LL | taking_two_units({}, foo(0));
2424
|
2525
help: move the expression in front of the call and replace it with the unit literal `()`
2626
|
27-
LL | foo(0); taking_two_units((), ());
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
LL | foo(0);
28+
LL | taking_two_units((), ());
29+
|
2930

3031
error: passing unit values to a function
3132
--> $DIR/unit_arg_empty_blocks.rs:18:5
@@ -35,8 +36,10 @@ LL | taking_three_units({}, foo(0), foo(1));
3536
|
3637
help: move the expressions in front of the call and replace them with the unit literal `()`
3738
|
38-
LL | foo(0); foo(1); taking_three_units((), (), ());
39-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
LL | foo(0);
40+
LL | foo(1);
41+
LL | taking_three_units((), (), ());
42+
|
4043

4144
error: aborting due to 4 previous errors
4245

0 commit comments

Comments
 (0)