Skip to content

Commit

Permalink
Suppress MethodCanBeStatic with @keep (including as a meta-annotation)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 438543791
  • Loading branch information
graememorgan authored and Error Prone Team committed Mar 31, 2022
1 parent 62a2d27 commit d8961c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ private static boolean isExcluded(MethodTree tree, VisitorState state) {
if (sym.isConstructor() || !disjoint(EXCLUDED_MODIFIERS, sym.getModifiers())) {
return true;
}
if (!ASTHelpers.canBeRemoved(sym, state)) {
// Methods that override other methods, or that are overridden, can't be static.
// We conservatively warn only for private methods.
if (!ASTHelpers.canBeRemoved(sym, state) || ASTHelpers.shouldKeep(tree)) {
return true;
}
switch (sym.owner.enclClass().getNestingKind()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ public void positiveTypeParameter() {
.doTest();
}

@Test
public void negativeSuppressedByKeep() {
testHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.Keep;",
"class Test {",
" @Keep",
" private int add(int x, int y) {",
" return x + y;",
" }",
"}")
.doTest();
}

@Test
public void negativeTypeParameter() {
testHelper
Expand Down

0 comments on commit d8961c2

Please sign in to comment.