2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
Curly braces can be omitted from a one-line block, such as with an ``++if++`` statement or ``++for++`` loop, but doing so can be misleading and induce bugs.
2020-06-30 12:48:07 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
This rule raises an issue when the whitespacing of the lines after a one line block indicates an intent to include those lines in the block, but the omission of curly braces means the lines will be unconditionally executed once.
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
Note that this rule considers tab characters to be equivalent to 1 space. If you mix spaces and tabs you will sometimes see issues in code which looks fine in your editor but is confusing when you change the size of tabs.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:07 +02:00
----
if (condition)
firstActionInBlock();
secondAction(); // Noncompliant; executed unconditionally
thirdAction();
if (condition) firstActionInBlock(); secondAction(); // Noncompliant; secondAction executed unconditionally
if (condition) firstActionInBlock(); // Noncompliant
secondAction(); // Executed unconditionally
if (condition); secondAction(); // Noncompliant; secondAction executed unconditionally
let str = undefined;
for (let i = 0; i < array.length; i++)
str = array[i];
doTheThing(str); // Noncompliant; executed only on last array element
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:07 +02:00
----
if (condition) {
firstActionInBlock();
secondAction();
}
thirdAction();
let str = undefined;
for (let i = 0; i < array.length; i++) {
str = array[i];
doTheThing(str);
}
----
2023-05-03 11:06:20 +02:00
== Resources
2020-12-21 15:38:52 +01:00
2022-04-07 08:53:59 -05:00
* https://cwe.mitre.org/data/definitions/483[MITRE, CWE-483] - Incorrect Block Delimitation
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)
include::../message.adoc[]
include::../highlighting.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]