diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractInferenceIndex.kt b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractInferenceIndex.kt index c4b2b3db1d69..8701952d1b17 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractInferenceIndex.kt +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ContractInferenceIndex.kt @@ -97,7 +97,7 @@ private fun walkMethodBody(tree: LighterAST, root: LighterASTNode, processor: (L object : RecursiveLighterASTNodeWalkingVisitor(tree) { override fun visitNode(element: LighterASTNode) { val type = element.tokenType - if (type === CLASS || type === ANONYMOUS_CLASS || type === LAMBDA_EXPRESSION) return + if (type === CLASS || type === FIELD || type == METHOD || type == ANNOTATION_METHOD || type === LAMBDA_EXPRESSION) return processor(element) super.visitNode(element) diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy b/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy index 7d5846021f31..6c56a5b59ba1 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInspection/PurityInferenceFromSourceTest.groovy @@ -163,6 +163,55 @@ public Foo() { """ } + void "test anonymous class initializer"() { + assertPure false, """ + Object smth() { + return new I(){{ created++; }}; + } + + private static int created = 0; + + interface I {} + """ + } + + void "test simple anonymous class creation"() { + assertPure true, """ + Object smth() { + return new I(){}; + } + + interface I {} + """ + } + + void "test anonymous class with arguments"() { + assertPure false, """ + Object smth() { + return new I(unknown()){}; + } + + class I { + I(int a) {} + } + """ + } + + void "test class with impure initializer creation"() { + assertPure false, """ + Object smth() { + return new I(42); + } + + class I { + I(int answer) {} + { + launchMissiles(); + } + } + """ + } + private void assertPure(boolean expected, String classBody) { def clazz = myFixture.addClass("final class Foo { $classBody }") assert !((PsiFileImpl) clazz.containingFile).contentsLoaded