JetBrains/intellij-community · error · FormatDecode.IllegalFormatException

illegal flag combination ''{0}'' and ''{1}'' in ''{2}''

Error message

illegal flag combination ''{0}'' and ''{1}'' in ''{2}''

What it means

Thrown while decoding a java.util.Formatter-style format specifier, this IllegalFormatException reports that the flags ' ' (leading space) and '+' (plus) were used together in the same specifier. The rules mirror java.util.Formatter: ' ' and '+' are mutually exclusive because both control the sign of numeric output. In IntelliJ this decode logic powers the 'Format String' inspection family, so the exception surfaces as an inspection error on the offending String.format/String.printf argument.

Source

Thrown at java/java-analysis-impl/src/com/siyeh/ig/format/FormatDecode.java:362

          }
          case 'e', 'E' -> { // floating point -> decimal number in computerized scientific notation
            checkFlags(flagBits, ~GROUP, specifier);
            allowed = new FloatValidator(specifier);
          }
          case 'g', 'G' -> { // scientific notation
            checkFlags(flagBits, ~ALTERNATE, specifier);
            allowed = new FloatValidator(specifier);
          }
          case 'f' -> // floating point -> decimal number
            allowed = new FloatValidator(specifier);
          default -> throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.unknown.conversion", specifier));
        }
      }
      if (precision != null && precision.length() < 2) {
        throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.invalid.precision", specifier));
      }
      if (isAllBitsSet(flagBits, LEADING_SPACE | PLUS)) {
        throw new IllegalFormatException(
          InspectionGadgetsBundle.message("format.string.error.illegal.flag.combination", ' ', '+', specifier));
      }
      if (isAllBitsSet(flagBits, LEFT_JUSTIFY | ZERO_PAD)) {
        throw new IllegalFormatException(
          InspectionGadgetsBundle.message("format.string.error.illegal.flag.combination", '-', '0', specifier));
      }
      if (StringUtil.isEmpty(width)) {
        if (isAllBitsSet(flagBits, LEFT_JUSTIFY)) {
          throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.left.justify.no.width", specifier));
        }
        if (isAllBitsSet(flagBits, ZERO_PAD)) {
          throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.zero.padding.no.width", specifier));
        }
      }
      storeValidator(allowed, pos, parameters, argumentCount);
    }
    if (i < formatString.length()) {
      String endString = formatString.substring(i);

View on GitHub (pinned to be881553f2)

Solutions

  1. Remove one of the two flags: use "%+d" when you always want a sign, or "% d" when you want a space only for non-negative values.
  2. If the specifier is built dynamically, audit the flag-accumulation code so mutually exclusive flags cannot both be set.
  3. Re-run the inspection after the edit to confirm the warning clears.

Example fix

// before
String s = String.format("% +d", 42); // ' ' and '+' together

// after
String s = String.format("%+d", 42); // always show sign
Defensive patterns

Strategy: validation

Validate before calling

// Reject ' ' + '+' in one specifier before formatting
static String checkFlags(String fmt) {
  java.util.regex.Matcher m = java.util.regex.Pattern.compile("%[-#+ 0,(]*").matcher(fmt);
  while (m.find()) {
    String f = m.group();
    if (f.contains(" ") && f.contains("+")) throw new IllegalArgumentException("' ' and '+' combined in " + f);
  }
  return fmt;
}

Try / catch

try { String r = String.format(fmt, arg); } catch (IllegalFormatException e) { /* fall back to plain toString */ }

Prevention

When it happens

Trigger: A format string containing a specifier like "% +d" or "%+ f" — i.e. both the space flag and the plus flag set on the same conversion — passed to String.format, printf, Formatter, or scanned by the IG format-string inspections via FormatDecode.

Common situations: Copy-pasted format strings from C/printf code that allowed '% +d'; hand-built specifiers where flags are appended programmatically and both flags end up set; running the 'Format String' inspection on legacy code.

Related errors


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