[java-dfa] Avoid false-positive NPE warnings inside java.lang.System source

Fixes IDEA-316248 Inspection "Method invocation ‘println’ will produce ‘NullPointerException’" on System.err

GitOrigin-RevId: 0d257c09bff9712299bc749288703d057dfa3fdb
This commit is contained in:
Tagir Valeev
2023-04-17 14:18:10 +00:00
committed by intellij-monorepo-bot
parent 622ea3b218
commit da62a731ac
3 changed files with 15 additions and 2 deletions
@@ -120,7 +120,7 @@ public final class JavaDfaValueFactory {
private static DfaValue createReferenceValue(DfaValueFactory factory, @NotNull PsiReferenceExpression refExpr) {
PsiElement target = refExpr.resolve();
if (target instanceof PsiVariable variable) {
if (!PsiUtil.isAccessedForWriting(refExpr)) {
if (!PsiUtil.isAccessedForWriting(refExpr) && !PlainDescriptor.hasInitializationHacks(variable)) {
DfaValue constValue = getConstantFromVariable(factory, variable);
if (constValue != null && !maybeUninitializedConstant(constValue, refExpr, variable)) return constValue;
}
@@ -0,0 +1,12 @@
// IDEA-316248
package java.lang;
import java.io.PrintStream;
class System {
public static final PrintStream out = null;
void test() {
out.println(1);
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInspection;
import com.intellij.JavaTestUtil;
@@ -724,4 +724,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testTryWithResourcesCloseThrows() { doTest(); }
public void testBooleanOrEquals() { doTest(); }
public void testDuplicatedByPointlessBooleanInspection() { doTest(); }
public void testSystemOutNullSource() { doTest(); }
}