This commit is contained in:
Ada Alakbarova 2025-09-29 19:21:21 +02:00
parent 3c93ba0c1d
commit c3e4d2bb90
No known key found for this signature in database
4 changed files with 40 additions and 25 deletions

View file

@ -89,15 +89,30 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, format_args: &FormatArgsStorag
walk_body(&mut visitor, body);
let mut has_in_format_capture = false;
suggestions.extend(visitor.spans.iter().filter_map(|span| match span {
suggestions.extend(visitor.spans.into_iter().filter_map(|span| match span {
MaybeInFormatCapture::Yes => {
has_in_format_capture = true;
None
},
MaybeInFormatCapture::No(span) => Some((*span, "()".to_string())),
MaybeInFormatCapture::No(span) => Some((span, "()".to_string())),
}));
if has_in_format_capture {
// In a case like this:
// ```
// let unit = returns_unit();
// eprintln!("{unit}");
// ```
// we can't remove the `unit` binding and replace its uses with a `()`,
// because the `eprintln!` would break.
//
// So do the following instead:
// ```
// let unit = ();
// returns_unit();
// eprintln!("{unit}");
// ```
// TODO: find a less awkward way to do this
suggestions.push((
init.span,
format!("();\n{}", reindent_multiline(&snip, false, indent_of(cx, local.span))),
@ -132,6 +147,12 @@ struct UnitVariableCollector<'a, 'tcx> {
macro_call: Option<&'a FormatArgs>,
}
/// Whether the unit variable is captured in a `format!`:
///
/// ```ignore
/// let unit = ();
/// eprintln!("{unit}");
/// ```
enum MaybeInFormatCapture {
Yes,
No(Span),

View file

@ -1,5 +1,5 @@
#![warn(clippy::let_unit_value)]
#![allow(unused, clippy::no_effect, clippy::needless_late_init, path_statements)]
#![allow(clippy::no_effect, clippy::needless_late_init, path_statements)]
macro_rules! let_and_return {
($n:expr) => {{
@ -15,12 +15,12 @@ fn main() {
if true {
// do not lint this, since () is explicit
let _a = ();
let () = dummy();
let () = returns_unit();
let () = ();
() = dummy();
() = returns_unit();
() = ();
let _a: () = ();
let _a: () = dummy();
let _a: () = returns_unit();
}
consume_units_with_for_loop(); // should be fine as well
@ -30,7 +30,7 @@ fn main() {
let_and_return!(()) // should be fine
}
fn dummy() {}
fn returns_unit() {}
// Related to issue #1964
fn consume_units_with_for_loop() {
@ -181,8 +181,6 @@ async fn issue10433() {
pub async fn issue11502(a: ()) {}
pub fn issue12594() {
fn returns_unit() {}
fn returns_result<T>(res: T) -> Result<T, ()> {
Ok(res)
}
@ -200,11 +198,10 @@ pub fn issue12594() {
}
fn issue15061() {
fn return_unit() {}
fn do_something(x: ()) {}
let res = ();
return_unit();
returns_unit();
//~^ let_unit_value
do_something(());
println!("{res:?}");

View file

@ -1,5 +1,5 @@
#![warn(clippy::let_unit_value)]
#![allow(unused, clippy::no_effect, clippy::needless_late_init, path_statements)]
#![allow(clippy::no_effect, clippy::needless_late_init, path_statements)]
macro_rules! let_and_return {
($n:expr) => {{
@ -15,12 +15,12 @@ fn main() {
if true {
// do not lint this, since () is explicit
let _a = ();
let () = dummy();
let () = returns_unit();
let () = ();
() = dummy();
() = returns_unit();
() = ();
let _a: () = ();
let _a: () = dummy();
let _a: () = returns_unit();
}
consume_units_with_for_loop(); // should be fine as well
@ -30,7 +30,7 @@ fn main() {
let_and_return!(()) // should be fine
}
fn dummy() {}
fn returns_unit() {}
// Related to issue #1964
fn consume_units_with_for_loop() {
@ -181,8 +181,6 @@ async fn issue10433() {
pub async fn issue11502(a: ()) {}
pub fn issue12594() {
fn returns_unit() {}
fn returns_result<T>(res: T) -> Result<T, ()> {
Ok(res)
}
@ -200,10 +198,9 @@ pub fn issue12594() {
}
fn issue15061() {
fn return_unit() {}
fn do_something(x: ()) {}
let res = return_unit();
let res = returns_unit();
//~^ let_unit_value
do_something(res);
println!("{res:?}");

View file

@ -55,7 +55,7 @@ LL + };
|
error: this let-binding has unit value
--> tests/ui/let_unit.rs:192:9
--> tests/ui/let_unit.rs:190:9
|
LL | let res = returns_unit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@ -69,15 +69,15 @@ LL ~ returns_result(()).unwrap();
|
error: this let-binding has unit value
--> tests/ui/let_unit.rs:206:5
--> tests/ui/let_unit.rs:203:5
|
LL | let res = return_unit();
| ^^^^^^^^^^^^^^^^^^^^^^^^
LL | let res = returns_unit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace variable usages with `()`
|
LL ~ let res = ();
LL ~ return_unit();
LL ~ returns_unit();
LL |
LL ~ do_something(());
|