Skip to content

Commit

Permalink
SONARJAVA-5116 S1105 sometimes falsely requests the curly brace to be…
Browse files Browse the repository at this point in the history
… moved (#4874)
  • Loading branch information
pauloreilly-sonarsource authored Sep 24, 2024
1 parent 1df6e7e commit fa31182
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package checks;

record LeftCurlyBraceEndLineCheck_record(Integer value) {

// Test https://sonarsource.atlassian.net/browse/SONARJAVA-5116
public LeftCurlyBraceEndLineCheck_record(String v) { // Compliant
// but <- Complains about this curly brace
this(Integer.parseInt(v));
}

public static final String FOR_SOME_REASON_THIS_NEEDS_TO_EXIST_HERE_TO_REPRODUCE_ISSUE = "IDK";
public static final String FOR_SOME_REASON_THIS_NEEDS_TO_EXIST_HERE_TO_REPRODUCE_ISSUE_2 = "IDK";

}

record TestRecordSyntaxV2(String value) {

// Test https://sonarsource.atlassian.net/browse/SONARJAVA-5116
private void doThing() { // Compliant
// but <- Complains about this curly brace
new LeftCurlyBraceEndLineCheck_record("brace");
}

public record ThisRecordCausesTheIssueAsWell(String value) {
}

}

record TestRecordSyntaxV3(Integer value)
{ // Noncompliant
public TestRecordSyntaxV3(String v)
{ // Noncompliant
this(Integer.parseInt(v));
}
public static final String CONST_1 = "IDK";
public static final String CONST_2 = "IDK";
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ void java_17() {
.verifyIssues();
}

@Test
void test_record() {
CheckVerifier.newVerifier()
.onFile(TestUtils.mainCodeSourcesPath("checks/LeftCurlyBraceEndLineCheck_record.java"))
.withCheck(new LeftCurlyBraceEndLineCheck())
.verifyIssues();
}

@Test
void detected_switch_expressions() {
CheckVerifier.newVerifier()
Expand Down
11 changes: 10 additions & 1 deletion java-frontend/src/main/java/org/sonar/java/model/JParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,16 @@ private int findLeftBraceTokenIndex(AbstractTypeDeclaration e) {
return tokenManager.lastIndexIn(e, TerminalTokens.TokenNameLBRACE);
}
if (!e.bodyDeclarations().isEmpty()) {
return tokenManager.firstIndexBefore((ASTNode) e.bodyDeclarations().get(0), TerminalTokens.TokenNameLBRACE);
// for records, bodyDeclarations may not be in the order encountered in file, for classes they are
List<BodyDeclaration> bodyDeclarations = e.bodyDeclarations();
BodyDeclaration firstDeclaration = bodyDeclarations.get(0);
for (int i = 1; i < bodyDeclarations.size(); i++) {
BodyDeclaration declaration = bodyDeclarations.get(i);
if (firstDeclaration.getStartPosition() > declaration.getStartPosition()) {
firstDeclaration = declaration;
}
}
return tokenManager.firstIndexBefore(firstDeclaration, TerminalTokens.TokenNameLBRACE);
}
return tokenManager.lastIndexIn(e, TerminalTokens.TokenNameLBRACE);
}
Expand Down

0 comments on commit fa31182

Please sign in to comment.