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

Refactoring of guarded field still fails and now has gratuitous extra variable #1901

Open
ewillink opened this issue Jan 6, 2025 · 2 comments

Comments

@ewillink
Copy link

ewillink commented Jan 6, 2025

In the following example

`import java.util.ArrayList;

public class RefactorBug {
private ArrayList list = null;

public void updateBug() {
	if (list == null) {
		list = new ArrayList<>();
	}
	int index = 0;
	while (index < list.size()) {
		Object it = list.get(index++);
	}
}

}`

In the old days e.g. Eclipse 4.10, selecting the first "list" within the "if" and refactoring to extract a local variable used to extract the variable "list2"

`import java.util.ArrayList;

public class RefactorBug {
private ArrayList list = null;

public void updateBug() {
	ArrayList<Object> list2 = list;
	if (list2 == null) {
		list = new ArrayList<>();
	}
	int index = 0;
	while (index < list2.size()) {
		Object it = list2.get(index++);
	}
}

}`

There was a warning about changed assign semantics that was easily fixed by changing the new assignment to "list2 = list = ..."

Now e.g 2024-12 the same warning is given and the following bad code is produced

`import java.util.ArrayList;

public class RefactorBug {
private ArrayList list = null;

public void updateBug() {
	ArrayList<Object> list2 = list;
	if (list2 == null) {
		list = new ArrayList<>();
	}
	int index = 0;
	ArrayList<Object> list22 = list;
	while (index < list22.size()) {
		Object it = list22.get(index++);
	}
}

}`

The same "list2 = list = ..." fixup is possible but the gratuitous extra "list22" variable makes the flow needlessly hard to follow.

Please remove the gratuitous variable and correct the assignment from the outset.

@jukzi jukzi transferred this issue from eclipse-jdt/eclipse.jdt.core Jan 6, 2025
@jukzi
Copy link
Contributor

jukzi commented Jan 6, 2025

@ewillink please edit the issue such that the whole sources are in text blocks. It would also help if you add comments to the source code where exactly you have the (which?) issue.

@ewillink
Copy link
Author

ewillink commented Jan 6, 2025

The whole sources are in text (paired single quotes). Unfortunately GitHub rendering seems deficient. If I put in more single quotes to 'look' better you won't be able to cut and paste in one go. The example is so short, surely the deficiencies are obvious?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants