Commit Graph

564 Commits

Author SHA1 Message Date
Ryan Mehri
146a7cc490 fix: allow more kinds of if let patterns in guarded return assist 2023-10-01 11:48:10 -07:00
bors
87e2c310f9 Auto merge of #15667 - rmehri01:bool_to_enum_top_level, r=Veykril
fix: make bool_to_enum assist create enum at top-level

This pr makes the `bool_to_enum` assist create the `enum` at the next closest module block or at top-level, which fixes a few tricky cases such as with an associated `const` in a trait or module:

```rust
trait Foo {
    const $0BOOL: bool;
}

impl Foo for usize {
    const BOOL: bool = true;
}

fn main() {
    if <usize as Foo>::BOOL {
        println!("foo");
    }
}
```

Which now properly produces:

```rust
#[derive(PartialEq, Eq)]
enum Bool { True, False }

trait Foo {
    const BOOL: Bool;
}

impl Foo for usize {
    const BOOL: Bool = Bool::True;
}

fn main() {
    if <usize as Foo>::BOOL == Bool::True {
        println!("foo");
    }
}
```

I also think it's a bit nicer, especially for local variables, but didn't really know to do it in the first PR :)
2023-09-29 10:20:11 +00:00
Ryan Mehri
1b3e5b2105 style: simplify node_to_insert_before 2023-09-28 10:09:13 -07:00
bors
3b1b58c225 Auto merge of #15662 - rmehri01:fix_panic_with_return_in_match, r=Veykril
fix: panic with wrapping/unwrapping result return type assists

With the `wrap_return_type_in_result` assist, the following code results in a panic (note the lack of a semicolon):

```rust
fn foo(num: i32) -> $0i32 {
    return num
}

=>

thread 'handlers::wrap_return_type_in_result::tests::wrap_return_in_tail_position' panicked at crates/syntax/src/ted.rs:137:41:
called `Option::unwrap()` on a `None` value
```

I think this is because it first walks the body expression to change any `return` expressions and then walks all tail expressions, resulting in the `return num` being changed twice since it is both a `return` and in tail position. This can also happen when a `match` is in tail position and `return` is used in a branch for example. Not really sure how big of an issue this is in practice though since this seems to be the only case that is impacted and can be reduced to just `num` instead of `return num`.

This also occurs with the `unwrap_result_return_type` assist but panics with the following instead:

```
thread 'handlers::unwrap_result_return_type::tests::wrap_return_in_tail_position' panicked at /rustc/3223b0b5e8dadda3f76c3fd1a8d6c5addc09599e/library/alloc/src/string.rs:1766:29:
assertion failed: self.is_char_boundary(n)
```
2023-09-26 14:18:33 +00:00
Ryan Mehri
73150c3f36 fix: wrap method call exprs in parens 2023-09-25 21:44:16 -07:00
Ryan Mehri
bce4be9478 fix: make bool_to_enum assist create enum at top-level 2023-09-25 21:01:54 -07:00
Milo
85ead6ec27 remove other unwraps 2023-09-25 11:48:23 +00:00
Milo
f64eecd2e2 fix one 2023-09-25 11:30:21 +00:00
Ryan Mehri
7306504b82 fix panic with wrapping/unwrapping result return type assists 2023-09-24 16:00:55 -07:00
bors
8139e8e072 Auto merge of #15425 - alibektas:deunwrap/convert_comment_block, r=Veykril
minor : Deunwrap convert_comment_block and desugar_doc_comment

Closes subtask 13 of #15398 . I still don't know a more idiomatic way for the for loops I added, any suggestion would make me happy.
2023-09-22 15:47:55 +00:00
bors
59bcbafc95 Auto merge of #15594 - alibektas:deunwrap/add_missing_match_arms, r=Veykril
Deunwrap add_missing_match_arms

Last subtask of #15398
2023-09-22 15:31:30 +00:00
Ali Bektas
622e1a8d88 Add a test case to add_missing_match_arms
Although it doesn't panic now, further changes to how we recover from incomplete syntax
may cause this assist to panic. To mitigate this a test case has been added.
2023-09-22 13:51:19 +02:00
Ali Bektas
0a91a54794 v4 2023-09-22 13:32:20 +02:00
bors
df75809a85 Auto merge of #15484 - rmehri01:14779_bool_to_enum_assist, r=Veykril
feat: Bool to enum assist

This adds the `bool_to_enum` assist, which converts the type of boolean local variables, fields, constants and statics to a new `enum` type, making it easier to distinguish the meaning of `true` and `false` by renaming the variants.

Closes #14779
2023-09-22 07:19:12 +00:00
bors
2ededa2f14 Auto merge of #15432 - alibektas:deunwrap/inline_call, r=Veykril
minor : Deunwrap inline call

#15398 subtask 4. There is still one instance of unwrap, which I found pretty hard to change.
2023-09-22 07:03:02 +00:00
Lukas Wirth
93562dd5bd Use parent + and_then instead of ancestors 2023-09-22 08:53:24 +02:00
Ryan Mehri
ea11846490 fix parens when inlining closure in body of function 2023-09-21 21:55:10 -07:00
Ryan Mehri
60f7473c99 fix parens when inlining closure local variables 2023-09-21 21:31:15 -07:00
Kevin Reid
cac796acb3 Give unmerge_use a label explaining what it will affect. 2023-09-16 13:29:03 -07:00
bors
9d0ccf01a1 Auto merge of #15597 - rmehri01:fix_promote_local_field_shorthand, r=HKalbasi
Field shorthand overwritten in promote local to const assist

