Skip to content

Commit c3e4d2b

Browse files
committed
clean-up
1 parent 3c93ba0 commit c3e4d2b

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

clippy_lints/src/unit_types/let_unit_value.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,30 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, format_args: &FormatArgsStorag
8989
walk_body(&mut visitor, body);
9090

9191
let mut has_in_format_capture = false;
92-
suggestions.extend(visitor.spans.iter().filter_map(|span| match span {
92+
suggestions.extend(visitor.spans.into_iter().filter_map(|span| match span {
9393
MaybeInFormatCapture::Yes => {
9494
has_in_format_capture = true;
9595
None
9696
},
97-
MaybeInFormatCapture::No(span) => Some((*span, "()".to_string())),
97+
MaybeInFormatCapture::No(span) => Some((span, "()".to_string())),
9898
}));
9999

100100
if has_in_format_capture {
101+
// In a case like this:
102+
// ```
103+
// let unit = returns_unit();
104+
// eprintln!("{unit}");
105+
// ```
106+
// we can't remove the `unit` binding and replace its uses with a `()`,
107+
// because the `eprintln!` would break.
108+
//
109+
// So do the following instead:
110+
// ```
111+
// let unit = ();
112+
// returns_unit();
113+
// eprintln!("{unit}");
114+
// ```
115+
// TODO: find a less awkward way to do this
101116
suggestions.push((
102117
init.span,
103118
format!("();\n{}", reindent_multiline(&snip, false, indent_of(cx, local.span))),
@@ -132,6 +147,12 @@ struct UnitVariableCollector<'a, 'tcx> {
132147
macro_call: Option<&'a FormatArgs>,
133148
}
134149

150+
/// Whether the unit variable is captured in a `format!`:
151+
///
152+
/// ```ignore
153+
/// let unit = ();
154+
/// eprintln!("{unit}");
155+
/// ```
135156
enum MaybeInFormatCapture {
136157
Yes,
137158
No(Span),

tests/ui/let_unit.fixed

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::let_unit_value)]
2-
#![allow(unused, clippy::no_effect, clippy::needless_late_init, path_statements)]
2+
#![allow(clippy::no_effect, clippy::needless_late_init, path_statements)]
33

44
macro_rules! let_and_return {
55
($n:expr) => {{
@@ -15,12 +15,12 @@ fn main() {
1515
if true {
1616
// do not lint this, since () is explicit
1717
let _a = ();
18-
let () = dummy();
18+
let () = returns_unit();
1919
let () = ();
20-
() = dummy();
20+
() = returns_unit();
2121
() = ();
2222
let _a: () = ();
23-
let _a: () = dummy();
23+
let _a: () = returns_unit();
2424
}
2525

2626
consume_units_with_for_loop(); // should be fine as well
@@ -30,7 +30,7 @@ fn main() {
3030
let_and_return!(()) // should be fine
3131
}
3232

33-
fn dummy() {}
33+
fn returns_unit() {}
3434

3535
// Related to issue #1964
3636
fn consume_units_with_for_loop() {
@@ -181,8 +181,6 @@ async fn issue10433() {
181181
pub async fn issue11502(a: ()) {}
182182

183183
pub fn issue12594() {
184-
fn returns_unit() {}
185-
186184
fn returns_result<T>(res: T) -> Result<T, ()> {
187185
Ok(res)
188186
}
@@ -200,11 +198,10 @@ pub fn issue12594() {
200198
}
201199

202200
fn issue15061() {
203-
fn return_unit() {}
204201
fn do_something(x: ()) {}
205202

206203
let res = ();
207-
return_unit();
204+
returns_unit();
208205
//~^ let_unit_value
209206
do_something(());
210207
println!("{res:?}");

tests/ui/let_unit.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::let_unit_value)]
2-
#![allow(unused, clippy::no_effect, clippy::needless_late_init, path_statements)]
2+
#![allow(clippy::no_effect, clippy::needless_late_init, path_statements)]
33

44
macro_rules! let_and_return {
55
($n:expr) => {{
@@ -15,12 +15,12 @@ fn main() {
1515
if true {
1616
// do not lint this, since () is explicit
1717
let _a = ();
18-
let () = dummy();
18+
let () = returns_unit();
1919
let () = ();
20-
() = dummy();
20+
() = returns_unit();
2121
() = ();
2222
let _a: () = ();
23-
let _a: () = dummy();
23+
let _a: () = returns_unit();
2424
}
2525

2626
consume_units_with_for_loop(); // should be fine as well
@@ -30,7 +30,7 @@ fn main() {
3030
let_and_return!(()) // should be fine
3131
}
3232

33-
fn dummy() {}
33+
fn returns_unit() {}
3434

3535
// Related to issue #1964
3636
fn consume_units_with_for_loop() {
@@ -181,8 +181,6 @@ async fn issue10433() {
181181
pub async fn issue11502(a: ()) {}
182182

183183
pub fn issue12594() {
184-
fn returns_unit() {}
185-
186184
fn returns_result<T>(res: T) -> Result<T, ()> {
187185
Ok(res)
188186
}
@@ -200,10 +198,9 @@ pub fn issue12594() {
200198
}
201199

202200
fn issue15061() {
203-
fn return_unit() {}
204201
fn do_something(x: ()) {}
205202

206-
let res = return_unit();
203+
let res = returns_unit();
207204
//~^ let_unit_value
208205
do_something(res);
209206
println!("{res:?}");

tests/ui/let_unit.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ LL + };
5555
|
5656

5757
error: this let-binding has unit value
58-
--> tests/ui/let_unit.rs:192:9
58+
--> tests/ui/let_unit.rs:190:9
5959
|
6060
LL | let res = returns_unit();
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -69,15 +69,15 @@ LL ~ returns_result(()).unwrap();
6969
|
7070

7171
error: this let-binding has unit value
72-
--> tests/ui/let_unit.rs:206:5
72+
--> tests/ui/let_unit.rs:203:5
7373
|
74-
LL | let res = return_unit();
75-
| ^^^^^^^^^^^^^^^^^^^^^^^^
74+
LL | let res = returns_unit();
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
7676
|
7777
help: replace variable usages with `()`
7878
|
7979
LL ~ let res = ();
80-
LL ~ return_unit();
80+
LL ~ returns_unit();
8181
LL |
8282
LL ~ do_something(());
8383
|

0 commit comments

Comments
 (0)