-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DefaultLocale check #2343
Add DefaultLocale check #2343
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* (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.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.WARNING) | ||
public final class DefaultLocale extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static final Matcher<ExpressionTree> MATCHER = MethodMatchers.instanceMethod() | ||
.onExactClass("java.lang.String") | ||
.namedAnyOf("toLowerCase", "toUpperCase") | ||
.withNoParameters(); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (MATCHER.matches(tree, state)) { | ||
return buildDescription(tree) | ||
.addFix(SuggestedFix.builder() | ||
.addImport("java.util.Locale") | ||
pkoenig10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.replace( | ||
state.getEndPosition(tree.getMethodSelect()), | ||
state.getEndPosition(tree), | ||
"(Locale.ROOT)") | ||
.build()) | ||
.build(); | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* (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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type: feature | ||
feature: | ||
description: |- | ||
Add DefaultLocale check | ||
|
||
Related to https://github.com/google/error-prone/issues/632 | ||
|
||
Adds a `DefaultLocale` check that replaces uses of `String.toLowerCase()` and `String.toUpperCase()` with the overloads that take a `Locale`, using `Locale.ROOT`. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/2343 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ public class BaselineErrorProneExtension { | |
"CompileTimeConstantViolatesLiskovSubstitution", | ||
"ConsistentLoggerName", | ||
"ConsistentOverrides", | ||
"DefaultLocale", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially, let’s make this check a suggestion, and not add it to this list. We can spin up an excavator to roll out the suggested fix asynchronously, otherwise some baseline upgrades will be blocked on a way that we aren’t currently prepared to handle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On us to build a better ratcheting rollout mechanism in the medium term There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it feels a bit pointless to write these error-prone checks if we're too worried about blocking upgrades to actually enforce them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It depends on the criticality of the finding. I suspect this will find a lot of things that aren’t going to cause issues in practice. I agree we should fix them, but we have other work in flight that isn’t worth risking. |
||
"DeprecatedGuavaObjects", | ||
"ExecutorSubmitRunnableFutureIgnored", | ||
"ExtendsErrorOrThrowable", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this add a check withi same name as, similar spirit to, but different implementation from https://errorprone.info/bugpattern/DefaultLocale ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see it's removed -- #2839