-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a check to prevent record components of array type.
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
1 parent
fcd3939
commit de6deb3
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
core/src/main/java/com/google/errorprone/bugpatterns/ArrayRecordComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
core/src/test/java/com/google/errorprone/bugpatterns/ArrayRecordComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |