rust-analyzer/crates/ide_assists/src/handlers
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
..
add_explicit_type.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
add_lifetime_to_type.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
add_missing_impl_members.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
add_turbo_fish.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
apply_demorgan.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
auto_import.rs Make group imports configurable 2021-03-07 10:15:17 +02:00
change_visibility.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
convert_comment_block.rs Apply edits 2021-03-01 11:41:22 -08:00
convert_integer_literal.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
early_return.rs De Morgan's Law assist now correctly inverts <, <=, >, >=. 2021-02-24 11:58:37 +01:00
expand_glob_import.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
extract_function.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
extract_struct_from_enum_variant.rs Hygiene is an internal implementation detail of the compiler 2021-03-08 22:14:52 +03:00
extract_variable.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
fill_match_arms.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
fix_visibility.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
flip_binexpr.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
flip_comma.rs Disable "Flip comma" assist inside a macro call 2021-02-26 19:12:53 +02:00
flip_trait_bound.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
generate_default_from_enum_variant.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
generate_default_from_new.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
generate_derive.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
generate_enum_is_method.rs rename generate_enum_match_method file to match assist name 2021-02-27 12:21:56 +02:00
generate_enum_projection_method.rs generate try_into instead of into 2021-02-27 12:21:57 +02:00
generate_from_impl_for_enum.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
generate_function.rs generate_function assist don't render snippet if ret type inferred 2021-03-08 14:38:36 -08:00
generate_getter.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
generate_getter_mut.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
generate_impl.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
generate_new.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
generate_setter.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
infer_function_return_type.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
inline_function.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
inline_local_variable.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
introduce_named_lifetime.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
invert_if.rs De Morgan's Law assist now correctly inverts <, <=, >, >=. 2021-02-24 11:58:37 +01:00
merge_imports.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
merge_match_arms.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
move_bounds.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
move_guard.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
move_module_to_file.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
pull_assignment_up.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
qualify_path.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
raw_string.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
remove_dbg.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
remove_mut.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
remove_unused_param.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
reorder_fields.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
reorder_impl.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
replace_derive_with_manual_impl.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
replace_for_loop_with_for_each.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
replace_if_let_with_match.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
replace_impl_trait_with_generic.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
replace_let_with_if_let.rs Don't spam loop-rewriting assist 2021-02-28 21:08:51 +03:00
replace_qualified_name_with_use.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
replace_string_with_char.rs Only replace quotes in replace_string_with_char assist 2021-03-06 21:21:18 +01:00
replace_unwrap_with_match.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
split_import.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
toggle_ignore.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
unmerge_use.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00
unwrap_block.rs 7526: Rename crate assists to ide_assists. 2021-02-23 00:59:16 +05:30
wrap_return_type_in_result.rs Use upstream cov-mark 2021-03-08 22:19:44 +02:00