From c24c5d1e205cb67c03d2e4f38e0b3357e70dd84a Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Tue, 6 Jun 2023 07:42:26 -0700 Subject: [PATCH] Remove DefaultLocale in favor of StringCaseLocaleUsage --- .../baseline/errorprone/DefaultLocale.java | 64 ------------------ .../errorprone/DefaultLocaleTest.java | 66 ------------------- .../baseline/plugins/BaselineErrorProne.java | 3 - 3 files changed, 133 deletions(-) delete mode 100644 baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/DefaultLocale.java delete mode 100644 baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/DefaultLocaleTest.java diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/DefaultLocale.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/DefaultLocale.java deleted file mode 100644 index 966818425..000000000 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/DefaultLocale.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * (c) Copyright 2019 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.SeverityLevel; -import com.google.errorprone.VisitorState; -import com.google.errorprone.bugpatterns.BugChecker; -import com.google.errorprone.fixes.SuggestedFix; -import com.google.errorprone.fixes.SuggestedFixes; -import com.google.errorprone.matchers.Description; -import com.google.errorprone.matchers.Matcher; -import com.google.errorprone.matchers.method.MethodMatchers; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.MethodInvocationTree; - -@AutoService(BugChecker.class) -@BugPattern( - link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", - linkType = BugPattern.LinkType.CUSTOM, - summary = "Implicit use of the platform default locale, which can result in differing behaviour between JVM" - + " executions.", - severity = SeverityLevel.SUGGESTION) -public final class DefaultLocale extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { - - private static final long serialVersionUID = 1L; - - private static final Matcher MATCHER = MethodMatchers.instanceMethod() - .onExactClass("java.lang.String") - .namedAnyOf("toLowerCase", "toUpperCase") - .withNoParameters(); - - @Override - public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { - if (MATCHER.matches(tree, state)) { - SuggestedFix.Builder fix = SuggestedFix.builder(); - return buildDescription(tree) - .addFix(fix.replace( - state.getEndPosition(tree.getMethodSelect()), - state.getEndPosition(tree), - String.format( - "(%s.ROOT)", SuggestedFixes.qualifyType(state, fix, "java.util.Locale"))) - .build()) - .build(); - } - - return Description.NO_MATCH; - } -} diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/DefaultLocaleTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/DefaultLocaleTest.java deleted file mode 100644 index 9fc267211..000000000 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/DefaultLocaleTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * (c) Copyright 2020 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 org.junit.jupiter.api.Test; - -class DefaultLocaleTest { - - @Test - void testFixToLowerCase() { - fix().addInputLines( - "Test.java", - "class Test {", - " String f(String s) {", - " return s.toLowerCase();", - " }", - "}") - .addOutputLines( - "Test.java", - "import java.util.Locale;", - "class Test {", - " String f(String s) {", - " return s.toLowerCase(Locale.ROOT);", - " }", - "}") - .doTest(); - } - - @Test - void testFixToUpperCase() { - fix().addInputLines( - "Test.java", - "class Test {", - " String f(String s) {", - " return s.toUpperCase();", - " }", - "}") - .addOutputLines( - "Test.java", - "import java.util.Locale;", - "class Test {", - " String f(String s) {", - " return s.toUpperCase(Locale.ROOT);", - " }", - "}") - .doTest(); - } - - private RefactoringValidator fix() { - return RefactoringValidator.of(DefaultLocale.class, getClass()); - } -} diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineErrorProne.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineErrorProne.java index ec383590f..f97b42c9a 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineErrorProne.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineErrorProne.java @@ -133,9 +133,6 @@ private static void configureErrorProneOptions( "CanIgnoreReturnValueSuggester", "InlineMeSuggester", "PreferImmutableStreamExCollections", - // StringCaseLocaleUsage duplicates our existing DefaultLocale check which is already - // enforced in some places. - "StringCaseLocaleUsage", "UnnecessaryTestMethodPrefix", "UnusedVariable", // See VarUsage: The var keyword results in illegible code in most cases and should not be used.