redundant if statements in transpiled ARM should be simplified
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 21m
- Merged PRs (30d)
- 79
Description
**Bicep version**
run `bicep --version` via the Bicep CLI, `az bicep version` via the AZ CLI or via VS code by navigating to the extensions tab and searching for Bicep
**Describe the bug**
Bicep should not produce an ARM template, where the true and false clause of an if statement are equal. While I use Bicep to write all my Azure code, I have to compile my Bicep into an ARM template to submit to the Azure Marketplace. When they review teh ARM template, the if statement stands out as unneccessary.
Example: `"value": "[if(equals(parameters('newOrExisting'), 'new'), parameters('name'), parameters('name'))]"`
Instead, Bicep should simplify this expression, `"value": "[parameters('name')]"`
This can be fixed a few different ways.
Bicep could warn/error for the user that their teneary operator should be reduced because it returns the same value for true or false.
Or, during compilation, before adding an 'if' statement Bicep should check that the true and false results are different.
**To Reproduce**
Steps to reproduce the behavior:
```bicep
param location string
param name string
param resourceGroupName string = resourceGroup().name
@description('Chose to use a new or existing virtual network')
@allowed([ 'new', 'existing' ])
param newOrExisting string = 'new'
resource newStorageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = if (newOrExisting == 'new') {
name: name
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' existing = if (newOrExisting == 'existing') {
name: name
scope: resourceGroup(resourceGroupName)
}
output id string = newOrExisting == 'new' ? newStorageAccount.id : storageAccount.id
output name string = newOrExisting == 'new' ? newStorageAccount.name : storageAccount.name
```
```json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.16.1.55165",
"templateHash": "10914676989121058361"
}
},
"parameters": {
"location": {
"type": "string"
},
"name": {
"type": "string"
},
"resourceGroupName": {
"type": "string",
"defaultValue": "[resourceGroup().name]"
},
"newOrExisting": {
"type": "string",
"defaultValue": "new",
"allowedValues": [
"new",
"existing"
],
"metadata": {
"description": "Chose to use a new or existing virtual network"
}
}
},
"resources": [
{
"condition": "[equals(parameters('newOrExisting'), 'new')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[parameters('name')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2"
}
],
"outputs": {
"id": {
"type": "string",
"value": "[if(equals(parameters('newOrExisting'), 'new'), resourceId('Microsoft.Storage/storageAccounts', parameters('name')), extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, parameters('resourceGroupName')), 'Microsoft.Storage/storageAccounts', parameters('name')))]"
},
"name": {
"type": "string",
"value": "[if(equals(parameters('newOrExisting'), 'new'), parameters('name'), parameters('name'))]"
}
}
}
```
**Additional context**
Add any other context about the problem here.
Contributor guide
Research direction
Reproduce the issue with the Bicep source and ARM JSON shown in the report, then trace the compiler path that lowers conditional expressions. The work is done when equivalent true and false branches produce the simplified ARM expression, with coverage for the reported output and no regression in genuinely differing branches.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100