From 02b1bd8bc6d5d783b0cad225881e123375808e02 Mon Sep 17 00:00:00 2001 From: Bruno Ferreira Date: Tue, 9 Aug 2022 02:27:40 +0100 Subject: [PATCH] fix: remove uneeded comparison with condition and simplify with new WalkExpressions algorithm --- .../terraform_empty_list_equality.go | 45 ++++++------------- .../terraform_empty_list_equality_test.go | 8 ++-- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/rules/terraformrules/terraform_empty_list_equality.go b/rules/terraformrules/terraform_empty_list_equality.go index a97545c19..25baf37a3 100644 --- a/rules/terraformrules/terraform_empty_list_equality.go +++ b/rules/terraformrules/terraform_empty_list_equality.go @@ -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 @@ -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, + ) } diff --git a/rules/terraformrules/terraform_empty_list_equality_test.go b/rules/terraformrules/terraform_empty_list_equality_test.go index 8812542f3..484fa4209 100644 --- a/rules/terraformrules/terraform_empty_list_equality_test.go +++ b/rules/terraformrules/terraform_empty_list_equality_test.go @@ -45,8 +45,8 @@ 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}, }, }, { @@ -54,8 +54,8 @@ 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: 10}, - End: hcl.Pos{Line: 3, Column: 18}, + Start: hcl.Pos{Line: 3, Column: 22}, + End: hcl.Pos{Line: 3, Column: 30}, }, }, },