Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract into function assist sometimes strips comments #13621

Closed
jplatte opened this issue Nov 15, 2022 · 6 comments
Closed

Extract into function assist sometimes strips comments #13621

jplatte opened this issue Nov 15, 2022 · 6 comments
Labels
A-assists C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now

Comments

@jplatte
Copy link
Contributor

jplatte commented Nov 15, 2022

I tried:

pub fn hello() {
    for _ in 0..1 {
        // Extract the following condition with this comment
        if true {
            continue;
        }

        if false {
            break;
        }
    }
}

and got

use std::ops::ControlFlow;

pub fn hello() {
    for _ in 0..1 {
        if let ControlFlow::Break(_) = fun_name() {
            continue;
        }

        if false {
            break;
        }
    }
}

fn fun_name() -> ControlFlow<()> {
    if true {
        return ControlFlow::Break(());
    }
    ControlFlow::Continue(())
}

when it should have been

use std::ops::ControlFlow;

pub fn hello() {
    for _ in 0..1 {
        if let ControlFlow::Break(_) = fun_name() {
            continue;
        }

        if false {
            break;
        }
    }
}

fn fun_name() -> ControlFlow<()> {
    // Extract the following condition with this comment
    if true {
        return ControlFlow::Break(());
    }
    ControlFlow::Continue(())
}

See also #13620 which is potentially related.

rust-analyzer version: rust-analyzer version: 0.4.1286-standalone

rustc version: rustc 1.65.0 (897e37553 2022-11-02)

relevant settings: -

@lowr lowr added S-actionable Someone could pick this issue up and work on it right now A-assists C-bug Category: bug labels Nov 16, 2022
@feniljain
Copy link
Contributor

Hey @jplatte 👋

I was exploring this issue and trying to replicate it, but when I tried:

pub fn hello() {
    for _ in 0..1 {
        $0// Extract the following condition with this comment
        if true {
            continue;
        }$0

        if false {
            break;
        }
    }
}

with this selection I couldn't find any code actions available, same when I added this as a test case, code action of extract_function was not applicable, am I missing something here? Would appreciate some help in repoducing the bug

@jplatte
Copy link
Contributor Author

jplatte commented Nov 29, 2022

Here's a failing test case: #13692

@feniljain
Copy link
Contributor

Heyo 👋,

Thanks for the failing test case, I see the difference between mine and yours was I had for loop while you directly used loop( mine too started working after this change, also interesting to see why ).

I see you created a draft PR, so I would guess you are already working it? Lemme know if that's not the case, I will pick it up then only :)

@jplatte
Copy link
Contributor Author

jplatte commented Dec 5, 2022

I just made it a PR so it's archived in a nice way (PRs survive deletion of the branch or even the whole repo). I'm not working on it.

@feniljain
Copy link
Contributor

Makes sense, thanks for the clarification! 🙇

bors added a commit that referenced this issue Dec 12, 2022
fix: make make_body respect comments in extract_function

Possible fix for #13621

### Points to help in review:

- Earlier we were only considering statements in a block expr and hence comments were being ignored, now we handle tokens hence making it aware of comments and then preserving them using `hacky_block_expr_with_comments`

Seems like I am not able to attach output video, github is glitching for it :(
@jplatte
Copy link
Contributor Author

jplatte commented Dec 12, 2022

The previously failing test was included on #13746 and passes now, so I'll close this.

@jplatte jplatte closed this as completed Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

3 participants