rust/crates/ide_assists/src
bors[bot] e6728a8cd3
Merge #8415
8415: Fix faulty assertion when extracting function with macro call r=matklad a=brandondong

**Reproduction:**
```rust
fn main() {
    let n = 1;
    let k = n * n;
    dbg!(n);
}
```
1. Select the second and third lines of the main function. Use the "Extract into function" code assist.
2. Panic occurs in debug, error is logged in release: "[ERROR ide_assists::handlers::extract_function] assertion failed: matches!(path, ast :: Expr :: PathExpr(_))".
3. Function generates successfully on release where the panic was bypassed.
```rust
fn fun_name(n: i32) {
    let k = n * n;
    dbg!(n);
}
```

**Cause:**
- The generated function will take `n` as a parameter. The extraction logic needs to search the usages of `n` to determine whether it is used mutably or not. The helper `path_element_of_reference` is called for each usage but the second usage is a macro call and fails the `Expr::PathExpr(_)` match assertion.
- The caller of `path_element_of_reference` does implicitly assume it to be a `Expr::PathExpr(_)` in how it looks at its parent node for determining whether it is used mutably. This logic will not work for macros.
- I'm not sure if there are any other cases besides macros where it could be something other than a `Expr::PathExpr(_)`. I tried various examples and could not find any.

**Fix:**
- Update assertion to include the macro case.
- Add a FIXME to properly handle checking if a macro usage requires mutable access. For now, return false instead of running the existing logic that is tailored for `Expr::PathExpr(_)`'s.

Co-authored-by: Brandon <brandondong604@hotmail.com>
2021-04-13 11:39:03 +00:00
..
handlers Merge #8415 2021-04-13 11:39:03 +00:00
tests Convert Into to From assist 2021-04-03 15:48:35 +01:00
utils Compress tests 2021-03-02 18:35:52 +03:00
assist_config.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
assist_context.rs Use find_node_at_range 2021-03-29 13:17:49 +02:00
ast_transform.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
lib.rs internal: unfork code paths for unresolved and resolved assist 2021-04-13 10:59:15 +03:00
tests.rs Make group imports configurable 2021-03-07 10:15:17 +02:00
utils.rs Rename target_ty to self_ty 2021-03-29 17:47:47 +02:00