Skip to content

Commit 0ec1d27

Browse files
bgwabhi12299
authored andcommitted
codemod(turbopack): Remove unused Result<...> return types from #[turbo_task::function]s (vercel#70492)
The `#[turbo_tasks::function]` macro always exposes a `impl Future<Output = Result<ReadRef<T>>>`, regardless of the function's actual return type. We only need to use a `Result<...>` return type when the function can fail. If it's infallible, this just adds noise. I left a bunch of these behind in vercel#70412, plus I think we had a lot of these independent of that PR. This cleans them up. ```yaml language: rust id: remove_unused_result_return_type utils: last-expression-inside-block: any: - inside: # single-line blocks just have the expression as the only child kind: block nthChild: position: 1 reverse: true - inside: # multi-line blocks wrap their final expression in an expression_statement kind: expression_statement inside: kind: block nthChild: position: 1 reverse: true ok-expression: kind: call_expression any: - pattern: Ok($_ARG) - pattern: $$$::Ok($_ARG) ok-expression-capturing: kind: call_expression any: - pattern: Ok($ARG) - pattern: $$$::Ok($ARG) # ast-grep does not appear to allow utils to be recursive, split out "simple blocks", and limit supported nesting simple-block-with-implicit-ok-return: kind: block has: nthChild: position: 1 reverse: true matches: ok-expression simple-expression-ok-value: any: - matches: simple-block-with-implicit-ok-return - kind: if_expression all: - has: field: consequence matches: simple-block-with-implicit-ok-return - has: field: alternative has: matches: simple-block-with-implicit-ok-return - kind: match_expression has: field: body not: has: kind: match_arm has: field: value not: any: - matches: ok-expression - matches: simple-block-with-implicit-ok-return block-with-implicit-ok-return: any: - matches: simple-block-with-implicit-ok-return - kind: block has: nthChild: position: 1 reverse: true any: - kind: expression_statement has: matches: simple-expression-ok-value - matches: simple-expression-ok-value # single-line blocks don't result-return-type: pattern: context: fn func() -> Result<$INNER_RET_TY> {} selector: generic_type infallible-fn: # this function only appears to return `Ok(...)`, it does not use try_expression (`?`) or `anyhow::bail!(...)` kind: function_item not: has: field: body any: - not: matches: block-with-implicit-ok-return - has: stopBy: kind: function_item any: - kind: try_expression - pattern: "?" inside: kind: macro_invocation stopBy: end - pattern: bail!($$$) - pattern: $$$::bail!($$$) - kind: return_expression not: has: matches: ok-expression rule: all: - pattern: $FUNC - kind: function_item has: field: return_type matches: result-return-type follows: pattern: context: | #[turbo_tasks::function] selector: attribute_item stopBy: not: kind: attribute_item - matches: infallible-fn rewriters: # this rewriter is far from perfect, and might rewrite too much - id: rewrite-return-type rule: matches: result-return-type inside: kind: function_item field: return_type fix: $INNER_RET_TY - id: unwrap-ok-values rule: all: - matches: ok-expression-capturing - any: - matches: last-expression-inside-block - inside: kind: return_expression - inside: kind: match_arm fix: $ARG transform: NEW_FUNC: rewrite: rewriters: - rewrite-return-type - unwrap-ok-values source: $FUNC fix: $NEW_FUNC ignores: - "**/turbo-tasks-testing/**" - "**/turbo-tasks-memory/tests/**" ``` ``` sg scan -U -r ../codemod_remove_unused_result_return_type.yml && cargo fix --lib --allow-dirty && cargo fmt ``` I used `cargo fix` in this case to auto-remove the now-unused `anyhow::Result` imports. I manually fixed clippy lints that now violated the `let_and_return` lint rule (using a separate commit inside this PR), as `cargo clippy --fix` seemed to hang when processing our `build.rs` files?
1 parent 98fbe93 commit 0ec1d27

File tree

103 files changed

+571
-643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+571
-643
lines changed

crates/next-api/src/app.rs

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,26 @@ impl AppProject {
152152
}
153153

154154
#[turbo_tasks::function]
155-
fn client_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
156-
Ok(get_client_module_options_context(
155+
fn client_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
156+
get_client_module_options_context(
157157
self.project().project_path(),
158158
self.project().execution_context(),
159159
self.project().client_compile_time_info().environment(),
160160
Value::new(self.client_ty()),
161161
self.project().next_mode(),
162162
self.project().next_config(),
163-
))
163+
)
164164
}
165165

166166
#[turbo_tasks::function]
167-
fn client_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
168-
Ok(get_client_resolve_options_context(
167+
fn client_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
168+
get_client_resolve_options_context(
169169
self.project().project_path(),
170170
Value::new(self.client_ty()),
171171
self.project().next_mode(),
172172
self.project().next_config(),
173173
self.project().execution_context(),
174-
))
174+
)
175175
}
176176

177177
#[turbo_tasks::function]
@@ -180,101 +180,101 @@ impl AppProject {
180180
}
181181

