boolean (PsiElement anchor) { if (!(anchor instanceof PsiExpression expression)) return false; // Don't report System.out.println(b = false) or doSomething((Type)null) if (anchor instanceof PsiAssignmentExpression || (anchor instanceof PsiTypeCastExpression cast && !(cast.getType() instanceof PsiPrimitiveType))) { return true; } // For conditional the root cause (constant condition or both branches constant) should be already reported for branches if (anchor instanceof PsiConditionalExpression) return true; if (expression instanceof PsiReferenceExpression ref && ("TRUE".equals(ref.getReferenceName()) || "FALSE".equals(ref.getReferenceName())) && ref.resolve() instanceof PsiField field) { PsiClass containingClass = field.getContainingClass(); if (containingClass != null && CommonClassNames.JAVA_LANG_BOOLEAN.equals(containingClass.getQualifiedName())) return true; } if (expression instanceof PsiBinaryExpression binOp) { PsiExpression lOperand = binOp.getLOperand(); PsiExpression rOperand = binOp.getROperand(); IElementType tokenType = binOp.getOperationTokenType(); // Suppress on type mismatch compilation errors if (rOperand == null) return true; PsiType lType = lOperand.getType(); PsiType rType = rOperand.getType(); if (lType == null || rType == null) return true; if (!TypeConversionUtil.isBinaryOperatorApplicable(tokenType, lType, rType, false)) return true; } if (expression instanceof PsiInstanceOfExpression instanceOf) { PsiType type = instanceOf.getOperand().getType(); if (type == null || !TypeConstraints.instanceOf(type).isResolved()) return true; PsiPattern pattern = instanceOf.getPattern(); if (pattern instanceof PsiTypeTestPattern typeTestPattern && typeTestPattern.getPatternVariable() != null) { PsiTypeElement checkType = typeTestPattern.getCheckType(); if (checkType != null && checkType.getType().isAssignableFrom(type)) { // Reported as compilation error return true; } } } //it can be done deliberately, because it is expected to throw exception if (expression instanceof PsiSwitchExpression switchExpression && containsImmediateThrowStatement(switchExpression)) { return true; } PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent()); // Don't report "x" in "x == null" as will be anyway reported as "always true" if (parent instanceof PsiBinaryExpression binOp && ExpressionUtils.getValueComparedWithNull(binOp) != null) return true; // Parent is negation: parent will be reported if (parent instanceof PsiPrefixExpression prefix && prefix.getOperationTokenType().equals(JavaTokenType.EXCL)) return true; // Dereference of null will be covered by other warning if (ExpressionUtils.isVoidContext(expression) || isDereferenceContext(expression)) return true; // We assume all Void variables as null because you cannot instantiate it without dirty hacks // However reporting them as "always null" looks redundant (dereferences or comparisons will be reported though). if (TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_VOID, expression.getType())) return true; if (isFlagCheck(anchor)) return true; if (!isCondition(expression) && expression instanceof PsiMethodCallExpression call) { List<? extends MethodContract> contracts = JavaMethodContractUtil.getMethodCallContracts(call); ContractReturnValue value = JavaMethodContractUtil.getNonFailingReturnValue(contracts); if (value != null) return true; if (!(parent instanceof PsiAssignmentExpression) && !(parent instanceof PsiVariable) && !(parent instanceof PsiReturnStatement)) { PsiMethod method = call.resolveMethod(); if (method == null || !JavaMethodContractUtil.isPure(method)) return true; } } if (new PointlessBooleanExpressionInspection().getExpressionKind(expression) != PointlessBooleanExpressionInspection.BooleanExpressionKind.UNKNOWN) { // avoid double reporting return true; } while (expression != null && BoolUtils.isNegation(expression)) { expression = BoolUtils.getNegated(expression); } if (expression == null) return false; if (!isCondition(expression) && expression instanceof PsiReferenceExpression ref) { PsiVariable variable = tryCast(ref.resolve(), PsiVariable.class); if (variable instanceof PsiField && variable.hasModifierProperty(PsiModifier.STATIC) && ExpressionUtils.isNullLiteral(PsiFieldImpl.getDetachedInitializer(variable))) { return true; } if (variable instanceof PsiLocalVariable && variable.hasInitializer()) { boolean effectivelyFinal = variable.hasModifierProperty(PsiModifier.FINAL) || !VariableAccessUtils.variableIsAssigned(variable, PsiUtil.getVariableCodeBlock(variable, null)); return effectivelyFinal && PsiUtil.isConstantExpression(variable.getInitializer()); } return false; } // Avoid double reporting return expression instanceof PsiMethodCallExpression call && EqualsWithItselfInspection.isEqualsWithItself(call) || expression instanceof PsiBinaryExpression binOp && ComparisonToNaNInspection.extractNaNFromComparison(binOp) != null; }