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:
parent
04628c7f91
commit
ce422f0b1d
7
.github/workflows/ci.yml
vendored
7
.github/workflows/ci.yml
vendored
@ -503,6 +503,13 @@ jobs:
|
||||
echo "No trailing commas for function calls"
|
||||
exit 1
|
||||
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 -------------------------------------------------------------------------
|
||||
|
||||
@ -118,7 +118,7 @@ func InitFunc(cmd *cobra.Command, args []string) error {
|
||||
// struct for testing.
|
||||
func initWithViper(vpr *viper.Viper, configFP string) error {
|
||||
// Configure default config file location
|
||||
if configFP == "" || configFP == displayDefaultFP {
|
||||
if len(configFP) == 0 || configFP == displayDefaultFP {
|
||||
// Find home directory.
|
||||
_, err := os.Stat(configDir)
|
||||
if err != nil {
|
||||
|
||||
@ -182,13 +182,13 @@ func printFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {
|
||||
buf.WriteString(fmt.Sprintf("`--%s`", flag.Name))
|
||||
buf.WriteString("|")
|
||||
|
||||
if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
|
||||
if len(flag.Shorthand) > 0 && flag.ShorthandDeprecated == "" {
|
||||
buf.WriteString(fmt.Sprintf("`-%s`", flag.Shorthand))
|
||||
}
|
||||
|
||||
buf.WriteString("|")
|
||||
|
||||
if flag.DefValue != "" {
|
||||
if len(flag.DefValue) > 0 {
|
||||
defValue := flag.DefValue
|
||||
if defValue == "[]" {
|
||||
defValue = ""
|
||||
|
||||
@ -48,11 +48,11 @@ func (p Permission) Equals(other Permission) bool {
|
||||
// have EntityID/Email as we backup permissions for all the
|
||||
// parents and children when we have a change in permissions.
|
||||
// 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
|
||||
}
|
||||
|
||||
if p.Email != "" && p.Email != other.Email {
|
||||
if len(p.Email) > 0 && p.Email != other.Email {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ func FilterPermissions(ctx context.Context, perms []models.Permissionable) []Per
|
||||
|
||||
// Technically GrantedToV2 can also contain devices, but the
|
||||
// documentation does not mention about devices in permissions
|
||||
if entityID == "" {
|
||||
if len(entityID) == 0 {
|
||||
// This should ideally not be hit
|
||||
continue
|
||||
}
|
||||
@ -204,7 +204,7 @@ func FilterLinkShares(ctx context.Context, perms []models.Permissionable) []Link
|
||||
|
||||
// Technically GrantedToV2 can also contain devices, but the
|
||||
// documentation does not mention about devices in permissions
|
||||
if entityID == "" {
|
||||
if len(entityID) == 0 {
|
||||
// This should ideally not be hit
|
||||
continue
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ func NewBetaClient(requestAdapter abstractions.RequestAdapter) *BetaClient {
|
||||
return kform.NewFormParseNodeFactory()
|
||||
})
|
||||
|
||||
if m.requestAdapter.GetBaseUrl() == "" {
|
||||
if len(m.requestAdapter.GetBaseUrl()) == 0 {
|
||||
m.requestAdapter.SetBaseUrl("https://graph.microsoft.com/beta")
|
||||
}
|
||||
return m
|
||||
@ -85,7 +85,7 @@ func (m *BetaClient) SitesById(id string) *i1a3c1a5501c5e41b7fd169f2d4c768dce9b0
|
||||
for idx, item := range m.pathParameters {
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["site%2Did"] = id
|
||||
}
|
||||
return i1a3c1a5501c5e41b7fd169f2d4c768dce9b096ac28fb5431bf02afcc57295411.NewSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
|
||||
|
||||
@ -75,7 +75,7 @@ func (m *ItemPagesItemCanvasLayoutHorizontalSectionsHorizontalSectionItemRequest
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["horizontalSectionColumn%2Did"] = id
|
||||
}
|
||||
//nolint:lll
|
||||
|
||||
@ -239,7 +239,7 @@ func (m *ItemPagesItemCanvasLayoutHorizontalSectionsItemColumnsHorizontalSection
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["webPart%2Did"] = id
|
||||
}
|
||||
|
||||
|
||||
@ -194,7 +194,7 @@ func (m *ItemPagesItemCanvasLayoutRequestBuilder) HorizontalSectionsById(id stri
|
||||
for idx, item := range m.pathParameters {
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["horizontalSection%2Did"] = id
|
||||
}
|
||||
return NewItemPagesItemCanvasLayoutHorizontalSectionsHorizontalSectionItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
|
||||
|
||||
@ -219,7 +219,7 @@ func (m *ItemPagesItemCanvasLayoutVerticalSectionRequestBuilder) WebpartsById(id
|
||||
for idx, item := range m.pathParameters {
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["webPart%2Did"] = id
|
||||
}
|
||||
return NewItemPagesItemCanvasLayoutVerticalSectionWebpartsWebPartItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
|
||||
|
||||
@ -230,7 +230,7 @@ func (m *ItemPagesSitePageItemRequestBuilder) WebPartsById(id string) *ItemPages
|
||||
for idx, item := range m.pathParameters {
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["webPart%2Did"] = id
|
||||
}
|
||||
return NewItemPagesItemWebPartsWebPartItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
|
||||
|
||||
@ -196,7 +196,7 @@ func (m *SiteItemRequestBuilder) PagesById(id string) *ItemPagesSitePageItemRequ
|
||||
for idx, item := range m.pathParameters {
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["sitePage%2Did"] = id
|
||||
}
|
||||
return NewItemPagesSitePageItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
|
||||
@ -240,7 +240,7 @@ func (m *SiteItemRequestBuilder) SitesById(id string) *ItemSitesSiteItemRequestB
|
||||
for idx, item := range m.pathParameters {
|
||||
urlTplParams[idx] = item
|
||||
}
|
||||
if id != "" {
|
||||
if len(id) > 0 {
|
||||
urlTplParams["site%2Did1"] = id
|
||||
}
|
||||
return NewItemSitesSiteItemRequestBuilderInternal(urlTplParams, m.requestAdapter)
|
||||
|
||||
@ -357,7 +357,7 @@ func (mw RetryMiddleware) getRetryDelay(
|
||||
retryAfter = resp.Header.Get(retryAfterHeader)
|
||||
}
|
||||
|
||||
if retryAfter != "" {
|
||||
if len(retryAfter) > 0 {
|
||||
retryAfterDelay, err := strconv.ParseFloat(retryAfter, 64)
|
||||
if err == nil {
|
||||
return time.Duration(retryAfterDelay) * time.Second
|
||||
|
||||
@ -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>')
|
||||
// Ref: https://learn.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http
|
||||
loc := resp.Header.Get("Location")
|
||||
if loc != "" {
|
||||
if len(loc) > 0 {
|
||||
splits := strings.Split(loc, "'")
|
||||
if len(splits) != 7 || splits[4] != ")/Attachments(" || len(splits[5]) == 0 {
|
||||
return 0, clues.New("invalid format for upload completion url").
|
||||
|
||||
@ -315,7 +315,7 @@ func (suite *ExportUnitSuite) TestZipExports() {
|
||||
Body: item.Body,
|
||||
}
|
||||
|
||||
if col.BasePath() != "" {
|
||||
if len(col.BasePath()) > 0 {
|
||||
expected.Name = strings.Join([]string{col.BasePath(), item.Name}, "/")
|
||||
}
|
||||
|
||||
|
||||
@ -36,7 +36,7 @@ func LogTimeOfTest(t TestT) string {
|
||||
now := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
name := t.Name()
|
||||
|
||||
if name == "" {
|
||||
if len(name) == 0 {
|
||||
t.Logf("Test run at %s.", now)
|
||||
return now
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ func NewGroupsLocationIDer(
|
||||
pb := path.Builder{}.Append(category.String())
|
||||
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)
|
||||
|
||||
prefixElems = 2
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
if os.Getenv("XDG_CACHE_HOME") != "" {
|
||||
if len(os.Getenv("XDG_CACHE_HOME")) > 0 {
|
||||
userLogsDir = os.Getenv("XDG_CACHE_HOME")
|
||||
} else {
|
||||
userLogsDir = filepath.Join(os.Getenv("HOME"), ".cache")
|
||||
|
||||
@ -91,7 +91,7 @@ type testIDsPager struct {
|
||||
func (p *testIDsPager) GetPage(
|
||||
ctx context.Context,
|
||||
) (NextLinkValuer[any], error) {
|
||||
if p.errorCode != "" {
|
||||
if len(p.errorCode) > 0 {
|
||||
ierr := odataerrors.NewMainError()
|
||||
ierr.SetCode(&p.errorCode)
|
||||
|
||||
@ -145,7 +145,7 @@ type testIDsDeltaPager struct {
|
||||
func (p *testIDsDeltaPager) GetPage(
|
||||
ctx context.Context,
|
||||
) (DeltaLinkValuer[any], error) {
|
||||
if p.errorCode != "" {
|
||||
if len(p.errorCode) > 0 {
|
||||
ierr := odataerrors.NewMainError()
|
||||
ierr.SetCode(&p.errorCode)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user