-
-
Notifications
You must be signed in to change notification settings - Fork 766
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
Check for === instead of == #3363
Comments
I suggest to create a custom rule here. Detekt tries to include rules that are relevant for the majority of the user base. IntelliJ also doesn’t flag the mentioned use case. If you have a lot of TypeScript developers in your team, a custom rule is the way to go. https://detekt.github.io/detekt/extensions.html#custom-rulesets |
I think not using === is common to everyone, it just comes up frequently for us. Still, a custom rule it is |
If you want to check the referential equality of two objects in Kotlin, you should use the
That's completely okay. Different teams have different needs and use cases. For that reason, detekt includes the possibility to write a custom rule. In this case, it should be fairly easy. |
Has such a rule actually been added? The changelog for 1.16.0 references this issue (see also all the linked pull requests above), however based on the comments it sounds like this rule has not been added. Would it make sense to have such a rule at least for some standard types? Consider this broken example code: fun main() {
val other = "100" + "0".dropLast(0)
println(other)
println("1000" === other);
} (That is a common bug pattern detected by many Java static code analysers, e.g. Sonar Source) |
Nope, it has not. It was a mistake on our end to include this into the
I think the reasons mentioned by @schalkms still holds, also for your code sample.
Java has a lot more of confusions related to usages of |
I understood their comment as response to the request for a general rule flagging all
I am not that familiar with Kotlin, but to me it would appear that |
I agree with @Marcono1234 that for |
I think there should probably just be a plugin for this check so folks can turn it on. I think it should be simple to add, but I just haven't found the time |
Agree, perhaps we can add a rule restricted to |
If there is work to support String then can it be a regex or config param that defaults to String? Then a team like mine would just change that to |
I don't see why not. But instead of regex I would use a list of glob. They are easier to write and we don't need all the power of regex here. |
Expected Behavior of the rule
Prevent usage of instance equality checks
Context
===
and==
are both valid in kotlin. We also write a lot of typescript/javascript on our team (sadly no multiplatform kotlin) and a few of us accidently use===
sometimes instead of==
for equals. There should be a rule to prevent usage of===
as you very rarely want instance equality checks anyways.The text was updated successfully, but these errors were encountered: