Skip to content

Commit

Permalink
fix: remove uneeded comparison with condition and simplify with new W…
Browse files Browse the repository at this point in the history
…alkExpressions algorithm
  • Loading branch information
bmbferreira committed Aug 9, 2022
1 parent 8d2cf27 commit 02b1bd8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
45 changes: 14 additions & 31 deletions rules/terraformrules/terraform_empty_list_equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/terraform-linters/tflint/tflint"
"github.com/zclconf/go-cty/cty"
)

// TerraformEmptyListEqualityRule checks whether is there a comparison with an empty list
Expand Down Expand Up @@ -55,40 +56,22 @@ func (r *TerraformEmptyListEqualityRule) Check(runner *tflint.Runner) error {
// checkEmptyList visits all blocks that can contain expressions and checks for comparisons with static empty list
func (r *TerraformEmptyListEqualityRule) checkEmptyList(runner *tflint.Runner) error {
return runner.WalkExpressions(func(expr hcl.Expression) error {
if conditionalExpr, ok := expr.(*hclsyntax.ConditionalExpr); ok {
var issuesRangeSet = make(map[hcl.Range]struct{})
r.searchEmptyList(conditionalExpr.Condition, runner, conditionalExpr.Range(), issuesRangeSet)
r.emitEmptyListEqualityIssues(issuesRangeSet, runner)
if binaryOpExpr, ok := expr.(*hclsyntax.BinaryOpExpr); ok && binaryOpExpr.Op.Type == cty.Bool {
if tupleConsExpr, ok := binaryOpExpr.LHS.(*hclsyntax.TupleConsExpr); ok && len(tupleConsExpr.Exprs) == 0 {
r.emitEmptyListEqualityIssue(binaryOpExpr.Range(), runner)
} else if tupleConsExpr, ok := binaryOpExpr.RHS.(*hclsyntax.TupleConsExpr); ok && len(tupleConsExpr.Exprs) == 0 {
r.emitEmptyListEqualityIssue(binaryOpExpr.Range(), runner)
}
}
return nil
})
}

// searchEmptyList Searches for comparisons with static empty list in the given expression
func (r *TerraformEmptyListEqualityRule) searchEmptyList(expr hcl.Expression, runner *tflint.Runner, exprRange hcl.Range, issuesRangeSet map[hcl.Range]struct{}) {
if binaryOpExpr, ok := expr.(*hclsyntax.BinaryOpExpr); ok {
if binaryOpExpr.Op.Type.FriendlyName() == "bool" {
r.searchEmptyList(binaryOpExpr.RHS, runner, binaryOpExpr.Range(), issuesRangeSet)
r.searchEmptyList(binaryOpExpr.LHS, runner, binaryOpExpr.Range(), issuesRangeSet)
}
} else if binaryOpExpr, ok := expr.(*hclsyntax.BinaryOpExpr); ok {
r.searchEmptyList(binaryOpExpr, runner, binaryOpExpr.Range(), issuesRangeSet)
} else if parenthesesExpr, ok := expr.(*hclsyntax.ParenthesesExpr); ok {
r.searchEmptyList(parenthesesExpr.Expression, runner, parenthesesExpr.Range(), issuesRangeSet)
} else if tupleConsExpr, ok := expr.(*hclsyntax.TupleConsExpr); ok {
if len(tupleConsExpr.Exprs) == 0 {
issuesRangeSet[exprRange] = struct{}{}
}
}
}

// emitEmptyListEqualityIssues emits issues for each found comparison with static empty list
func (r *TerraformEmptyListEqualityRule) emitEmptyListEqualityIssues(exprRanges map[hcl.Range]struct{}, runner *tflint.Runner) {
for exprRange := range exprRanges {
runner.EmitIssue(
r,
"Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
exprRange,
)
}
// emitEmptyListEqualityIssue emits issue for comparison with static empty list
func (r *TerraformEmptyListEqualityRule) emitEmptyListEqualityIssue(exprRange hcl.Range, runner *tflint.Runner) {
runner.EmitIssue(
r,
"Comparing a collection with an empty list is invalid. To detect an empty collection, check its length.",
exprRange,
)
}
8 changes: 4 additions & 4 deletions rules/terraformrules/terraform_empty_list_equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ resource "aws_db_instance" "mysql" {
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: 22},
End: hcl.Pos{Line: 3, Column: 30},
Start: hcl.Pos{Line: 3, Column: 10},
End: hcl.Pos{Line: 3, Column: 18},
},
},
{
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: 10},
End: hcl.Pos{Line: 3, Column: 18},
Start: hcl.Pos{Line: 3, Column: 22},
End: hcl.Pos{Line: 3, Column: 30},
},
},
},
Expand Down

0 comments on commit 02b1bd8

Please sign in to comment.