From ef56f3dde6849c44dec1d31f9cf02ab2640f46c5 Mon Sep 17 00:00:00 2001 From: ghm Date: Wed, 13 Apr 2022 09:23:03 -0700 Subject: [PATCH] Turn down DivZero. It's aiming to replicate a javac warning, which I think we enable as an error anyway? PiperOrigin-RevId: 441500720 --- .../errorprone/bugpatterns/DivZero.java | 80 ----------------- .../scanner/BuiltInCheckerSuppliers.java | 2 - .../errorprone/bugpatterns/DivZeroTest.java | 42 --------- .../testdata/DivZeroNegativeCases.java | 25 ------ .../testdata/DivZeroPositiveCases.java | 87 ------------------- .../scanner/ScannerSupplierTest.java | 6 +- docs/bugpattern/DivZero.md | 1 - 7 files changed, 4 insertions(+), 239 deletions(-) delete mode 100644 core/src/main/java/com/google/errorprone/bugpatterns/DivZero.java delete mode 100644 core/src/test/java/com/google/errorprone/bugpatterns/DivZeroTest.java delete mode 100644 core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroNegativeCases.java delete mode 100644 core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroPositiveCases.java delete mode 100644 docs/bugpattern/DivZero.md diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/DivZero.java b/core/src/main/java/com/google/errorprone/bugpatterns/DivZero.java deleted file mode 100644 index cac8cccb628..00000000000 --- a/core/src/main/java/com/google/errorprone/bugpatterns/DivZero.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2013 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 static com.google.errorprone.matchers.Matchers.anyOf; -import static com.google.errorprone.matchers.Matchers.kindIs; - -import com.google.errorprone.BugPattern; -import com.google.errorprone.VisitorState; -import com.google.errorprone.bugpatterns.BugChecker.BinaryTreeMatcher; -import com.google.errorprone.bugpatterns.BugChecker.CompoundAssignmentTreeMatcher; -import com.google.errorprone.fixes.SuggestedFix; -import com.google.errorprone.matchers.Description; -import com.google.errorprone.util.ASTHelpers; -import com.sun.source.tree.BinaryTree; -import com.sun.source.tree.CompoundAssignmentTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.LiteralTree; -import com.sun.source.tree.StatementTree; -import com.sun.source.tree.Tree; -import com.sun.source.tree.Tree.Kind; - -/** - * Matches the behaviour of javac's divzero xlint warning. - * - * @author cushon@google.com (Liam Miller-Cushon) - */ -@BugPattern(altNames = "divzero", summary = "Division by integer literal zero", severity = ERROR) -public class DivZero extends BugChecker - implements BinaryTreeMatcher, CompoundAssignmentTreeMatcher { - - @Override - public Description matchBinary(BinaryTree tree, VisitorState state) { - return matchDivZero(tree, tree.getRightOperand(), state); - } - - @Override - public Description matchCompoundAssignment(CompoundAssignmentTree tree, VisitorState state) { - return matchDivZero(tree, tree.getExpression(), state); - } - - private Description matchDivZero(Tree tree, ExpressionTree operand, VisitorState state) { - if (!anyOf(kindIs(Kind.DIVIDE), kindIs(Kind.DIVIDE_ASSIGNMENT)).matches(tree, state)) { - return Description.NO_MATCH; - } - - if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) { - return Description.NO_MATCH; - } - - LiteralTree rightOperand = (LiteralTree) operand; - if (((Integer) rightOperand.getValue()) != 0) { - return Description.NO_MATCH; - } - - // Find and replace enclosing Statement. - StatementTree enclosingStmt = - ASTHelpers.findEnclosingNode(state.getPath(), StatementTree.class); - return (enclosingStmt != null) - ? describeMatch( - tree, - SuggestedFix.replace(enclosingStmt, "throw new ArithmeticException(\"/ by zero\");")) - : describeMatch(tree); - } -} diff --git a/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java b/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java index 6b7c9bebd75..780593ddd83 100644 --- a/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java +++ b/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java @@ -101,7 +101,6 @@ import com.google.errorprone.bugpatterns.DifferentNameButSame; import com.google.errorprone.bugpatterns.DiscardedPostfixExpression; import com.google.errorprone.bugpatterns.DistinctVarargsChecker; -import com.google.errorprone.bugpatterns.DivZero; import com.google.errorprone.bugpatterns.DoNotCallChecker; import com.google.errorprone.bugpatterns.DoNotCallSuggester; import com.google.errorprone.bugpatterns.DoNotClaimAnnotations; @@ -1016,7 +1015,6 @@ public static ScannerSupplier errorChecks() { DeduplicateConstants.class, DepAnn.class, DifferentNameButSame.class, - DivZero.class, EmptyIfStatement.class, EmptyTopLevelDeclaration.class, EqualsBrokenForNull.class, diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/DivZeroTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/DivZeroTest.java deleted file mode 100644 index a6482e5f232..00000000000 --- a/core/src/test/java/com/google/errorprone/bugpatterns/DivZeroTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2013 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; - -/** - * @author cushon@google.com (Liam Miller-Cushon) - */ -@RunWith(JUnit4.class) -public class DivZeroTest { - - private final CompilationTestHelper compilationHelper = - CompilationTestHelper.newInstance(DivZero.class, getClass()); - - @Test - public void testPositiveCase() { - compilationHelper.addSourceFile("DivZeroPositiveCases.java").doTest(); - } - - @Test - public void testNegativeCase() { - compilationHelper.addSourceFile("DivZeroNegativeCases.java").doTest(); - } -} diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroNegativeCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroNegativeCases.java deleted file mode 100644 index 6ae7a8b9de3..00000000000 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroNegativeCases.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2013 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.testdata; - -/** @author cushon@google.com (Liam Miller-Cushon) */ -public class DivZeroNegativeCases { - - void method(int a) { - double y = (double) a / 0.0; - } -} diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroPositiveCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroPositiveCases.java deleted file mode 100644 index 7f8ed5c5b71..00000000000 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/DivZeroPositiveCases.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2013 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.testdata; - -/** @author cushon@google.com (Liam Miller-Cushon) */ -public class DivZeroPositiveCases { - - public static void main(String[] args) { - new DivZeroPositiveCases(); - } - - static int staticOne = 0; - - // BUG: Diagnostic contains: DivZero - static int staticTwo = staticOne / 0; - - // BUG: Diagnostic contains: DivZero - static int staticThree = (staticTwo / 0); - - int fieldOne; - - // BUG: Diagnostic contains: DivZero - int fieldTwo = fieldOne / 0; - - // BUG: Diagnostic contains: DivZero - int fieldThree = (fieldTwo /= 0); - - int f() { - return 42; - } - - void method(final int a, double b, boolean flag) { - int x; - double y; - - // BUG: Diagnostic contains: throw new ArithmeticException - x = a / 0; - - // BUG: Diagnostic contains: throw new ArithmeticException - x /= 0; - - x = - ((((a / a) / (a / a)) / ((a / a) / (a / a))) / (((a / a) / (a / a)) / ((a / a) / (a / a)))) - / - // BUG: Diagnostic contains: zero - ((((a / a) / (a / a)) / ((a / 0) / (a / a))) - / (((a / a) / (a / a)) / ((a / a) / (a / a)))); - - // BUG: Diagnostic contains: throw new ArithmeticException - x = flag ? a / 0 : 42; - - // BUG: Diagnostic contains: throw new ArithmeticException - for (int i = 0; i < 10; i /= 0) {} - - Object o = - new Object() { - // BUG: Diagnostic contains: throw new ArithmeticException - int x = a / 0; - }; - - // BUG: Diagnostic contains: throw new ArithmeticException - x = f() / 0; - } - - // TODO(cushon): write a check for self-references via qualified names in field initializers, - // even if JLS 8.3.2.3 permits it. - - // BUG: Diagnostic contains: DivZero - int selfRefField = this.selfRefField / 0; - - // BUG: Diagnostic contains: DivZero - static int staticSelfRefField = DivZeroPositiveCases.staticSelfRefField / 0; -} diff --git a/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java b/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java index 0fa66b2a940..38c4c176a6f 100644 --- a/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java +++ b/core/src/test/java/com/google/errorprone/scanner/ScannerSupplierTest.java @@ -44,9 +44,9 @@ import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.ChainingConstructorIgnoresParameter; import com.google.errorprone.bugpatterns.DepAnn; -import com.google.errorprone.bugpatterns.DivZero; import com.google.errorprone.bugpatterns.EqualsIncompatibleType; import com.google.errorprone.bugpatterns.LongLiteralLowerCaseSuffix; +import com.google.errorprone.bugpatterns.MethodCanBeStatic; import com.google.errorprone.bugpatterns.PackageLocation; import com.google.errorprone.bugpatterns.ReferenceEquality; import com.google.errorprone.bugpatterns.StaticQualifiedUsingExpression; @@ -325,7 +325,9 @@ public void applyOverridesEnableAllChecks() { // The 'AllDisabledChecksAsWarnings' flag doesn't populate through to additional plugins assertScanner( ss.applyOverrides(epOptions) - .plus(ScannerSupplier.fromBugCheckerClasses(DivZero.class).filter(t -> false))) + .plus( + ScannerSupplier.fromBugCheckerClasses(MethodCanBeStatic.class) + .filter(t -> false))) .hasEnabledChecks(BadShiftAmount.class, StaticQualifiedUsingExpression.class); } diff --git a/docs/bugpattern/DivZero.md b/docs/bugpattern/DivZero.md deleted file mode 100644 index 6e1cd396afc..00000000000 --- a/docs/bugpattern/DivZero.md +++ /dev/null @@ -1 +0,0 @@ -This code will cause a runtime arithmetic exception if it is executed.