-
Notifications
You must be signed in to change notification settings - Fork 134
Provider implicit toString #1322
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
Changes from 2 commits
d450981
05294b0
31f28b5
fcd32f9
5d39aa5
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,55 @@ | ||
| /* | ||
| * (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 com.google.auto.service.AutoService; | ||
| import com.google.errorprone.BugPattern; | ||
| import com.google.errorprone.VisitorState; | ||
| import com.google.errorprone.bugpatterns.AbstractToString; | ||
| import com.google.errorprone.bugpatterns.BugChecker; | ||
| import com.google.errorprone.fixes.Fix; | ||
| import com.google.errorprone.predicates.TypePredicate; | ||
| import com.google.errorprone.predicates.TypePredicates; | ||
| import com.sun.source.tree.ExpressionTree; | ||
| import com.sun.source.tree.Tree; | ||
| import java.util.Optional; | ||
|
|
||
| @AutoService(BugChecker.class) | ||
| @BugPattern( | ||
| name = "GradleProviderToString", | ||
| summary = "Calling toString on a Provider does not render the contained value", | ||
| severity = BugPattern.SeverityLevel.ERROR) | ||
| public final class GradleProviderToString extends AbstractToString { | ||
|
|
||
| private static final TypePredicate IS_PROVIDER = TypePredicates.isDescendantOf("org.gradle.api.provider.Provider"); | ||
|
|
||
| @Override | ||
| protected TypePredicate typePredicate() { | ||
| return IS_PROVIDER; | ||
| } | ||
|
|
||
| @Override | ||
| protected Optional<Fix> implicitToStringFix(ExpressionTree tree, VisitorState state) { | ||
| // We could automatically call Provider#get, but is that always right? | ||
| return Optional.empty(); | ||
|
||
| } | ||
|
|
||
| @Override | ||
| protected Optional<Fix> toStringFix(Tree parent, ExpressionTree expression, VisitorState state) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * (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 com.google.errorprone.CompilationTestHelper; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class GradleProviderToStringTest { | ||
|
|
||
| private CompilationTestHelper compilationHelper; | ||
|
|
||
| @BeforeEach | ||
| public void before() { | ||
| compilationHelper = CompilationTestHelper.newInstance(GradleProviderToString.class, getClass()); | ||
| } | ||
|
|
||
| @Test | ||
| public void failsUsedInStringConcatenation() { | ||
| compilationHelper | ||
| .addSourceLines( | ||
| "Foo.java", | ||
| "import org.gradle.api.Project;", | ||
| "import org.gradle.api.Plugin;", | ||
| "import org.gradle.api.provider.Provider;", | ||
| "class Foo implements Plugin<Project> {", | ||
| " public final void apply(Project project) {", | ||
| " Provider<String> provider = project.provider(() -> \"hello\");", | ||
| " // BUG: Diagnostic contains: Calling toString on a Provider", | ||
| " String value = \"My bad provider value: \" + provider;", | ||
| " }", | ||
| "}") | ||
| .doTest(); | ||
| } | ||
|
Contributor
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. I would just add a second test to check that toString on non-providers is not broken. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| type: feature | ||
| feature: | ||
| description: Forbid implicit toString of Gradle Providers. | ||
| links: | ||
| - https://github.com/palantir/gradle-baseline/pull/1322 |
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.
oh this is just too easy :D