palantir / javapoet

Java library used to generate Java source files.
Apache License 2.0
7 stars 5 forks source link

`CodeBlock.Builder#add` does not detect unused args when no placeholder exists #65

Open Marcono1234 opened 6 days ago

Marcono1234 commented 6 days ago

Version

0.2.0

What happened?

CodeBlock.Builder#add is supposed to verify that all arguments are used (see its implementation), however if you accidentally forget to add any placeholders (or mistype them), then no validation is performed:

// Bug: No exception is thrown
CodeBlock.of("test", 1, 2);
// Works correctly: Exception is thrown
CodeBlock.of("test $L", 1, 2);

What did you want to happen?

CodeBlock.Builder#add should probably have a check for if (!hasRelative && !hasIndexed && args.length > 0).

skku-daniilkim commented 15 hours ago

I feel like this.formatParts.size() <= 1 && args.length > 0feels more correct. Both work (all tests are passed), but I'm afraid of edge cases, so this.formatParts.size() == 1 or this.formatParts.size() <= 1 feels kinda safer imo. I will provide a PR soon.

Edit: it turns out I was totally wrong 😅