Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.fixes.SuggestedFixes.removeModifiers;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.util.ASTHelpers.canonicalConstructor;
import static com.google.errorprone.util.ASTHelpers.enclosingClass;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
import static com.google.errorprone.util.ASTHelpers.isEffectivelyPrivate;
import static com.google.errorprone.util.ASTHelpers.isRecord;
import static com.google.errorprone.util.ASTHelpers.streamSuperMethods;
import static javax.lang.model.element.Modifier.PROTECTED;
import static javax.lang.model.element.Modifier.PUBLIC;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
Expand Down Expand Up @@ -102,6 +106,21 @@ private void match(
if (hasAnnotation(methodSymbol, "java.lang.Override", state)) {
return;
}
if (methodSymbol.isConstructor()) {
ClassSymbol enclosingClass = enclosingClass(methodSymbol);
/*
* TODO(cpovirk): Introduce an ASTHelpers.isCanonicalConstructor to avoid scanning all
* members again in canonicalConstructor?
*/
if (isRecord(enclosingClass)
&& methodSymbol.equals(canonicalConstructor(enclosingClass, state))) {
if (enclosingClass.getModifiers().contains(PUBLIC)
|| enclosingClass.getModifiers().contains(PROTECTED)) {
// Canonical constructors are required to be at least as visible as the record itself.
return;
}
}
}
// TODO: cushon - technically this should only match final classes, otherwise it could break
// a subclass that relies on inheriting a method of a particular visibility to fulfil and
// interface contract. Skip that for now, since many classes don't rely on that and also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,21 @@ public class C extends B {}
""")
.doTest();
}

@Test
public void negative_recordInPrivateInterface() {
testHelper
.addSourceLines(
"MyClass.java",
"""
public class MyClass {
private interface MyInterface {
record MyRecord() {
public MyRecord {}
}
}
}
""")
.doTest();
}
}
Loading