diff --git a/README.md b/README.md index d92189532..0b89d5dc1 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,6 @@ Safe Logging can be found at [github.com/palantir/safe-logging](https://github.c +com.palantir.logsafe.Preconditions.checkNotNull(variable, "message"); // equivalent functionality is available in the safe-logging variant ``` - `ShutdownHook`: Applications should not use `Runtime#addShutdownHook`. -- `SwitchStatementDefaultCase`: Switch statements should avoid using default cases. Default cases prevent the [MissingCasesInEnumSwitch](http://errorprone.info/bugpattern/MissingCasesInEnumSwitch.html) check from detecting when an enum value is not explicitly handled. This check is important to help avoid incorrect behavior when new enum values are introduced. - `GradleCacheableTaskAction`: Gradle plugins should not call `Task.doFirst` or `Task.doLast` with a lambda, as that is not cacheable. See [gradle/gradle#5510](https://github.com/gradle/gradle/issues/5510) for more details. - `PreferBuiltInConcurrentKeySet`: Discourage relying on Guava's `com.google.common.collect.Sets.newConcurrentHashSet()`, when Java's `java.util.concurrent.ConcurrentHashMap.newKeySet()` serves the same purpose. - `JUnit5RuleUsage`: Prevent accidental usage of `org.junit.Rule`/`org.junit.ClassRule` within Junit5 tests diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java index d44b8b801..b09be6bc6 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java @@ -330,7 +330,6 @@ private static SuggestedFix makeAssignmentDeclaration( state.getSourceForNode(removedVariableTree.get().getType()) + " "); } - @SuppressWarnings("SwitchStatementDefaultCase") private static String describeVariable(Symbol.VarSymbol symbol) { switch (symbol.getKind()) { case FIELD: diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/SwitchStatementDefaultCase.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/SwitchStatementDefaultCase.java deleted file mode 100644 index 4452dfc27..000000000 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/SwitchStatementDefaultCase.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. - * - * 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.palantir.baseline.errorprone; - -import com.google.auto.service.AutoService; -import com.google.errorprone.BugPattern; -import com.google.errorprone.BugPattern.LinkType; -import com.google.errorprone.BugPattern.SeverityLevel; -import com.google.errorprone.VisitorState; -import com.google.errorprone.bugpatterns.BugChecker; -import com.google.errorprone.matchers.Description; -import com.sun.source.tree.CaseTree; -import com.sun.source.tree.SwitchTree; -import java.util.Objects; - -@AutoService(BugChecker.class) -@BugPattern( - name = "SwitchStatementDefaultCase", - link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", - linkType = LinkType.CUSTOM, - severity = SeverityLevel.SUGGESTION, - summary = "Avoid default cases in switch statements to correctly handle new enum values") -public final class SwitchStatementDefaultCase extends BugChecker implements BugChecker.SwitchTreeMatcher { - - @Override - public Description matchSwitch(SwitchTree tree, VisitorState state) { - if (hasDefaultCase(tree)) { - return buildDescription(tree) - .setMessage("Avoid using default case in switch statement.") - .build(); - } - - return Description.NO_MATCH; - } - - private boolean hasDefaultCase(SwitchTree tree) { - return tree.getCases().stream().map(CaseTree::getExpression).anyMatch(Objects::isNull); - } -} diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/SwitchStatementDefaultCaseTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/SwitchStatementDefaultCaseTest.java deleted file mode 100644 index 668a3fe88..000000000 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/SwitchStatementDefaultCaseTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. - * - * 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.palantir.baseline.errorprone; - -import com.google.errorprone.CompilationTestHelper; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class SwitchStatementDefaultCaseTest { - - private CompilationTestHelper compilationHelper; - - @BeforeEach - public void before() { - compilationHelper = CompilationTestHelper.newInstance(SwitchStatementDefaultCase.class, getClass()); - } - - @Test - public void testDefaultCase() { - compilationHelper - .addSourceLines( - "Test.java", - "class Test {", - " enum Case { ONE, TWO, THREE }", - " void m(Case c) {", - " // BUG: Diagnostic contains: Avoid using default case in switch statement.", - " switch (c) {", - " case ONE:", - " case TWO:", - " case THREE:", - " default:", - " break;", - " }", - " }", - "}") - .doTest(); - } - - @Test - public void testNoDefaultCase() { - compilationHelper - .addSourceLines( - "Test.java", - "class Test {", - " enum Case { ONE, TWO, THREE }", - " void m(Case c) {", - " switch (c) {", - " case ONE:", - " case TWO:", - " case THREE:", - " break;", - " }", - " }", - "}") - .doTest(); - } -} diff --git a/changelog/@unreleased/pr-1309.v2.yml b/changelog/@unreleased/pr-1309.v2.yml new file mode 100644 index 000000000..0eb78d41b --- /dev/null +++ b/changelog/@unreleased/pr-1309.v2.yml @@ -0,0 +1,6 @@ +type: improvement +improvement: + description: Baseline no longer provides a `SwitchStatementDefaultCase` error-prone + check. + links: + - https://github.com/palantir/gradle-baseline/pull/1309