When writing String.equals(Integer), FindBugs will report "EC_UNRELATED_TYPES: This method calls equals(Object) on two references of different class types with no common subclasses.".
However, doing the same thing in a Lambda goes unnoticed. This is quite common when using something like the stream filter API.
I was able to reproduce this (JDK8 u51, FindBugs 3.0.1) with the following test:
package regressions;
import edu.umd.cs.findbugs.annotations.ExpectWarning;
public class UnrelatedTypesInLambda {
@ExpectWarning("EC_UNRELATED_TYPES")
public void unrelatedPlain() {
String lhs = "";
Integer rhs = 1;
lhs.equals(rhs);
}
// This has the same bug as the above example but it isn't detected by FindBugs.
// @ExpectWarning("EC_UNRELATED_TYPES")
public void unrelatedLambda() {
String lhs = "";
Integer rhs = 1;
final Runnable lambda = () -> {
lhs.equals(rhs);
};
lambda.run();
}
}
The bug is reported in this case. The difference is that it's reported not for
unrelatedLambdamethod, but for compiled lambda body, so you cannot easily annotate it with @ExpectWarning. Here's the full XML report:As you can see, it's reported in autogenerated method
regressions.UnrelatedTypesInLambda.lambda$0(String, Integer)and has no information that this lambda is called inunrelatedLambdamethod, thus @ExpectWarning annotation does not work here. We may improve support of lambdas (currently it's very poor). See also GitHub issue#51.Last edit: Tagir Valeev 2015-10-08
The reason I wrote the test was because a bug made it's way past build which is set to fail if any bug is reported. Clearly my test is bad, but I still believe there is a bug here.
I'll try write a better test. Thanks for the pointer about the lambdas; I'll be sure to check the full report.
I have a similar issue.
Problems are not detected inside lambda body, in Eclipse: