Skip to content

Commit

Permalink
Issue checkstyle#11807: fixes exception on records and fields in Requ…
Browse files Browse the repository at this point in the history
…ireThis
  • Loading branch information
rnveach committed Dec 2, 2022
1 parent 488ed2d commit 1196640
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Expand Up @@ -1562,10 +1562,18 @@ public boolean hasStaticMethod(final DetailAST ident) {
public boolean hasFinalField(final DetailAST instanceMember) {
boolean result = false;
for (DetailAST member : instanceMembers) {
final DetailAST mods = member.getParent().findFirstToken(TokenTypes.MODIFIERS);
final boolean finalMod = mods.findFirstToken(TokenTypes.FINAL) != null;
if (finalMod && isAstSimilar(member, instanceMember)) {
final DetailAST parent = member.getParent();
if (parent.getType() == TokenTypes.RECORD_COMPONENT_DEF) {
result = true;
}
else {
final DetailAST mods = parent.findFirstToken(TokenTypes.MODIFIERS);
final boolean finalMod = mods.findFirstToken(TokenTypes.FINAL) != null;
if (finalMod && isAstSimilar(member, instanceMember)) {
result = true;
}
}
if (result) {
break;
}
}
Expand Down
Expand Up @@ -471,6 +471,14 @@ public void testRecordsDefault() throws Exception {
expected);
}

@Test
public void testRecordsWithCheckFields() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getNonCompilablePath("InputRequireThisRecordsWithCheckFields.java"),
expected);
}

@Test
public void testLocalClassesInsideLambdas() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
Expand Down
@@ -0,0 +1,34 @@
/*
RequireThis
checkFields = true
checkMethods = (default)true
validateOnlyOverlapping = (default)true
*/

//non-compiled with javac: Compilable with Java14
package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;

public record InputRequireThisRecordsWithCheckFields(int a) {
void method() {
String a = "BUG";
a = a.substring(0, 1); // ok, field 'a' is final and cannot be changed
}
}
class InputRequireThisRecordsWithCheckFieldsClass {
public final int a;

public InputRequireThisRecordsWithCheckFieldsClass(int a) {
this.a = a;
}

void method() {
String a = "BUG";
a = a.substring(0, 1); // ok, field 'a' is final and cannot be changed
}

public int a() {
return a;
}
}

0 comments on commit 1196640

Please sign in to comment.