bug(sniper): fields and local variables with joint declaration cannot be snipered
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
Hi all,
I have the following code:
```java
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:
```java
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:
```java
@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:
```java
package spoon.test.prettyprinter.testclasses;
public class OneLineMultipleVariableDeclaration {
int a, c = 0;
void foo(int a) {
a = a;
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.