2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
Use a ``+`` with two numbers and you'll get addition. But use it with a string and anything else, and you'll get concatenation. This could be confusing, especially if it's not obvious that one of the operands is a string. It is recommended to explicitly convert the non-string component to make it easier to understand to future maintainers.
2024-04-23 14:20:10 +02:00
This rule raises an issue when `pass:[+]` or `pass:[+=]` is used with a string and a non-string.
2021-04-28 16:49:39 +02:00
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
function foo() {
let x = 5 + 8; // okay
let z = "8"
return x + z; // Noncompliant; yields string "138"
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
function foo() {
let x = 5 + 8;
let z = "8"
return x + Number(z);
}
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Review this expression to be sure that the concatenation was intended.
=== Highlighting
* Primary: operator ``+``
* Additional: operands
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 20 Nov 2015, 17:20:49 Elena Vilchik wrote:
\[~ann.campbell.2] Perfect, thanks!
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]