Skip to content

Commit d127d3e

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

File tree

4 files changed

+84
-49
lines changed

4 files changed

+84
-49
lines changed

clippy_lints/src/types.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ use rustc_span::symbol::sym;
2525
use rustc_target::abi::LayoutOf;
2626
use rustc_target::spec::abi::Abi;
2727
use rustc_typeck::hir_ty_to_ty;
28+
use std::iter;
2829

2930
use crate::consts::{constant, Constant};
3031
use crate::utils::paths;
3132
use crate::utils::{
32-
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, is_type_diagnostic_item,
33+
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_type_diagnostic_item,
3334
last_path_segment, match_def_path, match_path, method_chain_args, multispan_sugg, numeric_literal::NumericLiteral,
3435
qpath_res, sext, snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
3536
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
@@ -802,6 +803,7 @@ impl<'tcx> LateLintPass<'tcx> for UnitArg {
802803
}
803804
}
804805

806+
#[allow(clippy::too_many_lines)]
805807
fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Expr<'_>]) {
806808
let mut applicability = Applicability::MachineApplicable;
807809
let (singular, plural) = if args_to_recover.len() > 1 {
@@ -856,17 +858,36 @@ fn lint_unit_args(cx: &LateContext<'_>, expr: &Expr<'_>, args_to_recover: &[&Exp
856858
.filter(|arg| !is_empty_block(arg))
857859
.filter_map(|arg| snippet_opt(cx, arg.span))
858860
.collect();
861+
let indent = indent_of(cx, expr.span).unwrap_or(0);
859862

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);
863+
if let Some(expr_str) = snippet_opt(cx, expr.span) {
864+
let expr_with_replacements = arg_snippets
865+
.iter()
866+
.fold(expr_str, |acc, arg| acc.replacen(arg, "()", 1));
865867
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-
}
868+
let sugg =
869+
if !matches!(parent_node, Some(Node::Block(_))) && !matches!(parent_node, Some(Node::Stmt(_))) {
870+
// expr is not in a block statement or result expression position, wrap in a block
871+
let indent_str = iter::repeat(' ').take(indent).collect::<String>();
872+
let inner_indent_str = iter::repeat(' ').take(indent + 4).collect::<String>();
873+
let mut block_content: Vec<String> = arg_snippets_without_empty_blocks.clone();
874+
block_content.push(expr_with_replacements);
875+
block_content = block_content
876+
.iter()
877+
.map(|item| inner_indent_str.clone() + item)
878+
.collect();
879+
let block_content_str = block_content.join(";\n");
880+
format!("{{\n{}\n{}}}", &block_content_str, &indent_str)
881+
} else {
882+
let indent_str = iter::repeat(' ').take(indent).collect::<String>();
883+
let join_str = format!(";\n{}", indent_str);
884+
format!(
885+
"{}{}{}",
886+
arg_snippets_without_empty_blocks.join(&join_str),
887+
&join_str,
888+
&expr_with_replacements
889+
)
890+
};
870891

871892
if arg_snippets_without_empty_blocks.is_empty() {
872893
db.multipart_suggestion(

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

+39-34
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 | | {
@@ -126,51 +134,48 @@ help: or move the expressions in front of the call and replace them with the uni
126134
LL | {
127135
LL | foo(0);
128136
LL | foo(1);
129-
LL | }; {
137+
LL | };
138+
LL | {
130139
LL | foo(2);
131-
LL | foo(3);
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)