Skip to content

Add AvoidReferentialEquality rule #3924

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

Merged
merged 16 commits into from
Jul 22, 2021

Conversation

marschwar
Copy link
Contributor

This closes #3363

@codecov
Copy link

codecov bot commented Jun 30, 2021

Codecov Report

Merging #3924 (637afa7) into main (40abc3f) will increase coverage by 0.04%.
The diff coverage is 93.93%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #3924      +/-   ##
============================================
+ Coverage     83.44%   83.49%   +0.04%     
- Complexity     3149     3162      +13     
============================================
  Files           456      458       +2     
  Lines          9014     9047      +33     
  Branches       1754     1757       +3     
============================================
+ Hits           7522     7554      +32     
  Misses          565      565              
- Partials        927      928       +1     
Impacted Files Coverage Δ
...osch/detekt/rules/bugs/AvoidReferentialEquality.kt 91.30% <91.30%> (ø)
...itlab/arturbosch/detekt/api/internal/SimpleGlob.kt 100.00% <100.00%> (ø)
...turbosch/detekt/rules/bugs/PotentialBugProvider.kt 100.00% <100.00%> (ø)
...lin/io/gitlab/arturbosch/detekt/rules/TypeUtils.kt 100.00% <0.00%> (+100.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 40abc3f...637afa7. Read the comment docs.


assertThat(actual).hasSize(2)
}
}
Copy link
Member

@BraisGabin BraisGabin Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some tests that show that == is never flagged? Other where the type is String === Any and Any === String. And even one more with List === List (without any new pattern)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by String === Any ?

This does not compile.

val b = "s" === 42

Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 🚀

@cortinico cortinico added this to the 1.18.0 milestone Jun 30, 2021
@marschwar marschwar changed the title Add ReferentialEquality rule Add AvoidReferentialEquality rule Jul 2, 2021
@marschwar marschwar force-pushed the feature/referential-equality branch from 0c036fe to 278f696 Compare July 3, 2021 17:42
@marschwar
Copy link
Contributor Author

marschwar commented Jul 3, 2021

After rebase, the branch fails on the CI. [edit]

* What went wrong:
Execution failed for task ':detekt-gradle-plugin:test'.
> Multiple build operations failed.
      Metaspace
      Metaspace
      Metaspace
   > Metaspace
   > Metaspace
   > Metaspace

Is there anythig I can do about it?

@cortinico
Copy link
Member

Is there anythig I can do about it?

Can you try to ./gradlew --stop and re-run?
Also I don't think we merged anything that could have caused this.

@marschwar
Copy link
Contributor Author

Sorry, I should have been more specific. It only happens in the CI.

@marschwar marschwar force-pushed the feature/referential-equality branch from 278f696 to 637afa7 Compare July 7, 2021 20:04
@cortinico
Copy link
Member

Are we ready to merge this?

@cortinico cortinico merged commit 1cf138d into detekt:main Jul 22, 2021
Comment on lines +13 to +18
val regex = globPattern
.replace("\\", "\\\\")
.replace(".", "\\.")
.replace("*", ".*")
.replace("?", ".")
.toRegex()
Copy link
Contributor

@Marcono1234 Marcono1234 Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this does not cover all meta characters. For example [, ], $, ^, (, ) (and probably more) would still be interpreted as regex meta characters.
This is likely not an issue for valid class names (since they should not contain any of these characters?), but users might misuse these characters which could become a maintenance burden.

A saner (but more complex) implementation might be to use Regex.escape for all the literal pieces of the glob pattern.
For example (not tested):

fun buildRegex(globPattern: String): Regex {
    val patternStringBuilder = StringBuilder()
    var nextStart = 0
    
    globPattern.forEachIndexed { index, char ->
        val replacement = when(char) {
            '*' -> ".*"
            '?' -> "."
            else -> null
        }
        
        if (replacement != null) {
            // Append escaped previous text, if any
            if (nextStart < index) {
            	val escaped = Regex.escape(globPattern.substring(nextStart, index))
            	patternStringBuilder.append(escaped)
            }
            // Append meta character replacement
            patternStringBuilder.append(replacement)
            nextStart = index + 1
        }
    }
    
    // Append escaped remainder, if any
    if (nextStart < globPattern.length) {
        val escaped = Regex.escape(globPattern.substring(nextStart))
        patternStringBuilder.append(escaped)
    }
    
    return Regex(patternStringBuilder.toString())
}

Note however, that I am not a member of this project, so this is at most a suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but users might misuse these characters which could become a maintenance burden.

Is this an edge case or are there a real examples to support this? I'm trying to understand from which prospective you're mentioning users misusing those characters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is probably an edge case.

It looks like the intention here was to create a simple glob pattern. However, a user might by accident use a regex pattern such as java.lang.(String|Integer) (which the current implementation permits) and then expect that this works in the future as well. This could become a maintenance burden because the intention here was not to support something like this.

But this is probably negligible, so feel free to ignore.

@marschwar marschwar deleted the feature/referential-equality branch July 27, 2021 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check for === instead of ==
4 participants