PMD-jPinpoint-rules icon indicating copy to clipboard operation
PMD-jPinpoint-rules copied to clipboard

Kotlin/avoid implicit recompiling regex

Open stokpop opened this issue 1 year ago • 4 comments

This is a big one. Not all tests and examples work the same for Kotlin compared to Java.

There is no difference for short or long Regexp's as far as I can see.

Basically you should not call .toRegex() for each invocation. Some String methods, like split() have versions without Regex, with String, these are advertised as more effictive.

We could add checks to see if strings are really regexps or could be used as plain string.

Need some help with this one (now not checked in unit test):

        val fs = FileSystems.getDefault()
        p = fs.getPathMatcher(PAT_STRING) // bad: violation

Where match should be made on the fs and the corresponding var to check if it is made of FileSystems.

The unit test can be cleaned up and rearranged when all are working.

stokpop avatar Jun 18 '24 15:06 stokpop

There is no difference for short or long Regexp's as far as I can see.

The number of characters to process in the compile is different and so the amount of work is, right? Every time a compile if 2 chars is not so bad, of 20 chars is bad. This holds for Kotlin too, I assume.

jborgers avatar Jun 25 '24 15:06 jborgers

There is no difference for short or long Regexp's as far as I can see.

The number of characters to process in the compile is different and so the amount of work is, right? Every time a compile if 2 chars is not so bad, of 20 chars is bad. This holds for Kotlin too, I assume.

On the other hand, the Kotlin Regex can easily be used from a (companion object) field.

stokpop avatar Jun 28 '24 12:06 stokpop

I think we need to add rules for the default constructor Regex("bla.*") and also toRegex(options)

stokpop avatar Jun 28 '24 12:06 stokpop

I think we need to add rules for the default constructor Regex("bla.*") and also toRegex(options)

also added this

Note that Regex(...) is not so Implicit... can also create a separate rule, but I think it fits with this one

stokpop avatar Jun 03 '25 09:06 stokpop

This PR also includes:

  • upgrade to PMD 7.13.0
  • make unit tests work for both Java and Kotlin
  • remove the maven profiles, both Java and Kotlin can be run from same branch now
  • note: for Kotlin merge use shell command: ./merge kotlin

stokpop avatar Jun 03 '25 09:06 stokpop