rust-analyzer/crates
bors[bot] 3fdf26a6fc
Merge #7898
7898: generate_function assist: infer return type r=JoshMcguigan a=JoshMcguigan

This PR makes two changes to the generate function assist:

1. Attempt to infer an appropriate return type for the generated function
2. If a return type is inferred, and that return type is not unit, don't render the snippet

```rust
fn main() {
    let x: u32 = foo$0();
    //              ^^^ trigger the assist to generate this function
}

// BEFORE
fn foo() ${0:-> ()} {
    todo!()
}

// AFTER (only change 1)
fn foo() ${0:-> u32} {
    todo!()
}

// AFTER (change  1 and 2, note the lack of snippet around the return type)
fn foo() -> u32 {
    todo!()
}
```

These changes are made as two commits, in case we want to omit change 2. I personally feel like it is a nice change, but I could understand there being some opposition.

#### Pros of change 2
If we are able to infer a return type, and especially if that return type is not the unit type, the return type is almost as likely to be correct as the argument names/types. I think this becomes even more true as people learn how this feature works.

#### Cons of change 2

We could never be as confident about the return type as we are about the function argument types, so it is more likely a user will want to change that. Plus it is a confusing UX to sometimes have the cursor highlight the return type after triggering this assist and sometimes not have that happen.

#### Why omit unit type?

The assumption is that if we infer the return type as unit, it is likely just because of the current structure of the code rather than that actually being the desired return type. However, this is obviously just a heuristic and will sometimes be wrong. But being wrong here just means falling back to the exact behavior that existed before this PR.



Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
2021-03-08 22:51:04 +00:00
..
base_db Fixed remaining references to AnalysisChange (now: Change) 2021-02-28 12:57:41 +01:00
cfg Upgrade expect-test to 1.1 2021-01-03 14:43:29 +01:00
flycheck Bump cargo_metadata 2021-03-02 14:27:29 +02:00
hir Hygiene is an internal implementation detail of the compiler 2021-03-08 22:14:52 +03:00
hir_def Use upstream cov-mark 2021-03-08 22:19:44 +02:00
hir_expand Fix assert split exprs on comma 2021-02-28 20:46:24 +08:00
hir_ty Use upstream cov-mark 2021-03-08 22:19:44 +02:00
ide Use upstream cov-mark 2021-03-08 22:19:44 +02:00
ide_assists Merge #7898 2021-03-08 22:51:04 +00:00
ide_completion Use upstream cov-mark 2021-03-08 22:19:44 +02:00
ide_db Use upstream cov-mark 2021-03-08 22:19:44 +02:00
ide_ssr Use upstream cov-mark 2021-03-08 22:19:44 +02:00
mbe Use upstream cov-mark 2021-03-08 22:19:44 +02:00
parser Fix fail to parse :: for meta in mbe 2021-03-06 04:57:34 +08:00
paths Document paths items 2021-01-22 15:38:33 +01:00
proc_macro_api Remove redundant clones 2021-02-05 16:57:26 +01:00
proc_macro_srv Use upstream cov-mark 2021-03-08 22:19:44 +02:00
proc_macro_test Add description for crates that will be published 2020-08-24 13:07:22 +02:00
profile add more counts 2021-01-27 12:39:19 +03:00
project_model Never run cargo check on the rustc source 2021-03-08 16:42:18 +00:00
rust-analyzer Make code generation just work 2021-03-08 21:45:37 +03:00
stdx Cleanup decl_check 2021-02-05 16:09:45 +01:00
syntax Use upstream cov-mark 2021-03-08 22:19:44 +02:00
test_utils Use upstream cov-mark 2021-03-08 22:19:44 +02:00
text_edit Avoid turning completion objects into builders 2020-11-16 23:16:41 +02:00
toolchain Add description for crates that will be published 2020-08-24 13:07:22 +02:00
tt Refactor TokenBuffer for reduc cloning 2021-01-05 02:00:46 +08:00
vfs Fix slow tests sometimes failing 2021-02-12 16:31:16 +01:00
vfs-notify Fix slow tests sometimes failing 2021-02-12 16:31:16 +01:00