Create rule S1481: Unused local variables should be removed (#3790)

This commit is contained in:
github-actions[bot] 2024-03-21 08:55:12 +01:00 committed by GitHub
parent 5cc4f345f1
commit 0dfb981d0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 116 additions and 5 deletions

View File

@ -0,0 +1,3 @@
{
}

View File

@ -0,0 +1,108 @@
include::../rationale.adoc[]
== How to fix it in ARM Templates
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,json,diff-id=1,diff-type=noncompliant]
----
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"variables": {
"unusedVariable": "unusedValue",
"virtualMachinesName": "[uniqueString(resourceGroup().id)]"
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('virtualMachinesName')]",
"apiVersion": "2023-09-01",
"location": "[resourceGroup().location]"
}
]
}
----
==== Compliant solution
[source,json,diff-id=1,diff-type=compliant]
----
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"variables": {
"virtualMachinesName": "[uniqueString(resourceGroup().id)]"
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"name": "[variables('virtualMachinesName')]",
"apiVersion": "2023-09-01",
"location": "[resourceGroup().location]"
}
]
}
----
== How to fix it in Bicep
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 unusedVariable = 'unusedValue' // Noncompliant
var virtualMachinesName = '${uniqueString(resourceGroup().id)}'
resource demoAccount 'Microsoft.Compute/virtualMachines@2023-09-01' = {
name: virtualMachinesName
location: resourceGroup().location
}
----
==== Compliant solution
[source,bicep,diff-id=2,diff-type=compliant]
----
var storageName = '${toLower(storageNamePrefix)}${uniqueString(resourceGroup().id)}'
resource demoAccount 'Microsoft.Compute/virtualMachines@2023-09-01' = {
name: virtualMachinesName
location: resourceGroup().location
}
----
== Resources
=== Documentation
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/best-practices#variables[ARM template best practices]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/best-practices#names[Best practices for Bicep]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/variables[Variables in ARM templates]
* Microsoft - https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/variables[Variables in Bicep]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]

View File

@ -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.