-
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
Optional.toString() should throw error #2833
base: develop
Are you sure you want to change the base?
Changes from all 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,52 @@ | ||
/* | ||
* (c) Copyright 2024 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.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; | ||
import java.util.Optional; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = BugPattern.LinkType.CUSTOM, | ||
severity = SeverityLevel.WARNING, | ||
summary = "Optional.toString() does not stringifies the value contained by the Optional object.") | ||
public final class OptionalToString extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { | ||
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. If we want this, we need to devise a roll out plan, since there may be a lot of existing code that does this and we don't want Baseline upgrades to be blocked. Typically we do this by automating the fix, but that seems more difficult here since the correct fix is context dependnent. 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. Would changing SeverityLevel to WARNING instead of error unblock the upgrades? Or should we suggest people to add |
||
|
||
private static final Matcher<ExpressionTree> OPTIONAL_TO_STRING_METHOD = MethodMatchers.instanceMethod() | ||
.onExactClassAny(Optional.class.getName(), com.google.common.base.Optional.class.getName()) | ||
.named("toString") | ||
.withNoParameters(); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState visitorState) { | ||
if (OPTIONAL_TO_STRING_METHOD.matches(methodInvocationTree, visitorState)) { | ||
return describeMatch(methodInvocationTree); | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* (c) Copyright 2024 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 OptionalToStringUsageTest { | ||
|
||
private CompilationTestHelper compilationHelper; | ||
|
||
@BeforeEach | ||
public void before() { | ||
compilationHelper = CompilationTestHelper.newInstance(OptionalToString.class, getClass()); | ||
} | ||
|
||
@Test | ||
public void should_throw_error_if_to_string_is_invoked_on_java_optional() { | ||
compilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import java.util.Optional;", | ||
"class Test {", | ||
" String f() {", | ||
" // BUG: Diagnostic contains: Optional.toString() does not stringifies the value", | ||
" return Optional.of(\"This is an optional value\").toString();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void should_throw_error_if_to_string_is_invoked_on_guava_optional() { | ||
compilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import com.google.common.base.Optional;", | ||
"class Test {", | ||
" String f() {", | ||
" // BUG: Diagnostic contains: Optional.toString() does not stringifies the value", | ||
" return Optional.of(\"This is an optional value\").toString();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void should_not_throw_error_if_to_string_is_invoked_on_optional_get() { | ||
compilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import com.google.common.base.Optional;", | ||
"class Test {", | ||
" String f() {", | ||
" return Optional.of(\"This is an optional value\").get().toString();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Optional.toString() should throw error | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/2833 |
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.
Optional#toString
does stringify the contained value, and decorates the result withOptional[<delegate string value>]
to make it clear that the value is an optional.