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

Fix #938: ExecutorSubmitRunnableFutureIgnored as error prone ERROR #943

Merged
merged 2 commits into from
Oct 7, 2019
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
@@ -0,0 +1,66 @@
/*
* (c) Copyright 2018 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.AbstractReturnValueIgnored;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.fixes.SuggestedFixes;
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.concurrent.ExecutorService;

@AutoService(BugChecker.class)
@BugPattern(
name = "ExecutorSubmitRunnableFutureIgnored",
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks",
linkType = BugPattern.LinkType.CUSTOM,
providesFix = BugPattern.ProvidesFix.REQUIRES_HUMAN_ATTENTION,
severity = BugPattern.SeverityLevel.ERROR,
summary = "Uncaught exceptions from ExecutorService.submit are not logged by the uncaught exception handler "
+ "because it is assumed that the returned future is used to watch for failures.\n"
+ "When the returned future is ignored, using ExecutorService.execute is preferred because "
+ "failures are recorded.")
public final class ExecutorSubmitRunnableFutureIgnored extends AbstractReturnValueIgnored {

private static final Matcher<ExpressionTree> MATCHER = MethodMatchers.instanceMethod()
.onDescendantOf(ExecutorService.class.getName())
.named("submit")
.withParameters(Runnable.class.getName());

@Override
public Matcher<? super ExpressionTree> specializedMatcher() {
return MATCHER;
}

// Override matchMethodInvocation from AbstractReturnValueIgnored to apply our suggested fix.
@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
Description description = super.matchMethodInvocation(methodInvocationTree, state);
if (Description.NO_MATCH.equals(description)) {
return description;
}
return buildDescription(methodInvocationTree)
.addFix(SuggestedFixes.renameMethodInvocation(methodInvocationTree, "execute", state))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* (c) Copyright 2018 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.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import org.junit.jupiter.api.Test;

class ExecutorSubmitRunnableFutureIgnoredTest {

@Test
void testFix() {
BugCheckerRefactoringTestHelper.newInstance(new ExecutorSubmitRunnableFutureIgnored(), getClass())
.addInputLines(
"Test.java",
"import " + ExecutorService.class.getName() + ';',
"class Test {",
" void f(ExecutorService exec) {",
" exec.submit(() -> System.out.println(\"Hello\"));",
" }",
"}")
.addOutputLines(
"Test.java",
"import " + ExecutorService.class.getName() + ';',
"class Test {",
" void f(ExecutorService exec) {",
" exec.execute(() -> System.out.println(\"Hello\"));",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
void testNegative() {
CompilationTestHelper.newInstance(ExecutorSubmitRunnableFutureIgnored.class, getClass()).addSourceLines(
"Test.java",
"import " + ExecutorService.class.getName() + ';',
"import " + Future.class.getName() + ';',
"class Test {",
" void f(ExecutorService exec) {",
" Future<?> future = exec.submit(() -> System.out.println(\"Hello\"));",
" }",
"}").doTest();
}

@Test
void testMessage() {
CompilationTestHelper.newInstance(ExecutorSubmitRunnableFutureIgnored.class, getClass()).addSourceLines(
"Test.java",
"import " + ExecutorService.class.getName() + ';',
"class Test {",
" void f(ExecutorService exec) {",
" // BUG: Diagnostic contains: not logged by the uncaught exception handler",
" exec.submit(() -> System.out.println(\"Hello\"));",
" }",
"}").doTest();
}
}

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-943.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: ExecutorSubmitRunnableFutureIgnored as error prone ERROR
links:
- https://github.com/palantir/gradle-baseline/pull/943
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class BaselineErrorProneExtension {

private static final ImmutableList<String> DEFAULT_PATCH_CHECKS = ImmutableList.of(
// Baseline checks
"ExecutorSubmitRunnableFutureIgnored",
"LambdaMethodReference",
"OptionalOrElseMethodInvocation",
"PreferBuiltInConcurrentKeySet",
Expand Down