Skip to content
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

Stream.collect safety no longer incorrectly includes map collectors #2264

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -16,6 +16,7 @@

package com.palantir.baseline.errorprone.safety;

import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
Expand All @@ -27,6 +28,7 @@
import com.google.errorprone.dataflow.AccessPath;
import com.google.errorprone.dataflow.AccessPathStore;
import com.google.errorprone.dataflow.AccessPathValues;
import com.google.errorprone.matchers.ChildMultiMatcher;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.method.MethodMatchers;
Expand Down Expand Up @@ -62,6 +64,7 @@
import java.util.OptionalLong;
import java.util.Set;
import java.util.stream.BaseStream;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.lang.model.element.VariableElement;
Expand Down Expand Up @@ -378,10 +381,29 @@ public final class SafetyPropagationTransfer implements ForwardTransferFunction<
Matchers.anyOf(MethodMatchers.instanceMethod()
.onExactClassAny(StringBuilder.class.getName(), StringBuffer.class.getName())
.namedAnyOf("append", "insert", "replace"));
private static final Matcher<ExpressionTree> RETURNS_SAFETY_OF_ARGS_AND_RECEIVER = Matchers.anyOf(

private static final Matcher<ExpressionTree> UNKNOWN_COLLECTORS = Matchers.anyOf(
MethodMatchers.staticMethod()
.onClass(Collectors.class.getName())
.named("counting")
.withNoParameters(),
MethodMatchers.staticMethod()
.onClass(Collectors.class.getName())
.namedAnyOf("toMap", "toUnmodifiableMap", "toConcurrentMap"),
MethodMatchers.staticMethod().onClass(ImmutableMap.class.getName()).named("toImmutableMap"),
MethodMatchers.staticMethod()
.onClass(ImmutableBiMap.class.getName())
.named("toImmutableBiMap"));

private static final Matcher<ExpressionTree> COLLECT_INCLUDES_STREAM_SAFETY = Matchers.methodInvocation(
MethodMatchers.instanceMethod()
.onDescendantOf(Stream.class.getName())
.namedAnyOf("collect"),
ChildMultiMatcher.MatchType.LAST,
Matchers.not(UNKNOWN_COLLECTORS));

private static final Matcher<ExpressionTree> RETURNS_SAFETY_OF_ARGS_AND_RECEIVER = Matchers.anyOf(
COLLECT_INCLUDES_STREAM_SAFETY,
MethodMatchers.instanceMethod()
.onDescendantOf(Optional.class.getName())
// TODO(ckozak): support 'or' and 'orElseGet' which require lambda support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,37 @@ public void testRecordComponentUsage() {
.doTest();
}

@Test
public void testTransformingStreamCollectors() {
helper().addSourceLines(
"Test.java",
"import com.palantir.logsafe.*;",
"import java.util.*;",
"import java.util.function.*;",
"import java.util.stream.*;",
"import com.google.common.collect.*;",
"class Test {",
" @DoNotLog",
" interface TopLevel {",
" @Safe String name();",
" @Safe String value();",
" @DoNotLog String token();",
" }",
" void f(@Unsafe Stream<TopLevel> stream) {",
" // BUG: Diagnostic contains: Dangerous argument value: arg is 'DO_NOT_LOG'",
" fun(stream.collect(Collectors.toList()));",
" fun(stream.collect(Collectors.counting()));",
" fun(stream.collect(Collectors.toMap(TopLevel::name, TopLevel::value)));",
" fun(stream.collect(Collectors.toConcurrentMap(TopLevel::name, TopLevel::value)));",
" fun(stream.collect(Collectors.toUnmodifiableMap(TopLevel::name, TopLevel::value)));",
" fun(stream.collect(ImmutableMap.toImmutableMap(TopLevel::name, TopLevel::value)));",
" fun(stream.collect(ImmutableBiMap.toImmutableBiMap(TopLevel::name, TopLevel::value)));",
" }",
" private static void fun(@Safe Object value) {}",
"}")
.doTest();
}

private CompilationTestHelper helper() {
return CompilationTestHelper.newInstance(IllegalSafeLoggingArgument.class, getClass());
}
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2264.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Stream.collect safety no longer incorrectly includes map collectors
links:
- https://github.com/palantir/gradle-baseline/pull/2264