Skip to content

FormatString validator complains about incompatible flags even though they are compatible #5460

@MinnDevelopment

Description

@MinnDevelopment

Example:

public class Example {
    @SuppressWarnings("FormatString")
    public static void main(String[] args){
        System.out.printf("%s\n", new ExampleFormattable());
        System.out.printf("%#s\n", new ExampleFormattable());
    }
}

class ExampleFormattable implements Formattable {
    @Override
    public void formatTo(Formatter formatter, int flags, int width, int precision) {
        if ((flags & FormattableFlags.ALTERNATE) != 0) {
            formatter.format("alternate");
        } else {
            formatter.format("normal");
        }
    }
}

Output:

normal
alternate

Compile Error without @SuppressWarnings("FormatString"):

Example.java:10: error: [FormatString] format specifier '%s' is not compatible with the given flag(s): #
        System.out.printf("%#s\n", new ExampleFormattable());
                         ^
    (see https://errorprone.info/bugpattern/FormatString)

This is likely caused by the validator using null for its check, which will throw an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions