Skip to content

Check definitions of arguments before suggest move expr out of loop #142189

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jun 8, 2025

Fixes #141880

The first commit is used to add tests and the second shows the changes. For the test cases I added, there are three cases, and only the third case passes compilation after applying this suggestion. The changed code implements this.

r? compiler

xizheyin added 2 commits June 8, 2025 17:26
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 8, 2025
@fee1-dead
Copy link
Member

If I search for the string "consider moving the expression out of the loop so it is only moved once" from the codebase, I haven't seen a single suggestion that is correct in the context. I think it might just be better to remove this.

cc @compiler-errors if you have opinions

@fee1-dead
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 23, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 23, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@xizheyin
Copy link
Contributor Author

There does seem to be something werid about this suggestion. As I tried it, I realized that the triggers to make this suggestion correct seemed a bit harsh, but it was indeed the correct suggestion in this instance:
https://github.com/rust-lang/rust/blob/master/tests/ui/moves/recreating-value-in-loop-condition.rs
https://github.com/rust-lang/rust/blob/master/tests/ui/moves/recreating-value-in-loop-condition.stderr
What do you think about it?

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 23, 2025
@fee1-dead
Copy link
Member

I still don't think this is the correct approach. The original suggestion is very limited in what kinds of contexts would trigger a suggestion. For example,

fn foo() {
    let vec = vec!["one", "two", "three"];
    while let Some(item) = vec.into_iter().next() {
        println!("{:?}", item);
    }
}

This above doesn't trigger the suggestion even though it should. We have many false negatives and false positives. This should be rewritten by someone experienced but I don't have enough time/effort at the moment.

r? compiler

@rustbot rustbot assigned compiler-errors and unassigned fee1-dead Jun 25, 2025
@xizheyin
Copy link
Contributor Author

xizheyin commented Jun 25, 2025

I also agree that this usage scenario is very limited. Do we need to remove it now?

Maybe I can try to rewrite it, but I don't know if the difficulty is acceptable to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compiler makes a suggestion that doesn't actually work (moving a statement outside a loop)
4 participants