-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce StringFormattingCheck
checkstyle rule
#81603
Introduce StringFormattingCheck
checkstyle rule
#81603
Conversation
The new rule Checks for calls to `String#formatted(Object...)` that include format specifiers that are not locale-safe. This method always uses the default `Locale`, and so for our purposes it is safer to use `String#format(Locale, String, Object...)`. Note that this rule can currently only detect violations when calling `formatted()` on a string literal or text block. In theory, it could be extended to detect violations in local variables or statics.
Pinging @elastic/es-delivery (Team:Delivery) |
Wouldn't it be simpler to just use forbidden apis for this? We'd just have to add the method signature here. |
It would be simpler, yes, but the |
How would that differ to this checkstyle rule? In what scenarios are we allowing it's use? You can always use |
This PR arose from #80751 where we're moving to text blocks in a lot of places, and at present it uses There are 2 ways of looking at this:
I suppose a problem with allowing Basically, I like the flow and succinctness of being able to do e.g. |
Right, how do we determine what is a "safe" usage though. Does the current checkstyle rule take that into account? |
Maybe just banning it production code but allowing it in test code is a good compromise. I suspect we intend to use it in tests much more often, and the terseness will make reading tests considerably easier so there's considerable benefit there. |
@arteam and I both went through an exercise of using all the format specifier with a locale known to be problematic, and this checkstyle rule checks for those format specifiers that will break. I think banning in prod and allowing in test is a good compromise, I'll adjust this PR accordingly. |
I've added a forbidden APIs signature in Am I right in saying that |
There's no explicit ordering between them so far as I understand. We don't want to make checkstyle run after forbiddenApis because the latter requires compiling the code and the former does not. Such an ordering rule would now make checkstyle wait on code compilation to finish which would reduce build performance. |
OK, so what do we think - OK to merge? @arteam WDYT? |
Do we still need this check if we have added the method to forbidden APIs? Isn't this redundant? |
Well I only banned it in non-test code, which is what I thought you were suggesting? |
We still want to prevent the user to use |
The check looks good to me! |
Ah, ok. I wasn't aware the check was that specific. Roger that. |
Some practical examples:
|
I've merged #80751, let's check this rule on the new changes! |
@elasticmachine update branch |
The new rule found some problems, which I've addressed. |
Thank you! |
@elasticmachine run elasticsearch-ci/eql-correctness |
Unsurprisingly, I also had to make some changes for the new forbidden API config. @mark-vieira Back to you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Can this be backported to 8.0 ? We need it to resolve #81804 |
The new rule Checks for calls to `String#formatted(Object...)` that include format specifiers that are not locale-safe. This method always uses the default `Locale`, and so for our purposes it is safer to use `String#format(Locale, String, Object...)`. Note that this rule can currently only detect violations when calling `formatted()` on a string literal or text block. In theory, it could be extended to detect violations in local variables or statics. Note that this change also forbids `.formatted()` in server code, so we are only permitted to use `.formatted()` in test code.
The new rule Checks for calls to
String#formatted(Object...)
thatinclude format specifiers that are not locale-safe. This method always
uses the default
Locale
, and so for our purposes it is safer to useString#format(Locale, String, Object...)
.Note that this rule can currently only detect violations when calling
formatted()
on a string literal or text block. In theory, it could beextended to detect violations in local variables or statics.