Currently, running `promote_local_to_const` on the following:

```rust
struct Foo {
    bar: usize,
}

fn main() {
    let $0bar = 0;
    let foo = Foo { bar };
}
```

Results in:

```rust
struct Foo {
    bar: usize,
}

fn main() {
    const BAR: usize = 0;
    let foo = Foo { BAR };
}
```

But instead should be something like:

```rust
struct Foo {
    bar: usize,
}

fn main() {
    const BAR: usize = 0;
    let foo = Foo { bar: BAR };
}
```
2023-09-16 16:48:21 +00:00
Ryan Mehri
cd0a89ac4f fix: field shorthand overwritten in promote local to const assist 2023-09-11 10:59:23 -07:00
Ali Bektas
145a101fe8 Deunwrap add_missing_match_arms 2023-09-11 14:09:19 +02:00
Ali Bektas
893e19137e Make assist lazy again 2023-09-11 13:33:26 +02:00
Ali Bektas
0863024b1a Make assist lazy again 2023-09-11 13:31:42 +02:00
Ryan Mehri
25b1b3e753 feat: add support for other ADT types and destructuring patterns 2023-09-10 22:21:12 -07:00
Ali Bektas
2fdf7e4b75 v3 2023-09-10 23:15:37 +02:00
Ali Bektas
a66dbd11ed v2 2023-09-10 23:15:05 +02:00
Ali Bektas
b316bccc97 replace for loops with sth more idiomatic 2023-09-10 23:15:05 +02:00
Ali Bektas
9c6257138d Deunwrap convert_comment_block 2023-09-10 23:15:05 +02:00
Ali Bektas
0f1673c6f1 v3 2023-09-10 23:00:19 +02:00
Ali Bektas
6f7460484a v2 2023-09-10 22:45:09 +02:00
Ali Bektas
35e0d800f0 Deunwrap extract_function 2023-09-10 22:45:09 +02:00
Ali Bektas
38491fcf07 v3 2023-09-10 22:39:07 +02:00
Ali Bektas
68d24b69d4 Deunwrap inline call v2 2023-09-10 22:39:07 +02:00
Ali Bektas
5683df2965 Deunwrap inline call 2023-09-10 22:39:07 +02:00
Ryan Mehri
7ba2e130b9 fix: add checks for overwriting incorrect ancestor 2023-09-10 00:21:11 -07:00
Ryan Mehri
2e13aed3bc feat: support cross module imports 2023-09-09 11:59:59 -07:00
Ryan Mehri
136a9dbe36 style: rename some locals 2023-09-09 11:59:59 -07:00
Ryan Mehri
455dacfd3b fix: only trigger assist on Name 2023-09-09 11:59:59 -07:00
Ryan Mehri
91ac1d6194 fix: initializing struct multiple times 2023-09-09 11:59:59 -07:00
Ryan Mehri
83196fd4d9 fix: remove trailing whitespace 2023-09-09 11:59:59 -07:00
Ryan Mehri
59738d5fd5 fix: add generated doctest 2023-09-09 11:59:58 -07:00
Ryan Mehri
d7a8e800c9 feat: initial version of bool_to_enum assist 2023-09-09 11:58:19 -07:00
bors
829e77709e Auto merge of #15524 - vsrs:bind_unused_param, r=Veykril
Bind unused parameter assistant

This PR introduces a new **Bind unused parameter assistant**.

While we do have a QuickFix from `rustc` (prefixing the parameter with an underscore), it's sometimes more convenient to suppress the warning using the following approach:
```rust
 fn some_function(unused: i32) {}
```
->
```rust
fn some_function(unused: i32) {
    let _ = unused;
}
```
2023-09-08 09:12:31 +00:00
bors
3325622230 Auto merge of #15430 - alibektas:deunwrap/wrap_return_type_in_result, r=Veykril
minor : Deunwrap wrap_return_type_in_result

#15398 subtask 7
2023-09-08 08:24:29 +00:00
bors
47e0d07eb0 Auto merge of #15573 - alibektas:15539/into_to_from, r=Veykril
assist : `into_to_qualified_from`

fixes #15539. This assist converts an `.into()` call into an explicit fully qualified `from()` call.
2023-09-08 07:44:24 +00:00
Ali Bektas
9762f764ae Add assist into_to_qualified_from
This assist converts an `.into()` call into an explicit
fully qualified from call.
2023-09-07 23:43:46 +02:00
Lukas Wirth
5046889f43 Don't allocate the format_args template string as an expression 2023-09-06 19:18:12 +02:00
Lukas Wirth
c0e402637e Emit builtin#format_args in builtin format_args expander 2023-09-06 18:08:20 +02:00
Lukas Wirth
9b8eb807a3 Parse builtin# syntax 2023-09-05 10:36:35 +02:00