JetBrains/intellij-community · error · ParseException

inspection.contract.checker.empty.constraint

inspection.contract.checker.empty.constraint

Error message

Constraint should not be empty

What it means

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).

Source

Thrown at java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardMethodContract.java:258

      for (int i = 0; i < args.length; i++) {
        args[i] = parseConstraint(argStrings[i], text, clauseIndex, i);
      }
    }
    else {
      args = new ValueConstraint[0];
    }
    String returnValueString = clause.substring(arrowIndex + arrow.length());
    ContractReturnValue returnValue = ContractReturnValue.valueOf(returnValueString);
    if (returnValue == null) {
      String possibleValues = "null, !null, true, false, this, new, paramN, fail, _";
      String message = JavaAnalysisBundle.message("inspection.contract.checker.unknown.return.value", possibleValues, returnValueString);
      throw ParseException.forReturnValue(message, text, clauseIndex);
    }
    return new StandardMethodContract(args, returnValue);
  }

  private static ValueConstraint parseConstraint(String name, String text, int clauseIndex, int constraintIndex) throws ParseException {
    if (StringUtil.isEmpty(name)) throw new ParseException(JavaAnalysisBundle.message("inspection.contract.checker.empty.constraint"));
    for (ValueConstraint constraint : ValueConstraint.values()) {
      if (constraint.toString().equals(name)) return constraint;
    }
    String allowedClause = StreamEx.of(ValueConstraint.values()).joining(", ");
    String message = JavaAnalysisBundle.message("inspection.contract.checker.unknown.constraint", allowedClause, name);
    throw ParseException.forConstraint(message, text, clauseIndex, constraintIndex);
  }

  public enum ValueConstraint {
    ANY_VALUE("_", ContractReturnValue.returnAny()),
    NULL_VALUE("null", ContractReturnValue.returnNull()),
    NOT_NULL_VALUE("!null", ContractReturnValue.returnNotNull()),
    TRUE_VALUE("true", ContractReturnValue.returnTrue()),
    FALSE_VALUE("false", ContractReturnValue.returnFalse());

    private final String myPresentableName;
    private final ContractReturnValue myCorrespondingReturnValue;

View on GitHub (pinned to be881553f2)

Solutions

  1. Fix the @Contract value so every constraint slot has a token: remove double/leading/trailing commas before '->'
  2. Use the IDE's intention/quick-fix on the annotated method, which offers valid contract completions
  3. If generating contracts programmatically, filter empty strings before joining

Example fix

// before
@Contract("_, ,null -> fail")
// after
@Contract("_,null -> fail")
Defensive patterns

Strategy: validation

Validate before calling

static boolean contractClauseWellFormed(String clause) {
  String constraints = clause.substring(0, clause.indexOf("->")).trim();
  if (constraints.isEmpty()) return constraints.equals("_"); // explicit single '_' is fine
  for (String c : constraints.split(",", -1)) {
    if (c.isBlank()) return false; // empty constraint slot -> ParseException
  }
  return true;
}

Try / catch

try { StandardMethodContract.parseContractText(text); } catch (ParseException e) { /* highlight the clause/constraint position given by e */ }

Prevention

When it happens

Trigger: 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 '->'.

Common situations: 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.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/e2f31ac3ad7a34b5. Report an issue: GitHub.