google / google/error-prone

FormatStringAnnotation checker is annoyingly stricter than FormatString

Open
#4,411 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

In our project we use a simple custom wrapper over `String.format()`.Error-Prone has FormatStringAnnotation that would check for inconsistencies in format string, but unfortunately it is _stricter_ (unwantedly) than FormatString. In other words, certain code that uses `String.format()` gets accepted, but results in compilation error as soon as that is replaced with `@FormatMethod`-annotated wrapper, with zero other changes.

Testcase (I can attach a full Gradle project if wanted):

package foo;

import com.google.errorprone.annotations.FormatMethod;
import com.google.errorprone.annotations.FormatString;

public class Test
{
public static void main (String[] args)
{
// No errors or warnings here.
System.out.println (String.format (args[Integer.parseInt (args[0])], "foo"));
// But here it says: "Format strings must be either literals or variables. Other expressions are not valid."
System.out.println (myFormat (args[Integer.parseInt (args[0])], "foo"));
}

@FormatMethod
public static String myFormat (@FormatString String format, Object... args)
{
return String.format (format, args);
}
}

I don't see any reason why a call to a `@FormatMethod`-annotated method would be checked differently compared to a call to `String.format()`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.