You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is continue of discussion started in #7019 (comment)
Expected Behavior of the rule
The rule should report explicit call of operator fun where it is possible to use its operator form.
Config:
RedundantExplicitOperatorCall:
enabled: true# This is just an example, list of default operators is not defined yetoperators:
- get
- set
- not
Examples:
val featureFlags:Map<String, Boolean> =...
// Noncompliantval isMyFeatureEnabled = featureFlags.get("myFeature")
if (isMyFeatureEnabled.not()) { ... }
// Compliantval isMyFeatureEnabled = featureFlags["myFeature"]
if (!isMyFeatureEnabled) { ... }
Exceptions to the rule
With nullable types. Explicit call with nullable type should not be reported, as operators couldn't be called on nullable types (unless it is extension operator for nullable types):
val featureFlags:Map<String, Boolean>?=...
featureFlags?.get("myFeature") // OK// but if we have an extension-function for nullable typeoperatorfunMap<String, Boolean>?.get(key:String) =this?.get(key) ?:false
featureFlags.get("myFeature") // Not OK
Call on implicit receiver. The only possible way to use operators with implicit receiver is to use this explicitly, so maybe such cases shouldn't be reported:
with(featureFlags) {
if (get("myFeature1")) println("OK")
if (this["myFeature2"]) println("Also OK")
}
Call chain. It may be not appreciated to use operator in a code written in functional style.
sequenceOf(1, 2, 3)
.filter { it %2==0 }
.plus(42) // It is an operator but it is OK to use it in call chain
.forEach(::println)
// With operator call it could be
(sequenceOf(1, 2, 3)
.filter { it %2==0 }
+42)
.forEach(::println)
It might be useful to restrict explicit usage of some operators according to project code style (see Redundant usage of Boolean.not() #7019 for example)
There already exists the rule promoting [] over get and set - ExplicitCollectionElementAccessMethod. This rule could be implemented via the new rule
The text was updated successfully, but these errors were encountered:
This issue is continue of discussion started in #7019 (comment)
Expected Behavior of the rule
The rule should report explicit call of
operator fun
where it is possible to use its operator form.Config:
Examples:
Exceptions to the rule
this
explicitly, so maybe such cases shouldn't be reported:ExplicitCollectionElementAccessMethod
with spread operator #5640Context
[]
overget
andset
- ExplicitCollectionElementAccessMethod. This rule could be implemented via the new ruleThe text was updated successfully, but these errors were encountered: