diff --git a/rules/S8049/apex/metadata.json b/rules/S8049/apex/metadata.json new file mode 100644 index 00000000000..2bc46ff9196 --- /dev/null +++ b/rules/S8049/apex/metadata.json @@ -0,0 +1,26 @@ +{ + "title": "Loops should use braces", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant/Issue", + "constantCost": "1 min" + }, + "tags": [ + "convention", + "formatting" + ], + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-8049", + "sqKey": "S8049", + "scope": "All", + "defaultQualityProfiles": [], + "quickfix": "unknown", + "code": { + "impacts": { + "RELIABILITY": "LOW", + "MAINTAINABILITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S8049/apex/rule.adoc b/rules/S8049/apex/rule.adoc new file mode 100644 index 00000000000..69f8fd6ce70 --- /dev/null +++ b/rules/S8049/apex/rule.adoc @@ -0,0 +1,63 @@ +This rule raises an issue when a loop does not use braces to enclose its body, even if the body contains only a single statement. + +== Why is this an issue? + +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 loop body, even for single statements. Place the opening brace on the same line as the loop 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) // Noncompliant + i++; +---- + +==== 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 + + * S121 diff --git a/rules/S8049/metadata.json b/rules/S8049/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S8049/metadata.json @@ -0,0 +1,2 @@ +{ +}