This commit is contained in:
Clément Fournier
2024-05-16 15:29:17 +02:00
parent ed18c9d746
commit 798b2c31a1
3 changed files with 55 additions and 1 deletions

View File

@ -434,6 +434,11 @@ public final class PolyResolution {
return ExprContext.getMissingInstance();
}
if (!internalUse) {
// Only in type resolution do we need to fetch the outermost context
return newInvocContext(papi, node.getIndexInParent());
}
// Constructor or method call, maybe there's another context around
// We want to fetch the outermost invocation node, but not further
ExprContext outerCtx = contextOf(papi, /*onlyInvoc:*/true, internalUse);

View File

@ -10,7 +10,6 @@ import io.kotest.matchers.shouldBe
import net.sourceforge.pmd.lang.java.ast.*
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils
import net.sourceforge.pmd.lang.java.types.STRING
import net.sourceforge.pmd.lang.java.types.TypeTestUtil
import net.sourceforge.pmd.lang.java.types.ast.ExprContext.ExprContextKind.ASSIGNMENT
import net.sourceforge.pmd.lang.java.types.ast.ExprContext.ExprContextKind.INVOCATION
import net.sourceforge.pmd.lang.java.types.parseWithTypeInferenceSpy
@ -303,4 +302,30 @@ class ConversionContextTests : ProcessorTestSpec({
lambda.conversionContext::getKind shouldBe INVOCATION
}
}
parserTest("Ctx of nested invocation") {
val (acu, spy) = parser.parseWithTypeInferenceSpy("""
class Foo {
int eatByte(byte b) {}
int eatInt(int b) {}
void eq(Object val) {
Bar b = (Bar) val;
eatByte(b.x);
eatInt(eatByte(b.x));
}
class Bar { byte x; }
}
""")
val (nonNested, nested) = acu.descendants(ASTFieldAccess::class.java).toList()
spy.shouldBeOk {
nested.conversionContext.targetType shouldBe byte // not int
nonNested.conversionContext.targetType shouldBe byte
nested.conversionContext.kind shouldBe INVOCATION
nonNested.conversionContext.kind shouldBe INVOCATION
}
}
})

View File

@ -440,4 +440,28 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Unboxing of byte in invocation ctx</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>10,11</expected-linenumbers>
<expected-messages>
<message>Unnecessary explicit unboxing</message>
<message>Unnecessary explicit unboxing</message>
</expected-messages>
<code><![CDATA[
public abstract class Example {
Example eatByte(byte b) {
}
int eatObj(Example b) {
}
void eq(Object val) {
Byte b = (Byte) val;
eatByte(b.byteValue());
eatObj(eatByte(b.byteValue()));
}
}
]]></code>
</test-code>
</test-data>