Skip to content

Commit

Permalink
fix: emit warning on MethodHandles.constant(void.class, ...) (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Jun 26, 2023
1 parent a1a9334 commit 3fc5a7c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ class MethodHandleCreationInspection: LocalInspectionTool() {
type: MhExactType,
parameter: PsiType
): Boolean {
if (type.signature.returnType is PsiPrimitiveType) {
return TypeConversionUtil.isAssignable(type.signature.returnType, parameter)
val returnType = type.signature.returnType
// void.class in invalid in constant(...), so skip here and handle it later separately
if (returnType == PsiType.VOID) return true
if (returnType is PsiPrimitiveType) {
return TypeConversionUtil.isAssignable(returnType, parameter)
}
return TypeConversionUtil.areTypesConvertible(type.signature.returnType, parameter)
return TypeConversionUtil.areTypesConvertible(returnType, parameter)
}

private fun checkParamNotVoidAt(expression: PsiMethodCallExpression, index: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class MethodHandleInspectionsTest : LightJavaCodeInsightFixtureTestCase() {

fun testWrongArgumentTypeInConstant() = doTest()

fun testVoidInConstant() = doTest()

}
6 changes: 6 additions & 0 deletions src/test/testData/VoidInConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

class VoidInIdentity {
private static final MethodHandle A = MethodHandles.constant(<warning descr="Parameter must not be of type void.">void.class</warning>, null);
}

0 comments on commit 3fc5a7c

Please sign in to comment.