182182
#[turbo_tasks::function]
183-
fn client_transition(self: Vc<Self>) -> Result<Vc<FullContextTransition>> {
183+
fn client_transition(self: Vc<Self>) -> Vc<FullContextTransition> {
184184
let module_context = self.client_module_context();
185-
Ok(FullContextTransition::new(module_context))
185+
FullContextTransition::new(module_context)
186186
}
187187

188188
#[turbo_tasks::function]
189-
fn rsc_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
190-
Ok(get_server_module_options_context(
189+
fn rsc_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
190+
get_server_module_options_context(
191191
self.project().project_path(),
192192
self.project().execution_context(),
193193
Value::new(self.rsc_ty()),
194194
self.project().next_mode(),
195195
self.project().next_config(),
196196
NextRuntime::NodeJs,
197-
))
197+
)
198198
}
199199

200200
#[turbo_tasks::function]
201-
fn edge_rsc_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
202-
Ok(get_server_module_options_context(
201+
fn edge_rsc_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
202+
get_server_module_options_context(
203203
self.project().project_path(),
204204
self.project().execution_context(),
205205
Value::new(self.rsc_ty()),
206206
self.project().next_mode(),
207207
self.project().next_config(),
208208
NextRuntime::Edge,
209-
))
209+
)
210210
}
211211

212212
#[turbo_tasks::function]
213-
fn route_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
214-
Ok(get_server_module_options_context(
213+
fn route_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
214+
get_server_module_options_context(
215215
self.project().project_path(),
216216
self.project().execution_context(),
217217
Value::new(self.route_ty()),
218218
self.project().next_mode(),
219219
self.project().next_config(),
220220
NextRuntime::NodeJs,
221-
))
221+
)
222222
}
223223

224224
#[turbo_tasks::function]
225-
fn edge_route_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
226-
Ok(get_server_module_options_context(
225+
fn edge_route_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
226+
get_server_module_options_context(
227227
self.project().project_path(),
228228
self.project().execution_context(),
229229
Value::new(self.route_ty()),
230230
self.project().next_mode(),
231231
self.project().next_config(),
232232
NextRuntime::Edge,
233-
))
233+
)
234234
}
235235

236236
#[turbo_tasks::function]
237-
fn rsc_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
238-
Ok(get_server_resolve_options_context(
237+
fn rsc_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
238+
get_server_resolve_options_context(
239239
self.project().project_path(),
240240
Value::new(self.rsc_ty()),
241241
self.project().next_mode(),
242242
self.project().next_config(),
243243
self.project().execution_context(),
244-
))
244+
)
245245
}
246246

247247
#[turbo_tasks::function]
248-
fn edge_rsc_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
249-
Ok(get_edge_resolve_options_context(
248+
fn edge_rsc_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
249+
get_edge_resolve_options_context(
250250
self.project().project_path(),
251251
Value::new(self.rsc_ty()),
252252
self.project().next_mode(),
253253
self.project().next_config(),
254254
self.project().execution_context(),
255-
))
255+
)
256256
}
257257

258258
#[turbo_tasks::function]
259-
fn route_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
260-
Ok(get_server_resolve_options_context(
259+
fn route_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
260+
get_server_resolve_options_context(
261261
self.project().project_path(),
262262
Value::new(self.route_ty()),
263263
self.project().next_mode(),
264264
self.project().next_config(),
265265
self.project().execution_context(),
266-
))
266+
)
267267
}
268268

269269
#[turbo_tasks::function]
270-
fn edge_route_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
271-
Ok(get_edge_resolve_options_context(
270+
fn edge_route_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
271+
get_edge_resolve_options_context(
272272
self.project().project_path(),
273273
Value::new(self.route_ty()),
274274
self.project().next_mode(),
275275
self.project().next_config(),
276276
self.project().execution_context(),
277-
))
277+
)
278278
}
279279

280280
#[turbo_tasks::function]
@@ -444,49 +444,49 @@ impl AppProject {
444444
}
445445

446446
#[turbo_tasks::function]
447-
fn ssr_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
448-
Ok(get_server_module_options_context(
447+
fn ssr_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
448+
get_server_module_options_context(
449449
self.project().project_path(),
450450
self.project().execution_context(),
451451
Value::new(self.ssr_ty()),
452452
self.project().next_mode(),
453453
self.project().next_config(),
454454
NextRuntime::NodeJs,
455-
))
455+
)
456456
}
457457

458458
#[turbo_tasks::function]
459-
fn edge_ssr_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
460-
Ok(get_server_module_options_context(
459+
fn edge_ssr_module_options_context(self: Vc<Self>) -> Vc<ModuleOptionsContext> {
460+
get_server_module_options_context(
461461
self.project().project_path(),
462462
self.project().execution_context(),
463463
Value::new(self.ssr_ty()),
464464
self.project().next_mode(),
465465
self.project().next_config(),
466466
NextRuntime::Edge,
467-
))
467+
)
468468
}
469469

