Skip to content

Commit

Permalink
Add a check to prevent record components of array type.
Browse files Browse the repository at this point in the history
The default `equals` and `hashCode` for a record with an array component will be inaccurate. Also, records should be immutable, and non-empty arrays are mutable.

PiperOrigin-RevId: 673606772
  • Loading branch information
eamonnmcmanus authored and Error Prone Team committed Sep 12, 2024
1 parent fcd3939 commit de6deb3
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;

import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.VariableTree;
import javax.lang.model.type.TypeKind;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(
summary =
"Record components should not be arrays. The default equals and hashCode for a record with"
+ " an array component will be inaccurate. Also, records should be immutable, and"
+ " non-empty arrays are mutable.",
severity = ERROR)
public final class ArrayRecordComponent extends BugChecker implements VariableTreeMatcher {
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
var sym = ASTHelpers.getSymbol(tree);
// isRecord(VarSymbol) is true iff the symbol represents a record component
if (ASTHelpers.isRecord(sym) && sym.asType().getKind() == TypeKind.ARRAY) {
return describeMatch(tree);
}
return Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.errorprone.bugpatterns.ArrayEquals;
import com.google.errorprone.bugpatterns.ArrayFillIncompatibleType;
import com.google.errorprone.bugpatterns.ArrayHashCode;
import com.google.errorprone.bugpatterns.ArrayRecordComponent;
import com.google.errorprone.bugpatterns.ArrayToString;
import com.google.errorprone.bugpatterns.ArraysAsListPrimitiveArray;
import com.google.errorprone.bugpatterns.AssertFalse;
Expand Down Expand Up @@ -667,6 +668,7 @@ public static ScannerSupplier warningChecks() {
ArrayEquals.class,
ArrayFillIncompatibleType.class,
ArrayHashCode.class,
ArrayRecordComponent.class,
ArrayToString.class,
ArraysAsListPrimitiveArray.class,
AssistedInjectScoping.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ArrayRecordComponentTest {
private final CompilationTestHelper testHelper =
CompilationTestHelper.newInstance(ArrayRecordComponent.class, getClass());

@Test
public void positive_recordWithArrayComponents() {
testHelper
.addSourceLines(
"Test.java",
"record Foo(",
" String string,",
" // BUG: Diagnostic contains: Record components should not be arrays",
" int[] array,",
" String otherString,",
" // BUG: Diagnostic contains: Record components should not be arrays",
" String[] otherArray) {}")
.doTest();
}

@Test
public void negative_recordWithNonArrayComponents() {
testHelper
.addSourceLines(
"Test.java",
"record Foo(",
" String string,",
" java.util.List<String> list,",
" int integer) {}")
.doTest();
}

@Test
public void negative_recordWithStaticArrayField() {
testHelper
.addSourceLines(
"Test.java",
"record Foo(String string) {",
" private static final int[] ints = {1, 2, 3};",
"}")
.doTest();
}
}

0 comments on commit de6deb3

Please sign in to comment.