Skip to content
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

Use StringBuilder#isEmpty() instead of comparing size() #336

Open
koppor opened this issue Oct 13, 2023 · 11 comments
Open

Use StringBuilder#isEmpty() instead of comparing size() #336

koppor opened this issue Oct 13, 2023 · 11 comments
Labels
good first issue Good for newcomers recipe Recipe requested

Comments

@koppor
Copy link

koppor commented Oct 13, 2023

Similar to https://docs.openrewrite.org/recipes/staticanalysis/isemptycalloncollections:

-                        if ((current.length() > 0) && !Character.isWhitespace(current.charAt(current.length() - 1))
+                        if ((!current.isEmpty()) && !Character.isWhitespace(current.charAt(current.length() - 1))

Example: https://github.com/JabRef/jabref/pull/10489/files#diff-7a00bcdda16c9953e17ec2db403404b78ad1a750671c76bd7ec885fdf8ffa776

@timtebeek timtebeek added good first issue Good for newcomers recipe Recipe requested labels Oct 14, 2023
@jeexan2
Copy link

jeexan2 commented Nov 2, 2023

Hello @koppor, can I pick this up? And if the answer is yes, can you add a bit more description regarding this?
cc: @timtebeek

@timtebeek
Copy link
Contributor

Hi @jeexan2 ! Sure; if you open up a draft pull request containing just the tests of what you want to cover, I'll then assign the issue to you. You can start with a test class similar to the one we have already for collections, but then adapted to fit StringBuilder.
https://github.com/openrewrite/rewrite-static-analysis/blob/d6dee9de9eff8136f77b5cf04748db147b51f591/src/test/java/org/openrewrite/staticanalysis/IsEmptyCallOnCollectionsTest.java#L37-L75

@timtebeek
Copy link
Contributor

Implementation-wise the isEmpty call on StringBuilder comes in through java.lang.CharSequence#isEmpty, so you'll want to target that in your recipe. The recipe itself can fit in right next to these existing String recipes in openrewrite/rewrite-migrate-java

@timtebeek
Copy link
Contributor

Actually; come to think of it we already have such recipes in https://github.com/openrewrite/rewrite-migrate-java/blob/v2.1.1/src/main/java/org/openrewrite/java/migrate/lang/UseStringIsEmpty.java , but we need to broaden their matchers to use CharSequence instead of String!

@timtebeek timtebeek transferred this issue from openrewrite/rewrite-static-analysis Nov 2, 2023
@timtebeek
Copy link
Contributor

timtebeek commented Nov 2, 2023

The work still to do for this recipe then becomes:

  • fork and clone rewrite-migrate-java
  • expand the tests to include CharSequence, and optionally StringBuilder
  • update the String recipes to use CharSequence where possible
  • create a PR containing the above changes, and tag me for review

Thanks a lot for the suggestion & offer to help!

@timtebeek
Copy link
Contributor

@jeexan2 (or anyone else looking to pick this up) I've added some clickable links to the above comment; hope that helps!

@gundamaiaha
Copy link

@timtebeek I am a new contributer. Shall I pick this ?

@timtebeek
Copy link
Contributor

That would be appreciated, yes please! Feel free to start with a draft pull request containing just a test, and then I'll assign the issue to you.

@gundamaiaha
Copy link

@timtebeek sure.

@koppor
Copy link
Author

koppor commented Jan 24, 2024

I tried replicating this one, but it seems this test already passes unexpectedly when added to

If I understand the test correctly, it assures that nothing happens if > 1 is used.

My request was a) for > 0 (!)

@timtebeek
Copy link
Contributor

Ah yes, of course; I'm multitasking too much. This one is interesting.
image

We could fit that in that way if we opt for the Java 9+ recipes we're adding in:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers recipe Recipe requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

4 participants