470470
#[turbo_tasks::function]
471-
fn ssr_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
472-
Ok(get_server_resolve_options_context(
471+
fn ssr_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
472+
get_server_resolve_options_context(
473473
self.project().project_path(),
474474
Value::new(self.ssr_ty()),
475475
self.project().next_mode(),
476476
self.project().next_config(),
477477
self.project().execution_context(),
478-
))
478+
)
479479
}
480480

481481
#[turbo_tasks::function]
482-
fn edge_ssr_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
483-
Ok(get_edge_resolve_options_context(
482+
fn edge_ssr_resolve_options_context(self: Vc<Self>) -> Vc<ResolveOptionsContext> {
483+
get_edge_resolve_options_context(
484484
self.project().project_path(),
485485
Value::new(self.ssr_ty()),
486486
self.project().next_mode(),
487487
self.project().next_config(),
488488
self.project().execution_context(),
489-
))
489+
)
490490
}
491491

492492
#[turbo_tasks::function]
@@ -530,11 +530,8 @@ impl AppProject {
530530
}
531531

532532
#[turbo_tasks::function]
533-
fn runtime_entries(self: Vc<Self>) -> Result<Vc<RuntimeEntries>> {
534-
Ok(get_server_runtime_entries(
535-
Value::new(self.rsc_ty()),
536-
self.project().next_mode(),
537-
))
533+
fn runtime_entries(self: Vc<Self>) -> Vc<RuntimeEntries> {
534+
get_server_runtime_entries(Value::new(self.rsc_ty()), self.project().next_mode())
538535
}
539536

540537
#[turbo_tasks::function]
@@ -558,15 +555,15 @@ impl AppProject {
558555
}
559556

560557
#[turbo_tasks::function]
561-
fn client_runtime_entries(self: Vc<Self>) -> Result<Vc<EvaluatableAssets>> {
562-
Ok(get_client_runtime_entries(
558+
fn client_runtime_entries(self: Vc<Self>) -> Vc<EvaluatableAssets> {
559+
get_client_runtime_entries(
563560
self.project().project_path(),
564561
Value::new(self.client_ty()),
565562
self.project().next_mode(),
566563
self.project().next_config(),
567564
self.project().execution_context(),
568565
)
569-
.resolve_entries(Vc::upcast(self.client_module_context())))
566+
.resolve_entries(Vc::upcast(self.client_module_context()))
570567
}
571568

572569
#[turbo_tasks::function]

crates/next-api/src/global_module_id_strategy.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ impl GlobalModuleIdStrategyBuilder {
8484
// NOTE(LichuAcu) We can't move this function to `turbopack-core` because we need access to
8585
// `Endpoint`, which is not available there.
8686
#[turbo_tasks::function]
87-
fn preprocess_module_ids(
88-
endpoint: Vc<Box<dyn Endpoint>>,
89-
) -> Result<Vc<PreprocessedChildrenIdents>> {
87+
fn preprocess_module_ids(endpoint: Vc<Box<dyn Endpoint>>) -> Vc<PreprocessedChildrenIdents> {
9088
let root_modules = endpoint.root_modules();
91-
Ok(children_modules_idents(root_modules))
89+
children_modules_idents(root_modules)
9290
}

crates/next-api/src/instrumentation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl InstrumentationEndpoint {
6565
}
6666

6767
#[turbo_tasks::function]
68-
fn core_modules(&self) -> Result<Vc<InstrumentationCoreModules>> {
68+
fn core_modules(&self) -> Vc<InstrumentationCoreModules> {
6969
let userland_module = self
7070
.asset_context
7171
.process(
@@ -81,11 +81,11 @@ impl InstrumentationEndpoint {
8181
"instrumentation".into(),
8282
);
8383

84-
Ok(InstrumentationCoreModules {
84+
InstrumentationCoreModules {
8585
userland_module,
8686
edge_entry_module,
8787
}
88-
.cell())
88+
.cell()
8989
}
9090

9191
#[turbo_tasks::function]

crates/next-api/src/middleware.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,13 @@ impl MiddlewareEndpoint {
255255
}
256256

257257
#[turbo_tasks::function]
258-
fn userland_module(&self) -> Result<Vc<Box<dyn Module>>> {
259-
Ok(self
260-
.asset_context
258+
fn userland_module(&self) -> Vc<Box<dyn Module>> {
259+
self.asset_context
261260
.process(
262261
self.source,
263262
Value::new(ReferenceType::Entry(EntryReferenceSubType::Middleware)),
264263
)
265-
.module())
264+
.module()
266265
}
267266
}
268267

@@ -311,7 +310,7 @@ impl Endpoint for MiddlewareEndpoint {
311310
}
312311

313312
#[turbo_tasks::function]
314-
fn root_modules(self: Vc<Self>) -> Result<Vc<Modules>> {
315-
Ok(Vc::cell(vec![self.userland_module()]))
313+
fn root_modules(self: Vc<Self>) -> Vc<Modules> {
314+
Vc::cell(vec![self.userland_module()])
316315
}
317316
}

0 commit comments

Comments
 (0)