diff --git a/.golangci.yml b/.golangci.yml index 2383f123ce4..d0fa7a415ad 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,7 +14,7 @@ linters: - errcheck - exportloopref - gofmt - # - gosimple # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865 + - gosimple # - ineffassign # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865 # - makezero # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865 - nilerr diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 32080d3222e..fb3bac2caff 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -126,7 +126,7 @@ func runSweepers(regions []string, sweepers map[string]*Sweeper, allowFailures b return sweeperRunList, fmt.Errorf("sweeper (%s) for region (%s) failed: %s", sweeper.Name, region, err) } } - elapsed := time.Now().Sub(start) + elapsed := time.Since(start) log.Printf("Completed Sweepers for region (%s) in %s", region, elapsed) log.Printf("Sweeper Tests for region (%s) ran successfully:\n", region) @@ -239,7 +239,7 @@ func runSweeperWithRegion(region string, s *Sweeper, sweepers map[string]*Sweepe start := time.Now() runE := s.F(region) - elapsed := time.Now().Sub(start) + elapsed := time.Since(start) log.Printf("[DEBUG] Completed Sweeper (%s) in region (%s) in %s", s.Name, region, elapsed) diff --git a/helper/schema/field_reader_map.go b/helper/schema/field_reader_map.go index 61a3fba5eb9..6fa19152274 100644 --- a/helper/schema/field_reader_map.go +++ b/helper/schema/field_reader_map.go @@ -159,11 +159,8 @@ func (r *MapFieldReader) readSet( // "ports.1", but the "state" map might have those plus "ports.2". // We don't want "ports.2" countActual[idx] = struct{}{} - if len(countActual) >= countExpected { - return false - } - return true + return len(countActual) < countExpected }) if !completed && err != nil { return FieldReadResult{}, err diff --git a/helper/schema/resource.go b/helper/schema/resource.go index aaa8ea96a40..1eb66d847e5 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -844,7 +844,7 @@ func validateResourceID(s *Schema) error { // ID should at least be computed. If unspecified it will be set to Computed and Optional, // but Optional is unnecessary if undesired. - if s.Computed != true { + if !s.Computed { return fmt.Errorf(`the "id" attribute must be marked Computed`) } return nil diff --git a/helper/schema/set.go b/helper/schema/set.go index 48755c22d1a..b937ab6e561 100644 --- a/helper/schema/set.go +++ b/helper/schema/set.go @@ -3,12 +3,12 @@ package schema import ( "bytes" "fmt" - "github.com/google/go-cmp/cmp" "reflect" "sort" "strconv" "sync" + "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/hashcode" ) @@ -238,6 +238,6 @@ func (s *Set) listCode() []string { for k := range s.m { keys = append(keys, k) } - sort.Sort(sort.StringSlice(keys)) + sort.Strings(keys) return keys } diff --git a/helper/validation/float_test.go b/helper/validation/float_test.go index f268747ae7e..b62d399d5ac 100644 --- a/helper/validation/float_test.go +++ b/helper/validation/float_test.go @@ -63,12 +63,12 @@ func TestValidateFloatAtLeast(t *testing.T) { { val: 1.5, f: FloatAtLeast(2.5), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\.5\\d*\\), got 1\\.5\\d*"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(2\.5\d*\), got 1\.5\d*`), }, { val: "2.5", f: FloatAtLeast(1.5), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be float`), }, }) } @@ -86,12 +86,12 @@ func TestValidateFloatAtMost(t *testing.T) { { val: 2.5, f: FloatAtMost(1.5), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(1\\.5\\d*\\), got 2\\.5\\d*"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(1\.5\d*\), got 2\.5\d*`), }, { val: "2.5", f: FloatAtMost(3.5), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be float`), }, }) } diff --git a/helper/validation/int_test.go b/helper/validation/int_test.go index 935da204b52..ed71c2d2c73 100644 --- a/helper/validation/int_test.go +++ b/helper/validation/int_test.go @@ -18,12 +18,12 @@ func TestValidationIntBetween(t *testing.T) { { val: 1, f: IntBetween(2, 3), - expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(2 - 3\\), got 1"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be in the range \(2 - 3\), got 1`), }, { val: "1", f: IntBetween(2, 3), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`), }, }) } @@ -41,12 +41,12 @@ func TestValidationIntAtLeast(t *testing.T) { { val: 1, f: IntAtLeast(2), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\), got 1"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(2\), got 1`), }, { val: "1", f: IntAtLeast(2), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`), }, }) } @@ -64,12 +64,12 @@ func TestValidationIntAtMost(t *testing.T) { { val: 1, f: IntAtMost(0), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(0\\), got 1"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(0\), got 1`), }, { val: "1", f: IntAtMost(0), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`), }, }) } @@ -119,12 +119,12 @@ func TestValidationIntInSlice(t *testing.T) { { val: 42, f: IntInSlice([]int{10, 20}), - expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[10 20\\], got 42"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[10 20\], got 42`), }, { val: "InvalidValue", f: IntInSlice([]int{10, 20}), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`), }, }) } diff --git a/helper/validation/meta_test.go b/helper/validation/meta_test.go index 398dcb0612d..5bbe013c69d 100644 --- a/helper/validation/meta_test.go +++ b/helper/validation/meta_test.go @@ -52,7 +52,7 @@ func TestValidationAll(t *testing.T) { StringLenBetween(5, 42), StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"), ), - expectedErr: regexp.MustCompile("expected length of [\\w]+ to be in the range \\(5 - 42\\), got foo"), + expectedErr: regexp.MustCompile(`expected length of [\w]+ to be in the range \(5 - 42\), got foo`), }, { val: "!!!!!", @@ -87,7 +87,7 @@ func TestValidationAny(t *testing.T) { IntAtLeast(42), IntAtMost(5), ), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(42\), got 7`), }, { val: 7, @@ -95,7 +95,7 @@ func TestValidationAny(t *testing.T) { IntAtLeast(42), IntAtMost(5), ), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(5\), got 7`), }, }) } @@ -122,7 +122,7 @@ func TestToDiagFunc(t *testing.T) { IntAtLeast(42), IntAtMost(5), )), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(42\), got 7`), }, { val: 7, @@ -130,7 +130,7 @@ func TestToDiagFunc(t *testing.T) { IntAtLeast(42), IntAtMost(5), )), - expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(5\), got 7`), }, }) } diff --git a/helper/validation/strings_test.go b/helper/validation/strings_test.go index f18c68aa931..61ae6b4e699 100644 --- a/helper/validation/strings_test.go +++ b/helper/validation/strings_test.go @@ -290,17 +290,17 @@ func TestValidationStringInSlice(t *testing.T) { { val: "VALIDVALUE", f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), - expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got VALIDVALUE"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[ValidValue AnotherValidValue\], got VALIDVALUE`), }, { val: "InvalidValue", f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), - expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got InvalidValue"), + expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[ValidValue AnotherValidValue\], got InvalidValue`), }, { val: 1, f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`), }, }) } @@ -319,18 +319,18 @@ func TestValidationStringNotInSlice(t *testing.T) { { val: "AnotherInvalidValue", f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false), - expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got AnotherInvalidValue"), + expectedErr: regexp.MustCompile(`expected [\w]+ to not be any of \[InvalidValue AnotherInvalidValue\], got AnotherInvalidValue`), }, // ignore case { val: "INVALIDVALUE", f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, true), - expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got INVALIDVALUE"), + expectedErr: regexp.MustCompile(`expected [\w]+ to not be any of \[InvalidValue AnotherInvalidValue\], got INVALIDVALUE`), }, { val: 1, f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false), - expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"), + expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`), }, }) } @@ -344,12 +344,12 @@ func TestValidationStringMatch(t *testing.T) { { val: "bar", f: StringMatch(regexp.MustCompile(".*foo.*"), ""), - expectedErr: regexp.MustCompile("expected value of [\\w]+ to match regular expression " + regexp.QuoteMeta(`".*foo.*"`)), + expectedErr: regexp.MustCompile(`expected value of [\w]+ to match regular expression ` + regexp.QuoteMeta(`".*foo.*"`)), }, { val: "bar", f: StringMatch(regexp.MustCompile(".*foo.*"), "value must contain foo"), - expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must contain foo\\)"), + expectedErr: regexp.MustCompile(`invalid value for [\w]+ \(value must contain foo\)`), }, }) } @@ -363,12 +363,12 @@ func TestValidationStringDoesNotMatch(t *testing.T) { { val: "bar", f: StringDoesNotMatch(regexp.MustCompile(".*bar.*"), ""), - expectedErr: regexp.MustCompile("expected value of [\\w]+ to not match regular expression " + regexp.QuoteMeta(`".*bar.*"`)), + expectedErr: regexp.MustCompile(`expected value of [\w]+ to not match regular expression ` + regexp.QuoteMeta(`".*bar.*"`)), }, { val: "bar", f: StringDoesNotMatch(regexp.MustCompile(".*bar.*"), "value must not contain foo"), - expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must not contain foo\\)"), + expectedErr: regexp.MustCompile(`invalid value for [\w]+ \(value must not contain foo\)`), }, }) } diff --git a/internal/addrs/module_instance.go b/internal/addrs/module_instance.go index f31d833d2a8..113d3d675ab 100644 --- a/internal/addrs/module_instance.go +++ b/internal/addrs/module_instance.go @@ -92,7 +92,6 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra "Invalid address operator", "Module address prefix must be followed by dot and then a name.", )) - break } if next != "module" { @@ -122,7 +121,6 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra "Invalid address operator", "Prefix \"module.\" must be followed by a module name.", )) - break } remain = remain[1:] step := ModuleInstanceStep{ diff --git a/internal/plugin/convert/diagnostics.go b/internal/plugin/convert/diagnostics.go index 252456747de..d0277227564 100644 --- a/internal/plugin/convert/diagnostics.go +++ b/internal/plugin/convert/diagnostics.go @@ -93,13 +93,13 @@ func AttributePathToPath(ap *tftypes.AttributePath) cty.Path { return p } for _, step := range ap.Steps() { - switch step.(type) { + switch step := step.(type) { case tftypes.AttributeName: - p = p.GetAttr(string(step.(tftypes.AttributeName))) + p = p.GetAttr(string(step)) case tftypes.ElementKeyString: - p = p.Index(cty.StringVal(string(step.(tftypes.ElementKeyString)))) + p = p.Index(cty.StringVal(string(step))) case tftypes.ElementKeyInt: - p = p.Index(cty.NumberIntVal(int64(step.(tftypes.ElementKeyInt)))) + p = p.Index(cty.NumberIntVal(int64(step))) } } return p diff --git a/terraform/state.go b/terraform/state.go index 6018e181ccd..7b195631920 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -1023,7 +1023,7 @@ func (m *ModuleState) String() string { } if len(rs.Dependencies) > 0 { - buf.WriteString(fmt.Sprintf("\n Dependencies:\n")) + buf.WriteString("\n Dependencies:\n") for _, dep := range rs.Dependencies { buf.WriteString(fmt.Sprintf(" %s\n", dep)) } @@ -1237,11 +1237,7 @@ func (s *ResourceState) Equal(other *ResourceState) bool { } // States must be equal - if !s.Primary.Equal(other.Primary) { - return false - } - - return true + return s.Primary.Equal(other.Primary) } // Taint marks a resource as tainted.