Skip to content

Commit 74c4eeb

Browse files
authored
Enable gosimple linter and fix issues (#880)
Reference: #865 Previously: ```text helper/validation/float_test.go:66:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\.5\\d*\\), got 1\\.5\\d*"), ^ helper/validation/float_test.go:71:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"), ^ helper/validation/float_test.go:89:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(1\\.5\\d*\\), got 2\\.5\\d*"), ^ helper/validation/float_test.go:94:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"), ^ helper/validation/int_test.go:21:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(2 - 3\\), got 1"), ^ helper/validation/int_test.go:26:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), ^ helper/validation/int_test.go:44:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\), got 1"), ^ helper/validation/int_test.go:49:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), ^ helper/validation/int_test.go:67:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(0\\), got 1"), ^ helper/validation/int_test.go:72:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), ^ helper/validation/int_test.go:122:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[10 20\\], got 42"), ^ helper/validation/int_test.go:127:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"), ^ helper/validation/meta_test.go:55:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected length of [\\w]+ to be in the range \\(5 - 42\\), got foo"), ^ helper/validation/meta_test.go:90:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"), ^ helper/validation/meta_test.go:98:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"), ^ helper/validation/meta_test.go:125:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"), ^ helper/validation/meta_test.go:133:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"), ^ helper/validation/strings_test.go:293:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got VALIDVALUE"), ^ helper/validation/strings_test.go:298:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got InvalidValue"), ^ helper/validation/strings_test.go:303:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"), ^ helper/validation/strings_test.go:322:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got AnotherInvalidValue"), ^ helper/validation/strings_test.go:328:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got INVALIDVALUE"), ^ helper/validation/strings_test.go:333:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"), ^ helper/validation/strings_test.go:352:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must contain foo\\)"), ^ helper/validation/strings_test.go:371:17: S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple) expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must not contain foo\\)"), ^ internal/plugin/convert/diagnostics.go:96:10: S1034: assigning the result of this type assertion to a variable (switch step := step.(type)) could eliminate type assertions in switch cases (gosimple) switch step.(type) { ^ internal/plugin/convert/diagnostics.go:98:25: S1034(related information): could eliminate this type assertion (gosimple) p = p.GetAttr(string(step.(tftypes.AttributeName))) ^ internal/plugin/convert/diagnostics.go:100:37: S1034(related information): could eliminate this type assertion (gosimple) p = p.Index(cty.StringVal(string(step.(tftypes.ElementKeyString)))) ^ internal/plugin/convert/diagnostics.go:102:39: S1034(related information): could eliminate this type assertion (gosimple) p = p.Index(cty.NumberIntVal(int64(step.(tftypes.ElementKeyInt)))) ^ internal/addrs/module_instance.go:95:4: S1023: redundant break statement (gosimple) break ^ internal/addrs/module_instance.go:125:4: S1023: redundant break statement (gosimple) break ^ helper/resource/testing.go:129:14: S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple) elapsed := time.Now().Sub(start) ^ helper/resource/testing.go:242:13: S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple) elapsed := time.Now().Sub(start) ^ helper/schema/set.go:241:2: S1032: should use sort.Strings(...) instead of sort.Sort(sort.StringSlice(...)) (gosimple) sort.Sort(sort.StringSlice(keys)) ^ helper/schema/resource.go:847:5: S1002: should omit comparison to bool constant, can be simplified to `!s.Computed` (gosimple) if s.Computed != true { ^ helper/schema/field_reader_map.go:162:3: S1008: should use 'return len(countActual) < countExpected' instead of 'if len(countActual) >= countExpected { return false }; return true' (gosimple) if len(countActual) >= countExpected { ^ terraform/state.go:1026:20: S1039: unnecessary use of fmt.Sprintf (gosimple) buf.WriteString(fmt.Sprintf("\n Dependencies:\n")) ^ terraform/state.go:1240:2: S1008: should use 'return s.Primary.Equal(other.Primary)' instead of 'if !s.Primary.Equal(other.Primary) { return false }; return true' (gosimple) if !s.Primary.Equal(other.Primary) { ```
1 parent 9e15c51 commit 74c4eeb

File tree

12 files changed

+40
-49
lines changed

12 files changed

+40
-49
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ linters:
1414
- errcheck
1515
- exportloopref
1616
- gofmt
17-
# - gosimple # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865
17+
- gosimple
1818
# - ineffassign # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865
1919
# - makezero # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865
2020
- nilerr

helper/resource/testing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func runSweepers(regions []string, sweepers map[string]*Sweeper, allowFailures b
126126
return sweeperRunList, fmt.Errorf("sweeper (%s) for region (%s) failed: %s", sweeper.Name, region, err)
127127
}
128128
}
129-
elapsed := time.Now().Sub(start)
129+
elapsed := time.Since(start)
130130
log.Printf("Completed Sweepers for region (%s) in %s", region, elapsed)
131131

132132
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
239239

240240
start := time.Now()
241241
runE := s.F(region)
242-
elapsed := time.Now().Sub(start)
242+
elapsed := time.Since(start)
243243

244244
log.Printf("[DEBUG] Completed Sweeper (%s) in region (%s) in %s", s.Name, region, elapsed)
245245

helper/schema/field_reader_map.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,8 @@ func (r *MapFieldReader) readSet(
159159
// "ports.1", but the "state" map might have those plus "ports.2".
160160
// We don't want "ports.2"
161161
countActual[idx] = struct{}{}
162-
if len(countActual) >= countExpected {
163-
return false
164-
}
165162

166-
return true
163+
return len(countActual) < countExpected
167164
})
168165
if !completed && err != nil {
169166
return FieldReadResult{}, err

helper/schema/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ func validateResourceID(s *Schema) error {
844844

845845
// ID should at least be computed. If unspecified it will be set to Computed and Optional,
846846
// but Optional is unnecessary if undesired.
847-
if s.Computed != true {
847+
if !s.Computed {
848848
return fmt.Errorf(`the "id" attribute must be marked Computed`)
849849
}
850850
return nil

helper/schema/set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package schema
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/google/go-cmp/cmp"
76
"reflect"
87
"sort"
98
"strconv"
109
"sync"
1110

11+
"github.com/google/go-cmp/cmp"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/helper/hashcode"
1313
)
1414

@@ -238,6 +238,6 @@ func (s *Set) listCode() []string {
238238
for k := range s.m {
239239
keys = append(keys, k)
240240
}
241-
sort.Sort(sort.StringSlice(keys))
241+
sort.Strings(keys)
242242
return keys
243243
}

helper/validation/float_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ func TestValidateFloatAtLeast(t *testing.T) {
6363
{
6464
val: 1.5,
6565
f: FloatAtLeast(2.5),
66-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\.5\\d*\\), got 1\\.5\\d*"),
66+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(2\.5\d*\), got 1\.5\d*`),
6767
},
6868
{
6969
val: "2.5",
7070
f: FloatAtLeast(1.5),
71-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"),
71+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be float`),
7272
},
7373
})
7474
}
@@ -86,12 +86,12 @@ func TestValidateFloatAtMost(t *testing.T) {
8686
{
8787
val: 2.5,
8888
f: FloatAtMost(1.5),
89-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(1\\.5\\d*\\), got 2\\.5\\d*"),
89+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(1\.5\d*\), got 2\.5\d*`),
9090
},
9191
{
9292
val: "2.5",
9393
f: FloatAtMost(3.5),
94-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"),
94+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be float`),
9595
},
9696
})
9797
}

helper/validation/int_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func TestValidationIntBetween(t *testing.T) {
1818
{
1919
val: 1,
2020
f: IntBetween(2, 3),
21-
expectedErr: regexp.MustCompile("expected [\\w]+ to be in the range \\(2 - 3\\), got 1"),
21+
expectedErr: regexp.MustCompile(`expected [\w]+ to be in the range \(2 - 3\), got 1`),
2222
},
2323
{
2424
val: "1",
2525
f: IntBetween(2, 3),
26-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
26+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`),
2727
},
2828
})
2929
}
@@ -41,12 +41,12 @@ func TestValidationIntAtLeast(t *testing.T) {
4141
{
4242
val: 1,
4343
f: IntAtLeast(2),
44-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\), got 1"),
44+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(2\), got 1`),
4545
},
4646
{
4747
val: "1",
4848
f: IntAtLeast(2),
49-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
49+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`),
5050
},
5151
})
5252
}
@@ -64,12 +64,12 @@ func TestValidationIntAtMost(t *testing.T) {
6464
{
6565
val: 1,
6666
f: IntAtMost(0),
67-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(0\\), got 1"),
67+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(0\), got 1`),
6868
},
6969
{
7070
val: "1",
7171
f: IntAtMost(0),
72-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
72+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`),
7373
},
7474
})
7575
}
@@ -119,12 +119,12 @@ func TestValidationIntInSlice(t *testing.T) {
119119
{
120120
val: 42,
121121
f: IntInSlice([]int{10, 20}),
122-
expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[10 20\\], got 42"),
122+
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[10 20\], got 42`),
123123
},
124124
{
125125
val: "InvalidValue",
126126
f: IntInSlice([]int{10, 20}),
127-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be integer"),
127+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be integer`),
128128
},
129129
})
130130
}

helper/validation/meta_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestValidationAll(t *testing.T) {
5252
StringLenBetween(5, 42),
5353
StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
5454
),
55-
expectedErr: regexp.MustCompile("expected length of [\\w]+ to be in the range \\(5 - 42\\), got foo"),
55+
expectedErr: regexp.MustCompile(`expected length of [\w]+ to be in the range \(5 - 42\), got foo`),
5656
},
5757
{
5858
val: "!!!!!",
@@ -87,15 +87,15 @@ func TestValidationAny(t *testing.T) {
8787
IntAtLeast(42),
8888
IntAtMost(5),
8989
),
90-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"),
90+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(42\), got 7`),
9191
},
9292
{
9393
val: 7,
9494
f: Any(
9595
IntAtLeast(42),
9696
IntAtMost(5),
9797
),
98-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"),
98+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(5\), got 7`),
9999
},
100100
})
101101
}
@@ -122,15 +122,15 @@ func TestToDiagFunc(t *testing.T) {
122122
IntAtLeast(42),
123123
IntAtMost(5),
124124
)),
125-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"),
125+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at least \(42\), got 7`),
126126
},
127127
{
128128
val: 7,
129129
f: ToDiagFunc(Any(
130130
IntAtLeast(42),
131131
IntAtMost(5),
132132
)),
133-
expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"),
133+
expectedErr: regexp.MustCompile(`expected [\w]+ to be at most \(5\), got 7`),
134134
},
135135
})
136136
}

helper/validation/strings_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,17 @@ func TestValidationStringInSlice(t *testing.T) {
290290
{
291291
val: "VALIDVALUE",
292292
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
293-
expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got VALIDVALUE"),
293+
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[ValidValue AnotherValidValue\], got VALIDVALUE`),
294294
},
295295
{
296296
val: "InvalidValue",
297297
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
298-
expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[ValidValue AnotherValidValue\\], got InvalidValue"),
298+
expectedErr: regexp.MustCompile(`expected [\w]+ to be one of \[ValidValue AnotherValidValue\], got InvalidValue`),
299299
},
300300
{
301301
val: 1,
302302
f: StringInSlice([]string{"ValidValue", "AnotherValidValue"}, false),
303-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
303+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
304304
},
305305
})
306306
}
@@ -319,18 +319,18 @@ func TestValidationStringNotInSlice(t *testing.T) {
319319
{
320320
val: "AnotherInvalidValue",
321321
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
322-
expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got AnotherInvalidValue"),
322+
expectedErr: regexp.MustCompile(`expected [\w]+ to not be any of \[InvalidValue AnotherInvalidValue\], got AnotherInvalidValue`),
323323
},
324324
// ignore case
325325
{
326326
val: "INVALIDVALUE",
327327
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, true),
328-
expectedErr: regexp.MustCompile("expected [\\w]+ to not be any of \\[InvalidValue AnotherInvalidValue\\], got INVALIDVALUE"),
328+
expectedErr: regexp.MustCompile(`expected [\w]+ to not be any of \[InvalidValue AnotherInvalidValue\], got INVALIDVALUE`),
329329
},
330330
{
331331
val: 1,
332332
f: StringNotInSlice([]string{"InvalidValue", "AnotherInvalidValue"}, false),
333-
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be string"),
333+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
334334
},
335335
})
336336
}
@@ -344,12 +344,12 @@ func TestValidationStringMatch(t *testing.T) {
344344
{
345345
val: "bar",
346346
f: StringMatch(regexp.MustCompile(".*foo.*"), ""),
347-
expectedErr: regexp.MustCompile("expected value of [\\w]+ to match regular expression " + regexp.QuoteMeta(`".*foo.*"`)),
347+
expectedErr: regexp.MustCompile(`expected value of [\w]+ to match regular expression ` + regexp.QuoteMeta(`".*foo.*"`)),
348348
},
349349
{
350350
val: "bar",
351351
f: StringMatch(regexp.MustCompile(".*foo.*"), "value must contain foo"),
352-
expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must contain foo\\)"),
352+
expectedErr: regexp.MustCompile(`invalid value for [\w]+ \(value must contain foo\)`),
353353
},
354354
})
355355
}
@@ -363,12 +363,12 @@ func TestValidationStringDoesNotMatch(t *testing.T) {
363363
{
364364
val: "bar",
365365
f: StringDoesNotMatch(regexp.MustCompile(".*bar.*"), ""),
366-
expectedErr: regexp.MustCompile("expected value of [\\w]+ to not match regular expression " + regexp.QuoteMeta(`".*bar.*"`)),
366+
expectedErr: regexp.MustCompile(`expected value of [\w]+ to not match regular expression ` + regexp.QuoteMeta(`".*bar.*"`)),
367367
},
368368
{
369369
val: "bar",
370370
f: StringDoesNotMatch(regexp.MustCompile(".*bar.*"), "value must not contain foo"),
371-
expectedErr: regexp.MustCompile("invalid value for [\\w]+ \\(value must not contain foo\\)"),
371+
expectedErr: regexp.MustCompile(`invalid value for [\w]+ \(value must not contain foo\)`),
372372
},
373373
})
374374
}

internal/addrs/module_instance.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra
9292
"Invalid address operator",
9393
"Module address prefix must be followed by dot and then a name.",
9494
))
95-
break
9695
}
9796

9897
if next != "module" {
@@ -122,7 +121,6 @@ func parseModuleInstancePrefix(traversal hcl.Traversal) (ModuleInstance, hcl.Tra
122121
"Invalid address operator",
123122
"Prefix \"module.\" must be followed by a module name.",
124123
))
125-
break
126124
}
127125
remain = remain[1:]
128126
step := ModuleInstanceStep{

0 commit comments

Comments
 (0)