{"record":{"id":"e2f31ac3ad7a34b5","repo":"JetBrains/intellij-community","slug":"inspection-contract-checker-empty-constraint","errorCode":"inspection.contract.checker.empty.constraint","errorMessage":"Constraint should not be empty","messagePattern":"Constraint should not be empty","errorType":"validation","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardMethodContract.java","lineNumber":258,"sourceCode":"      for (int i = 0; i < args.length; i++) {\n        args[i] = parseConstraint(argStrings[i], text, clauseIndex, i);\n      }\n    }\n    else {\n      args = new ValueConstraint[0];\n    }\n    String returnValueString = clause.substring(arrowIndex + arrow.length());\n    ContractReturnValue returnValue = ContractReturnValue.valueOf(returnValueString);\n    if (returnValue == null) {\n      String possibleValues = \"null, !null, true, false, this, new, paramN, fail, _\";\n      String message = JavaAnalysisBundle.message(\"inspection.contract.checker.unknown.return.value\", possibleValues, returnValueString);\n      throw ParseException.forReturnValue(message, text, clauseIndex);\n    }\n    return new StandardMethodContract(args, returnValue);\n  }\n\n  private static ValueConstraint parseConstraint(String name, String text, int clauseIndex, int constraintIndex) throws ParseException {\n    if (StringUtil.isEmpty(name)) throw new ParseException(JavaAnalysisBundle.message(\"inspection.contract.checker.empty.constraint\"));\n    for (ValueConstraint constraint : ValueConstraint.values()) {\n      if (constraint.toString().equals(name)) return constraint;\n    }\n    String allowedClause = StreamEx.of(ValueConstraint.values()).joining(\", \");\n    String message = JavaAnalysisBundle.message(\"inspection.contract.checker.unknown.constraint\", allowedClause, name);\n    throw ParseException.forConstraint(message, text, clauseIndex, constraintIndex);\n  }\n\n  public enum ValueConstraint {\n    ANY_VALUE(\"_\", ContractReturnValue.returnAny()),\n    NULL_VALUE(\"null\", ContractReturnValue.returnNull()),\n    NOT_NULL_VALUE(\"!null\", ContractReturnValue.returnNotNull()),\n    TRUE_VALUE(\"true\", ContractReturnValue.returnTrue()),\n    FALSE_VALUE(\"false\", ContractReturnValue.returnFalse());\n\n    private final String myPresentableName;\n    private final ContractReturnValue myCorrespondingReturnValue;\n","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardMethodContract.java#L240-L276","documentation":"ParseException from StandardMethodContract.parseConstraint when a constraint value inside a @Contract clause is empty — e.g. 'null -> ...' style clause where a constraint slot before the arrow is blank ('_,null,,fail -> ...' or a leading comma). Each constraint position must contain one of the ValueConstraint tokens (_, null, !null, true, false, this, new, paramN, fail).","triggerScenarios":"StandardMethodContract.parse (called when reading an @Contract annotation string) encountering a clause whose comma-separated constraint list has an empty element — leading/trailing/double commas before '->'.","commonSituations":"Typos in @Contract strings like \"_, ,null -> fail\" or \"null,true, -> new\"; editing contracts by hand and leaving dangling commas; the IDE inspection 'Contract' checker highlighting the annotation.","solutions":["Fix the @Contract value so every constraint slot has a token: remove double/leading/trailing commas before '->'","Use the IDE's intention/quick-fix on the annotated method, which offers valid contract completions","If generating contracts programmatically, filter empty strings before joining"],"exampleFix":"// before\n@Contract(\"_, ,null -> fail\")\n// after\n@Contract(\"_,null -> fail\")","handlingStrategy":"validation","validationCode":"static boolean contractClauseWellFormed(String clause) {\n  String constraints = clause.substring(0, clause.indexOf(\"->\")).trim();\n  if (constraints.isEmpty()) return constraints.equals(\"_\"); // explicit single '_' is fine\n  for (String c : constraints.split(\",\", -1)) {\n    if (c.isBlank()) return false; // empty constraint slot -> ParseException\n  }\n  return true;\n}","typeGuard":null,"tryCatchPattern":"try { StandardMethodContract.parseContractText(text); } catch (ParseException e) { /* highlight the clause/constraint position given by e */ }","preventionTips":["Never leave empty slots between commas in @Contract value constraints","Use the IDE's contract intention/quick-fix to build valid values"],"tags":["intellij","java-analysis","contract","parsing","annotations"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}