Skip to content

Commit 95c45be

Browse files
committed
Auto merge of #12603 - m-rph:12594, r=Manishearth
Elide unit variables linted by `let_unit` and use `()` directly instead Situation: `let_unit` lints when an expression binds a unit (`()`) to a variable. In some cases this binding may be passed down to another function. Currently, the lint removes the binding without considering usage. fixes: #12594 changelog: Suggestion Fix [`let_unit`]. Clippy will remove unit bindings and replace all their instances in the body with `()`.
2 parents 2b30a59 + eee4db9 commit 95c45be

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

clippy_lints/src/unit_types/let_unit_value.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_context;
3-
use clippy_utils::visitors::{for_each_local_assignment, for_each_value_source};
3+
use clippy_utils::visitors::{for_each_local_assignment, for_each_value_source, is_local_used};
44
use core::ops::ControlFlow;
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
7+
use rustc_hir::intravisit::{walk_body, Visitor};
78
use rustc_hir::{Expr, ExprKind, HirId, HirIdSet, Local, MatchSource, Node, PatKind, QPath, TyKind};
89
use rustc_lint::{LateContext, LintContext};
910
use rustc_middle::lint::{in_external_macro, is_from_async_await};
1011
use rustc_middle::ty;
1112

1213
use super::LET_UNIT_VALUE;
1314

14-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
15+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
1516
// skip `let () = { ... }`
1617
if let PatKind::Tuple(fields, ..) = local.pat.kind
1718
&& fields.is_empty()
@@ -75,12 +76,53 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
7576
let snip = snippet_with_context(cx, expr.span, local.span.ctxt(), "()", &mut app).0;
7677
diag.span_suggestion(local.span, "omit the `let` binding", format!("{snip};"), app);
7778
}
79+
80+
if let PatKind::Binding(_, binding_hir_id, ident, ..) = local.pat.kind
81+
&& let Some(body_id) = cx.enclosing_body.as_ref()
82+
&& let body = cx.tcx.hir().body(*body_id)
83+
&& is_local_used(cx, body, binding_hir_id)
84+
{
85+
let identifier = ident.as_str();
86+
let mut visitor = UnitVariableCollector::new(binding_hir_id);
87+
walk_body(&mut visitor, body);
88+
visitor.spans.into_iter().for_each(|span| {
89+
let msg =
90+
format!("variable `{identifier}` of type `()` can be replaced with explicit `()`");
91+
diag.span_suggestion(span, msg, "()", Applicability::MachineApplicable);
92+
});
93+
}
7894
},
7995
);
8096
}
8197
}
8298
}
8399

100+
struct UnitVariableCollector {
101+
id: HirId,
102+
spans: Vec<rustc_span::Span>,
103+
}
104+
105+
impl UnitVariableCollector {
106+
fn new(id: HirId) -> Self {
107+
Self { id, spans: vec![] }
108+
}
109+
}
110+
111+
/**
112+
* Collect all instances where a variable is used based on its `HirId`.
113+
*/
114+
impl<'tcx> Visitor<'tcx> for UnitVariableCollector {
115+
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) -> Self::Result {
116+
if let ExprKind::Path(QPath::Resolved(None, path)) = ex.kind
117+
&& let Res::Local(id) = path.res
118+
&& id == self.id
119+
{
120+
self.spans.push(path.span);
121+
}
122+
rustc_hir::intravisit::walk_expr(self, ex);
123+
}
124+
}
125+
84126
/// Checks sub-expressions which create the value returned by the given expression for whether
85127
/// return value inference is needed. This checks through locals to see if they also need inference
86128
/// at this point.

tests/ui/let_unit.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,21 @@ async fn issue10433() {
177177
}
178178

179179
pub async fn issue11502(a: ()) {}
180+
181+
pub fn issue12594() {
182+
fn returns_unit() {}
183+
184+
fn returns_result<T>(res: T) -> Result<T, ()> {
185+
Ok(res)
186+
}
187+
188+
fn actual_test() {
189+
// create first a unit value'd value
190+
returns_unit();
191+
returns_result(()).unwrap();
192+
returns_result(()).unwrap();
193+
// make sure we replace only the first variable
194+
let res = 1;
195+
returns_result(res).unwrap();
196+
}
197+
}

tests/ui/let_unit.rs

+18
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,21 @@ async fn issue10433() {
177177
}
178178

179179
pub async fn issue11502(a: ()) {}
180+
181+
pub fn issue12594() {
182+
fn returns_unit() {}
183+
184+
fn returns_result<T>(res: T) -> Result<T, ()> {
185+
Ok(res)
186+
}
187+
188+
fn actual_test() {
189+
// create first a unit value'd value
190+
let res = returns_unit();
191+
returns_result(res).unwrap();
192+
returns_result(res).unwrap();
193+
// make sure we replace only the first variable
194+
let res = 1;
195+
returns_result(res).unwrap();
196+
}
197+
}

tests/ui/let_unit.stderr

+20-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,24 @@ LL + Some(_) => (),
5151
LL + };
5252
|
5353

54-
error: aborting due to 3 previous errors
54+
error: this let-binding has unit value
55+
--> tests/ui/let_unit.rs:190:9
56+
|
57+
LL | let res = returns_unit();
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
59+
|
60+
help: omit the `let` binding
61+
|
62+
LL | returns_unit();
63+
|
64+
help: variable `res` of type `()` can be replaced with explicit `()`
65+
|
66+
LL | returns_result(()).unwrap();
67+
| ~~
68+
help: variable `res` of type `()` can be replaced with explicit `()`
69+
|
70+
LL | returns_result(()).unwrap();
71+
| ~~
72+
73+
error: aborting due to 4 previous errors
5574

0 commit comments

Comments
 (0)