Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions rules/S8049/apex/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"title": "While loops should use braces",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "1 min"
},
"tags": [
"convention",
"formatting"
],
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-8049",
"sqKey": "S8049",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "BLOCKER",
"MAINTAINABILITY": "BLOCKER"
},
"attribute": "CONVENTIONAL"
}
}
63 changes: 63 additions & 0 deletions rules/S8049/apex/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
This rule raises an issue when a `while` loop does not use braces to enclose its body, even if the body contains only a single statement.

== Why is this an issue?

While loops without braces can lead to maintenance issues and bugs. When the loop body contains only one statement, it's tempting to omit the braces for brevity.

However, this practice creates several problems:

* **Accidental logic errors**: When developers later add statements after the loop, they might assume these new statements are part of the loop body. Without braces, only the first statement actually belongs to the loop.
* **Reduced readability**: Braces make the loop structure immediately clear to anyone reading the code.
* **Inconsistent formatting**: Different developers might format single-statement loops differently, making the codebase harder to maintain.

Consider this example:

[source,apex]
----
while (i < items.size())
processItem(items[i]);
i++; // This line is NOT part of the loop!
----

In this case, the increment statement runs only once after the loop completes, creating an infinite loop. With braces, this mistake would be immediately obvious.

=== What is the potential impact?

This issue can lead to logic errors and infinite loops when developers mistakenly add statements they believe are part of the loop body. It also reduces code maintainability and consistency across the codebase.

== How to fix it

Add braces around the while loop body, even for single statements. Place the opening brace on the same line as the while statement and the closing brace on its own line.

=== Code examples

==== Noncompliant code example

[source,apex,diff-id=1,diff-type=noncompliant]
----
while (i < 10)
i++; // Noncompliant
----

==== Compliant solution

[source,apex,diff-id=1,diff-type=compliant]
----
while (i < 10) {
i++;
}
----

== Resources

=== Documentation

* Apex Developer Guide - Control Flow Statements - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops.htm[Official Salesforce documentation on loop statements in Apex]

=== Standards

* CWE-483: Incorrect Block Delimitation - https://cwe.mitre.org/data/definitions/483.html[Covers issues related to incorrect use of block delimiters that can lead to logic errors]

=== Related rules

* RSPEC-121 - https://rules.sonarsource.com/csharp/RSPEC-121/[Control structures should use curly braces (C# version)]
2 changes: 2 additions & 0 deletions rules/S8049/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}