Skip to content

bug(sniper): fields and local variables with joint declaration cannot be snipered #3386

Description

@fermadeiral

Hi all,

I have the following code:

        int a, c = 0;

	void foo(int a) {
		a = a;
	}

I want to remove the statement a = a;, which is fine. However, after the sniper printer prints the code, I get: java.lang.IllegalStateException: Registering symbol: 'c' twice in the same scope. This happens because the printed code is:

        int a, c = 0;

	int c = 0;

	void foo(int a);

See, everything is fine with my transformation, but the variable c is printed twice. I created a failing test case to expose this issue, I hope it helps for fixing the bug:

@Test
	public void testPrintOneLineMultipleVariableDeclaration() {
		testSniper(OneLineMultipleVariableDeclaration.class.getName(), type -> {
			CtMethod<?> m = type.getMethodsByName("foo").get(0);
			m.setBody(null);
		}, (type, printed) -> {
			assertIsPrintedWithExpectedChanges(type, printed, "\\Qvoid foo(int a) {\n\t\ta = a;\n\t}", "void foo(int a);");
		});
	}

This is the content of the OneLineMultipleVariableDeclaration class:

package spoon.test.prettyprinter.testclasses;

public class OneLineMultipleVariableDeclaration {

	int a, c = 0;

	void foo(int a) {
		a = a;
	}
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions