Skip to content

Conversation

@bflad
Copy link
Contributor

@bflad bflad commented Feb 4, 2022

Reference: #865

Previously:

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) {

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) {
```
@bflad bflad added this to the v2.11.0 milestone Feb 4, 2022
@bflad bflad requested a review from a team as a code owner February 4, 2022 00:46
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines -162 to -166
if len(countActual) >= countExpected {
return false
}

return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I this is funny :)

Comment on lines -1240 to -1244
if !s.Primary.Equal(other.Primary) {
return false
}

return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one.

Comment on lines -96 to -102
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))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, to understand: here we are getting rid of a "double casting". Before it was going (for example) from interface{} to tftypes.AttributeName to string. Now, just interface{} to string. Correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction: it's not casting. You are removing the type assertion because the assertion was already done by the switch step.(type). So, it's safe to then do just the type conversion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the original issue reported in the linter:

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))))
                                                           ^

There is a fun feature in the Go specification that allows switch statements to automatically handle type assertions when using the .(type) syntax with assignment. It's really handy for simplifying code that needs to handle multiple types, if you haven't run across it before to avoid the duplicate type handling within a case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep yep: essentially, by the time we are in any of the specific cases of the switch, the assertion has already happened and we can avoid doing it again. Makes sense.

@bflad bflad merged commit 74c4eeb into main Feb 4, 2022
@bflad bflad deleted the bflad-enable-gosimple branch February 4, 2022 18:36
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants