JetBrains/intellij-community · error · FormatDecode.IllegalFormatException
zero padding flag ''0'' used but width not specified in ''{0
Error message
zero padding flag ''0'' used but width not specified in ''{0}'' What it means
Thrown when a specifier uses the zero-padding flag '0' but specifies no field width. Zero padding means 'pad with zeros up to width N'; without a width there is nothing to pad, so java.util.Formatter rejects it. FormatDecode applies the identical rule (ZERO_PAD set while width is empty) when validating format strings for the IG inspections.
Source
Thrown at java/java-analysis-impl/src/com/siyeh/ig/format/FormatDecode.java:374
}
}
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);
if (!isPrefix) {
checkText(endString);
}
else {
//only case, when fsPattern doesn't find a specifier, is when there is no any conversion.
//add "s" as a random reasonable conversion
String suggestedString = endString + "s";
Matcher endMatcher = fsPattern.matcher(suggestedString);
if (!endMatcher.find() || endMatcher.end() != suggestedString.length()) {
checkText(endString);
}
}View on GitHub (pinned to be881553f2)
Solutions
- Add a width after the flags: "%05d" zero-pads an integer to 5 digits.
- If you only wanted to print the number unchanged, drop the '0' flag entirely: "%d".
- When constructing specifiers programmatically, emit the '0' flag only when a width is also emitted.
Example fix
// before
String s = String.format("%0d", 42);
// after
String s = String.format("%05d", 42); Defensive patterns
Strategy: validation
Validate before calling
static void checkZeroPadHasWidth(String fmt) {
java.util.regex.Matcher m = java.util.regex.Pattern.compile("%(0)(\d+)?[a-zA-Z]").matcher(fmt);
while (m.find()) {
if (m.group(2) == null) throw new IllegalArgumentException("'0' without width: " + m.group());
}
} Prevention
- Remember '0' is a flag, not the first digit of the width
- Lint generated format strings in CI with a small regex harness
When it happens
Trigger: A specifier such as "%0d" or "%05.1f"-style strings where the width got dropped, leaving '0' with no following width digits (e.g. "%0f"), decoded by FormatDecode.
Common situations: Deleting the width while editing a format string; building specifiers from flag and width variables where the width is empty; misreading '0' as part of the width rather than as a flag.
Related errors
- illegal flag combination ''{0}'' and ''{1}'' in ''{2}''
- left justify flag ''-'' used but width not specified in ''{0
- precision (''{0}'') not allowed in ''{1}''
- {2, choice, 1#flag|1<flags} ''{0}'' not allowed in ''{1}''
- unexpected character ''{0}'' in ''{1}''
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/5b98e008b378b40f.
Report an issue: GitHub.