Add linter for checking with empty string (#4246)

Finding failing cases:

``` bash
tree-grepper -q go '((binary_expression (identifier) ["==" "!="] (interpreted_string_literal) @ri) (#eq? @ri "\"\""))'
```

Fixing failing cases:

``` bash
comby 'if :[1~[^ ]*] == ""' 'if len(:[1]) == 0' -matcher .go -in-place
comby 'if :[1~[^ ]*] != ""' 'if len(:[1]) > 0' -matcher .go -in-place
```

<!-- PR description-->

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* closes https://github.com/alcionai/corso/issues/3654

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Abin Simon 2023-09-15 12:15:00 +05:30 committed by GitHub
parent 04628c7f91
commit ce422f0b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 31 additions and 24 deletions

View File

@ -503,6 +503,13 @@ jobs:
echo "No trailing commas for function calls" echo "No trailing commas for function calls"
exit 1 exit 1
fi fi
- name: Check for empty string comparison
run: |
# Using `grep .` as the exit codes are always true for correct grammar
if tree-grepper -q go '((binary_expression (identifier) ["==" "!="] (interpreted_string_literal) @_ri) @exp (#eq? @_ri "\"\""))' | grep .; then
echo "Use len check instead of empty string comparison"
exit 1
fi
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
# --- GitHub Actions Linting ------------------------------------------------------------------------- # --- GitHub Actions Linting -------------------------------------------------------------------------

View File

@ -118,7 +118,7 @@ func InitFunc(cmd *cobra.Command, args []string) error {
// struct for testing. // struct for testing.
func initWithViper(vpr *viper.Viper, configFP string) error { func initWithViper(vpr *viper.Viper, configFP string) error {
// Configure default config file location // Configure default config file location
if configFP == "" || configFP == displayDefaultFP { if len(configFP) == 0 || configFP == displayDefaultFP {
// Find home directory. // Find home directory.
_, err := os.Stat(configDir) _, err := os.Stat(configDir)
if err != nil { if err != nil {

View File

@ -182,13 +182,13 @@ func printFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {
buf.WriteString(fmt.Sprintf("`--%s`", flag.Name)) buf.WriteString(fmt.Sprintf("`--%s`", flag.Name))
buf.WriteString("|") buf.WriteString("|")
if flag.Shorthand != "" && flag.ShorthandDeprecated == "" { if len(flag.Shorthand) > 0 && flag.ShorthandDeprecated == "" {
buf.WriteString(fmt.Sprintf("`-%s`", flag.Shorthand)) buf.WriteString(fmt.Sprintf("`-%s`", flag.Shorthand))
} }
buf.WriteString("|") buf.WriteString("|")
if flag.DefValue != "" { if len(flag.DefValue) > 0 {
defValue := flag.DefValue defValue := flag.DefValue
if defValue == "[]" { if defValue == "[]" {
defValue = "" defValue = ""

View File

@ -48,11 +48,11 @@ func (p Permission) Equals(other Permission) bool {
// have EntityID/Email as we backup permissions for all the // have EntityID/Email as we backup permissions for all the
// parents and children when we have a change in permissions. // parents and children when we have a change in permissions.
// We cannot just compare id because of the problem described in #3117 // We cannot just compare id because of the problem described in #3117
if p.EntityID != "" && p.EntityID != other.EntityID { if len(p.EntityID) > 0 && p.EntityID != other.EntityID {
return false return false
} }
if p.Email != "" && p.Email != other.Email { if len(p.Email) > 0 && p.Email != other.Email {
return false return false
} }
@ -165,7 +165,7 @@ func FilterPermissions(ctx context.Context, perms []models.Permissionable) []Per
// Technically GrantedToV2 can also contain devices, but the // Technically GrantedToV2 can also contain devices, but the
// documentation does not mention about devices in permissions // documentation does not mention about devices in permissions
if entityID == "" { if len(entityID) == 0 {
// This should ideally not be hit // This should ideally not be hit
continue continue
} }
@ -204,7 +204,7 @@ func FilterLinkShares(ctx context.Context, perms []models.Permissionable) []Link
// Technically GrantedToV2 can also contain devices, but the // Technically GrantedToV2 can also contain devices, but the
// documentation does not mention about devices in permissions // documentation does not mention about devices in permissions
if entityID == "" { if len(entityID) == 0 {
// This should ideally not be hit // This should ideally not be hit
continue continue
} }

View File

@ -73,7 +73,7 @@ func NewBetaClient(requestAdapter abstractions.RequestAdapter) *BetaClient {
return kform.NewFormParseNodeFactory() return kform.NewFormParseNodeFactory()
}) })
if m.requestAdapter.GetBaseUrl() == "" { if len(m.requestAdapter.GetBaseUrl()) == 0 {
m.requestAdapter.SetBaseUrl("https://graph.microsoft.com/beta") m.requestAdapter.SetBaseUrl("https://graph.microsoft.com/beta")
} }
return m return m
@ -85,7 +85,7 @@ func (m *BetaClient) SitesById(id string) *i1a3c1a5501c5e41b7fd169f2d4c768dce9b0
for idx, item := range m.pathParameters { for idx, item := range m.pathParameters {
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["site%2Did"] = id urlTplParams["site%2Did"] = id
} }
return i1a3c1a5501c5e41b7fd169f2d4c768dce9b096ac28fb5431bf02afcc57295411.NewSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter) return i1a3c1a5501c5e41b7fd169f2d4c768dce9b096ac28fb5431bf02afcc57295411.NewSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter)

View File

@ -75,7 +75,7 @@ func (m *ItemPagesItemCanvasLayoutHorizontalSectionsHorizontalSectionItemRequest
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["horizontalSectionColumn%2Did"] = id urlTplParams["horizontalSectionColumn%2Did"] = id
} }
//nolint:lll //nolint:lll

View File

@ -239,7 +239,7 @@ func (m *ItemPagesItemCanvasLayoutHorizontalSectionsItemColumnsHorizontalSection
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["webPart%2Did"] = id urlTplParams["webPart%2Did"] = id
} }

View File

@ -194,7 +194,7 @@ func (m *ItemPagesItemCanvasLayoutRequestBuilder) HorizontalSectionsById(id stri
for idx, item := range m.pathParameters { for idx, item := range m.pathParameters {
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["horizontalSection%2Did"] = id urlTplParams["horizontalSection%2Did"] = id
} }
return NewItemPagesItemCanvasLayoutHorizontalSectionsHorizontalSectionItemRequestBuilderInternal(urlTplParams, m.requestAdapter) return NewItemPagesItemCanvasLayoutHorizontalSectionsHorizontalSectionItemRequestBuilderInternal(urlTplParams, m.requestAdapter)

View File

@ -219,7 +219,7 @@ func (m *ItemPagesItemCanvasLayoutVerticalSectionRequestBuilder) WebpartsById(id
for idx, item := range m.pathParameters { for idx, item := range m.pathParameters {
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["webPart%2Did"] = id urlTplParams["webPart%2Did"] = id
} }
return NewItemPagesItemCanvasLayoutVerticalSectionWebpartsWebPartItemRequestBuilderInternal(urlTplParams, m.requestAdapter) return NewItemPagesItemCanvasLayoutVerticalSectionWebpartsWebPartItemRequestBuilderInternal(urlTplParams, m.requestAdapter)

View File

@ -230,7 +230,7 @@ func (m *ItemPagesSitePageItemRequestBuilder) WebPartsById(id string) *ItemPages
for idx, item := range m.pathParameters { for idx, item := range m.pathParameters {
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["webPart%2Did"] = id urlTplParams["webPart%2Did"] = id
} }
return NewItemPagesItemWebPartsWebPartItemRequestBuilderInternal(urlTplParams, m.requestAdapter) return NewItemPagesItemWebPartsWebPartItemRequestBuilderInternal(urlTplParams, m.requestAdapter)

View File

@ -196,7 +196,7 @@ func (m *SiteItemRequestBuilder) PagesById(id string) *ItemPagesSitePageItemRequ
for idx, item := range m.pathParameters { for idx, item := range m.pathParameters {
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["sitePage%2Did"] = id urlTplParams["sitePage%2Did"] = id
} }
return NewItemPagesSitePageItemRequestBuilderInternal(urlTplParams, m.requestAdapter) return NewItemPagesSitePageItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
@ -240,7 +240,7 @@ func (m *SiteItemRequestBuilder) SitesById(id string) *ItemSitesSiteItemRequestB
for idx, item := range m.pathParameters { for idx, item := range m.pathParameters {
urlTplParams[idx] = item urlTplParams[idx] = item
} }
if id != "" { if len(id) > 0 {
urlTplParams["site%2Did1"] = id urlTplParams["site%2Did1"] = id
} }
return NewItemSitesSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter) return NewItemSitesSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter)

View File

@ -357,7 +357,7 @@ func (mw RetryMiddleware) getRetryDelay(
retryAfter = resp.Header.Get(retryAfterHeader) retryAfter = resp.Header.Get(retryAfterHeader)
} }
if retryAfter != "" { if len(retryAfter) > 0 {
retryAfterDelay, err := strconv.ParseFloat(retryAfter, 64) retryAfterDelay, err := strconv.ParseFloat(retryAfter, 64)
if err == nil { if err == nil {
return time.Duration(retryAfterDelay) * time.Second return time.Duration(retryAfterDelay) * time.Second

View File

@ -91,7 +91,7 @@ func (iw *largeItemWriter) Write(p []byte) (int, error) {
// https://outlook.office.com/api/v2.0/Users('<user-id>')/Messages('<message-id>')/Attachments('<attachment-id>') // https://outlook.office.com/api/v2.0/Users('<user-id>')/Messages('<message-id>')/Attachments('<attachment-id>')
// Ref: https://learn.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http // Ref: https://learn.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http
loc := resp.Header.Get("Location") loc := resp.Header.Get("Location")
if loc != "" { if len(loc) > 0 {
splits := strings.Split(loc, "'") splits := strings.Split(loc, "'")
if len(splits) != 7 || splits[4] != ")/Attachments(" || len(splits[5]) == 0 { if len(splits) != 7 || splits[4] != ")/Attachments(" || len(splits[5]) == 0 {
return 0, clues.New("invalid format for upload completion url"). return 0, clues.New("invalid format for upload completion url").

View File

@ -315,7 +315,7 @@ func (suite *ExportUnitSuite) TestZipExports() {
Body: item.Body, Body: item.Body,
} }
if col.BasePath() != "" { if len(col.BasePath()) > 0 {
expected.Name = strings.Join([]string{col.BasePath(), item.Name}, "/") expected.Name = strings.Join([]string{col.BasePath(), item.Name}, "/")
} }

View File

@ -36,7 +36,7 @@ func LogTimeOfTest(t TestT) string {
now := time.Now().UTC().Format(time.RFC3339Nano) now := time.Now().UTC().Format(time.RFC3339Nano)
name := t.Name() name := t.Name()
if name == "" { if len(name) == 0 {
t.Logf("Test run at %s.", now) t.Logf("Test run at %s.", now)
return now return now
} }

View File

@ -24,7 +24,7 @@ func NewGroupsLocationIDer(
pb := path.Builder{}.Append(category.String()) pb := path.Builder{}.Append(category.String())
prefixElems := 1 prefixElems := 1
if driveID != "" { // non sp paths don't have driveID if len(driveID) > 0 { // non sp paths don't have driveID
pb = pb.Append(driveID) pb = pb.Append(driveID)
prefixElems = 2 prefixElems = 2

View File

@ -9,7 +9,7 @@ import (
) )
func init() { func init() {
if os.Getenv("XDG_CACHE_HOME") != "" { if len(os.Getenv("XDG_CACHE_HOME")) > 0 {
userLogsDir = os.Getenv("XDG_CACHE_HOME") userLogsDir = os.Getenv("XDG_CACHE_HOME")
} else { } else {
userLogsDir = filepath.Join(os.Getenv("HOME"), ".cache") userLogsDir = filepath.Join(os.Getenv("HOME"), ".cache")

View File

@ -91,7 +91,7 @@ type testIDsPager struct {
func (p *testIDsPager) GetPage( func (p *testIDsPager) GetPage(
ctx context.Context, ctx context.Context,
) (NextLinkValuer[any], error) { ) (NextLinkValuer[any], error) {
if p.errorCode != "" { if len(p.errorCode) > 0 {
ierr := odataerrors.NewMainError() ierr := odataerrors.NewMainError()
ierr.SetCode(&p.errorCode) ierr.SetCode(&p.errorCode)
@ -145,7 +145,7 @@ type testIDsDeltaPager struct {
func (p *testIDsDeltaPager) GetPage( func (p *testIDsDeltaPager) GetPage(
ctx context.Context, ctx context.Context,
) (DeltaLinkValuer[any], error) { ) (DeltaLinkValuer[any], error) {
if p.errorCode != "" { if len(p.errorCode) > 0 {
ierr := odataerrors.NewMainError() ierr := odataerrors.NewMainError()
ierr.SetCode(&p.errorCode) ierr.SetCode(&p.errorCode)