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

Invalid CheckReturnValue suggested fix when a variable must be final or effectively final #2624

Open
hisener opened this issue Oct 14, 2021 · 0 comments

Comments

@hisener
Copy link
Contributor

hisener commented Oct 14, 2021

CheckReturnValue suggests a fix that causes a non-compilable state for lambda expressions since the variables defined in lambda expressions must be final or effectively final.

You can find a failing test here: PicnicSupermarket@769049b. The test failure:

import com.google.errorprone.annotations.CheckReturnValue;
import java.util.List;

public class TestClass {
  public void test() {
    var variable = new CustomType();
    // BUG: Diagnostic contains: Ignored return value
    List.of(1).forEach(unused -> variable.get());
  }
  static final class CustomType {
    @CheckReturnValue
    public CustomType get() {
      return null;
    }
  }
}


Actual Source:
=================

import com.google.errorprone.annotations.CheckReturnValue;
import java.util.List;

public class TestClass {
  public void test() {
    var variable = new CustomType();
    // BUG: Diagnostic contains: Ignored return value
    List.of(1).forEach(unused -> variable = variable.get());
  }
  static final class CustomType {
    @CheckReturnValue
    public CustomType get() {
      return null;
    }
  }
}

Diffs:
======

Found 1 nodes that differed in expected and actual trees. 

> Difference in expected tree and actual tree.
  Expected node: Line 8 COMPILATION_UNIT->CLASS(TestClass)->METHOD(test)->BLOCK(non-static)->EXPRESSION_STATEMENT->METHOD_INVOCATION->LAMBDA_EXPRESSION->METHOD_INVOCATION->METHOD_INVOCATION
  Actual node: Line 8 COMPILATION_UNIT->CLASS(TestClass)->METHOD(test)->BLOCK(non-static)->EXPRESSION_STATEMENT->METHOD_INVOCATION->LAMBDA_EXPRESSION->ASSIGNMENT->ASSIGNMENT
  Expected node kind to be <METHOD_INVOCATION> but was <ASSIGNMENT>.

I have a naive fix but this would prevent potential fixes for variables defined in lambda expressions.

diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractReturnValueIgnored.java b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractReturnValueIgnored.java
index d5236902b..0739f866a 100644
--- a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractReturnValueIgnored.java
+++ b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractReturnValueIgnored.java
@@ -223,12 +223,12 @@ public abstract class AbstractReturnValueIgnored extends BugChecker
 
     Fix fix = SuggestedFix.emptyFix();
     Symbol symbol = getSymbol(identifierExpr);
-    if (identifierExpr != null
-        && symbol != null
+    if (symbol != null
         && !symbol.name.contentEquals("this")
+        && state.getPath().getParentPath().getLeaf().getKind() != Kind.LAMBDA_EXPRESSION
         && returnType != null
         && state.getTypes().isAssignable(returnType, identifierType)) {
-      // Fix by assigning the assigning the result of the call to the root receiver reference.
+      // Fix by assigning the result of the call to the root receiver reference.
       fix =
           SuggestedFix.prefixWith(
               methodInvocationTree, state.getSourceForNode(identifierExpr) + " = ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant