Skip to content

Commit

Permalink
PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-wielage-sonarsource committed Mar 20, 2024
1 parent d710455 commit f193e8a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
3 changes: 2 additions & 1 deletion rules/S1481/azureresourcemanager/metadata.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{
}

}
14 changes: 8 additions & 6 deletions rules/S1481/azureresourcemanager/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ include::../rationale.adoc[]

== How to fix it in ARM Templates

Remove the unused variable, after ensuring it is not part of an incomplete implementation leading to bugs.
The fix for this issue is straightforward.
Once you ensure the unused variable is not part of an incomplete implementation leading to bugs, you just need to remove it.

=== Code examples

Expand All @@ -13,8 +14,8 @@ Remove the unused variable, after ensuring it is not part of an incomplete imple
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"variables": {
"virtualMachinesName": "[concat('virtualMachinesResource', uniqueString(resourceGroup().id))]",
"unusedVariable": "unusedValue"
"unusedVariable": "unusedValue",
"virtualMachinesName": "[uniqueString(resourceGroup().id)]"
},
"resources": [
{
Expand All @@ -34,7 +35,7 @@ Remove the unused variable, after ensuring it is not part of an incomplete imple
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"variables": {
"virtualMachinesName": "[concat('virtualMachinesResource', uniqueString(resourceGroup().id))]"
"virtualMachinesName": "[uniqueString(resourceGroup().id)]"
},
"resources": [
{
Expand All @@ -49,16 +50,17 @@ Remove the unused variable, after ensuring it is not part of an incomplete imple

== How to fix it in Bicep

Remove the unused variable, after ensuring it is not part of an incomplete implementation leading to bugs.
The fix for this issue is straightforward.
Once you ensure the unused variable is not part of an incomplete implementation leading to bugs, you just need to remove it.

=== Code examples

==== Noncompliant code example

[source,bicep,diff-id=2,diff-type=noncompliant]
----
var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'
var unusedVariable = 'unusedValue' # Noncompliant
var virtualMachinesName = '${uniqueString(resourceGroup().id)}'
resource demoAccount 'Microsoft.Compute/virtualMachines@2023-09-01' = {
name: virtualMachinesName
Expand Down
10 changes: 5 additions & 5 deletions rules/S1481/rationale.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ An unused local variable is a variable that has been declared but is not used an

Having unused local variables in your code can lead to several issues:

* Decreased Readability: Unused variables can make your code more difficult to read. They add extra lines and complexity, which can distract from the main logic of the code.
* *Decreased Readability*: Unused variables can make your code more difficult to read. They add extra lines and complexity, which can distract from the main logic of the code.

* Misunderstanding: When other developers read your code, they may wonder why a variable is declared but not used. This can lead to confusion and misinterpretation of the code's intent.
* *Misunderstanding*: When other developers read your code, they may wonder why a variable is declared but not used. This can lead to confusion and misinterpretation of the code's intent.

* Potential for Bugs: If a variable is declared but not used, it might indicate a bug or incomplete code. For example, if you declared a variable intending to use it in a calculation, but then forgot to do so, your program might not work as expected.
* *Potential for Bugs*: If a variable is declared but not used, it might indicate a bug or incomplete code. For example, if you declared a variable intending to use it in a calculation, but then forgot to do so, your program might not work as expected.

* Maintenance Issues: Unused variables can make code maintenance more difficult. If a programmer sees an unused variable, they might think it is a mistake and try to 'fix' the code, potentially introducing new bugs.
* *Maintenance Issues*: Unused variables can make code maintenance more difficult. If a programmer sees an unused variable, they might think it is a mistake and try to 'fix' the code, potentially introducing new bugs.

* Memory Usage: Although modern compilers are smart enough to ignore unused variables, not all compilers do this. In such cases, unused variables take up memory space, leading to inefficient use of resources.
* *Memory Usage*: Although modern compilers are smart enough to ignore unused variables, not all compilers do this. In such cases, unused variables take up memory space, leading to inefficient use of resources.

In summary, unused local variables can make your code less readable, more confusing, and harder to maintain, and they can potentially lead to bugs or inefficient memory use. Therefore, it is best to remove them.
2 changes: 1 addition & 1 deletion rules/S6893/azureresourcemanager/metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Use a hard-coded value for the apiVersion",
"title": "Use a constant value for the apiVersion",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
Expand Down
4 changes: 2 additions & 2 deletions rules/S6893/azureresourcemanager/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ when the API version you set for a resource doesn't match the properties in your

== How to fix it in ARM Templates

To avoid these issues, it is recommended to set the `apiVersion` to a hard-coded value for the resource type.
To avoid these issues, it is recommended to set the `apiVersion` to a constant value for the resource type.

When creating a new template, we suggest you use the latest API version for a resource type.

Expand Down Expand Up @@ -99,7 +99,7 @@ ifdef::env-github,rspecator-view[]

=== Message

Use a hard-coded value for the `apiVersion` of this resource.
Use a constant value for the `apiVersion` of this resource.


=== Highlighting
Expand Down

0 comments on commit f193e8a

Please sign in to comment.