-
Notifications
You must be signed in to change notification settings - Fork 242
Enable gosimple linter and fix issues #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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) { ```
detro
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| if len(countActual) >= countExpected { | ||
| return false | ||
| } | ||
|
|
||
| return true |
There was a problem hiding this comment.
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 :)
| if !s.Primary.Equal(other.Primary) { | ||
| return false | ||
| } | ||
|
|
||
| return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another one.
| 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)))) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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. |
Reference: #865
Previously: