-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
Add AvoidReferentialEquality rule #3924
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
|
||
assertThat(actual).hasSize(2) | ||
} | ||
} |
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.
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)
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.
What do you mean by String === Any
?
This does not compile.
val b = "s" === 42
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.
Great work 🚀
...les-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/ReferentialEquality.kt
Outdated
Show resolved
Hide resolved
...les-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/ReferentialEquality.kt
Outdated
Show resolved
Hide resolved
...les-errorprone/src/main/kotlin/io/gitlab/arturbosch/detekt/rules/bugs/ReferentialEquality.kt
Outdated
Show resolved
Hide resolved
detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/SimpleGlob.kt
Outdated
Show resolved
Hide resolved
detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/internal/SimpleGlobSpec.kt
Outdated
Show resolved
Hide resolved
0c036fe
to
278f696
Compare
After rebase, the branch fails on the CI. [edit]
Is there anythig I can do about it? |
Can you try to |
Sorry, I should have been more specific. It only happens in the CI. |
…etekt/rules/bugs/ReferentialEquality.kt Co-authored-by: Nicola Corti <corti.nico@gmail.com>
…etekt/rules/bugs/ReferentialEquality.kt Co-authored-by: Nicola Corti <corti.nico@gmail.com>
278f696
to
637afa7
Compare
Are we ready to merge this? |
val regex = globPattern | ||
.replace("\\", "\\\\") | ||
.replace(".", "\\.") | ||
.replace("*", ".*") | ||
.replace("?", ".") | ||
.toRegex() |
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.
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.
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.
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
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.
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.
This closes #3363