Skip to content

Commit

Permalink
fix: improves empty list equality check on conditional expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
bmbferreira committed Jul 1, 2022
1 parent 611a62e commit 2095371
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
40 changes: 23 additions & 17 deletions rules/terraformrules/terraform_empty_list_equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,33 @@ func (r *TerraformEmptyListEqualityRule) Check(runner *tflint.Runner) error {
func (r *TerraformEmptyListEqualityRule) checkEmptyList(runner *tflint.Runner) error {
return runner.WalkExpressions(func(expr hcl.Expression) error {
if conditionalExpr, ok := expr.(*hclsyntax.ConditionalExpr); ok {
if binaryOpExpr, ok := conditionalExpr.Condition.(*hclsyntax.BinaryOpExpr); ok {
if binaryOpExpr.Op.Type.FriendlyName() == "bool" {
if right, ok := binaryOpExpr.RHS.(*hclsyntax.TupleConsExpr); ok {
checkEmptyList(right, runner, r, binaryOpExpr)
}
if left, ok := binaryOpExpr.LHS.(*hclsyntax.TupleConsExpr); ok {
checkEmptyList(left, runner, r, binaryOpExpr)
}
}
}
searchEmptyList(conditionalExpr.Condition, runner, r, conditionalExpr.Range())
}
return nil
})
}

func checkEmptyList(tupleConsExpr *hclsyntax.TupleConsExpr, runner *tflint.Runner, r *TerraformEmptyListEqualityRule, binaryOpExpr *hclsyntax.BinaryOpExpr) {
if len(tupleConsExpr.Exprs) == 0 {
runner.EmitIssue(
r,
"Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
binaryOpExpr.Range(),
)
func searchEmptyList(expr hcl.Expression, runner *tflint.Runner, r *TerraformEmptyListEqualityRule, exprRange hcl.Range) {
if binaryOpExpr, ok := expr.(*hclsyntax.BinaryOpExpr); ok {
if binaryOpExpr.Op.Type.FriendlyName() == "bool" {
searchEmptyList(binaryOpExpr.RHS, runner, r, binaryOpExpr.Range())
searchEmptyList(binaryOpExpr.LHS, runner, r, binaryOpExpr.Range())
}
} else if binaryOpExpr, ok := expr.(*hclsyntax.BinaryOpExpr); ok {
searchEmptyList(binaryOpExpr, runner, r, binaryOpExpr.Range())
} else if parenthesesExpr, ok := expr.(*hclsyntax.ParenthesesExpr); ok {
searchEmptyList(parenthesesExpr.Expression, runner, r, parenthesesExpr.Range())
} else if tupleConsExpr, ok := expr.(*hclsyntax.TupleConsExpr); ok {
if len(tupleConsExpr.Exprs) == 0 {
emitIssue(exprRange, runner, r)
}
}
}

func emitIssue(exprRange hcl.Range, runner *tflint.Runner, r *TerraformEmptyListEqualityRule) {
runner.EmitIssue(
r,
"Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
exprRange,
)
}
28 changes: 28 additions & 0 deletions rules/terraformrules/terraform_empty_list_equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ resource "aws_db_instance" "mysql" {
},
},
},
{
Name: "comparing with [] is not recommended (mixed with other conditions)",
Content: `
resource "aws_db_instance" "mysql" {
count = true == true || false != true && (false == false || [] == []) ? 1 : 0
instance_class = "m4.2xlarge"
}`,
Expected: tflint.Issues{
{
Rule: NewTerraformEmptyListEqualityRule(),
Message: "Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 3, Column: 62},
End: hcl.Pos{Line: 3, Column: 70},
},
},
{
Rule: NewTerraformEmptyListEqualityRule(),
Message: "Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 3, Column: 62},
End: hcl.Pos{Line: 3, Column: 70},
},
},
},
},
{
Name: "negatively comparing with [] is not recommended",
Content: `
Expand Down

0 comments on commit 2095371

Please sign in to comment.