PsiReferenceExpression (PsiExpression expression) { if (!(expression instanceof PsiMethodCallExpression call)) return null; if (!PsiTypes.booleanType().equals(call.getType())) return null; PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); if (qualifier != null && SideEffectChecker.mayHaveSideEffects(qualifier)) return null; PsiMethod method = call.resolveMethod(); if (method == null) return null; List<? extends MethodContract> contracts; if (MethodUtils.isEquals(method)) { // Contracts for equals are tuned for some classes to (this == arg#0) -> true; () -> false // so let's rewrite this to (null == arg#0) -> false which is more relevant to this inspection ValueConstraint[] nullArg = {ValueConstraint.NULL_VALUE}; contracts = Collections.singletonList(new StandardMethodContract(nullArg, ContractReturnValue.returnFalse())); } else { contracts = JavaMethodContractUtil.getMethodCallContracts(method, call); } if (contracts.isEmpty()) return null; MethodContract contract = tryCast(contracts.get(0), StandardMethodContract.class); if (contract == null || !contract.getReturnValue().equals(ContractReturnValue.returnFalse())) return null; ContractValue condition = ContainerUtil.getOnlyItem(contract.getConditions()); if (condition == null) return null; int idx = condition.getNullCheckedArgument(true).orElse(-1); if (idx == -1) return null; PsiExpression[] args = ((PsiMethodCallExpression)expression).getArgumentList().getExpressions(); if (args.length <= idx || method.isVarArgs() && idx == args.length - 1) return null; PsiReferenceExpression reference = tryCast(args[idx], PsiReferenceExpression.class); if (reference == null) return null; PsiVariable target = tryCast(reference.resolve(), PsiVariable.class); if (target == null) return null; if (!SyntaxTraverser.psiTraverser(call).filter(PsiReference.class) .filter(ref -> !reference.equals(ref) && ref.isReferenceTo(target)).isEmpty()) { // variable is reused for something else return null; } if (Stream.of(args).anyMatch(SideEffectChecker::mayHaveSideEffects)) return null; return reference; }