Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

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


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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's right enough of the time, we could do it for convenience. We don't need to automatically apply it.

}

@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();
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1322.v2.yml
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