codemod(turbopack): Remove unused `Result<...>` return types from…
codemod(turbopack): Remove unused `Result<...>` return types from `#[turbo_task::function]`s (#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 #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?
Showing
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Please
register
or
sign in
to comment