rspec/rules/S3110/swift/rule.adoc

48 lines
1.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
The conventional expectation of operators that end with ``++=++``, such as ``+++=++``, ``++-=++``, ``++*=++``, and so on, is that the result of the operation will be assigned to the operand on the left-hand side of the operator.
Define any other behavior and you almost guarantee that the users of your code will misunderstand and therefore misuse your operator.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
func **= (p1:Int, p2:Int) -> Int { // Noncompliant. Change operator name or update value of first parameter
return p1 ** p2
}
func => (p1:Int, p2:Int) -> Int { // Compliant; doesn't end with '='
return p1 ** p1 ** p2
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
func **= (inout p1:Int, p2:Int) {
p1 = p1 ** p2
}
func => (p1:Int, p2:Int) -> Int {
return p1 ** p1 ** p2
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Assign the value of the operation to "xxx".
endif::env-github,rspecator-view[]