Skip to content

Commit

Permalink
DE-1369 Add gocritic linter (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc authored Dec 7, 2024
1 parent b3b8931 commit 8e3c3ac
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 37 deletions.
19 changes: 8 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ linters:
- govet
- gosimple
- goimports
- gocritic
- revive
- gocyclo
- noctx
- gomodguard
- nolintlint

# TODO:
# - gocritic
# - stylecheck
# - stylecheck # TODO(v5): enable
# - unused

linters-settings:
Expand All @@ -32,6 +32,12 @@ linters-settings:
- opinionated
- performance
- style
disabled-checks:
- singleCaseSwitch
- deferInLoop # TODO(DE-1373): enable
- hugeParam # TODO(v5): enable?
- sprintfQuotedString # noisy # TODO: enable
- exitAfterDefer # TODO: enable?

errcheck:
# List of functions to exclude from checking, where each entry is a single function to exclude.
Expand Down Expand Up @@ -144,15 +150,6 @@ linters-settings:
disabled: false
exclude: [""]

# TODO(DE-1373): enable:
# # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#defer
# - name: defer
# severity: warning
# disabled: false
# exclude: [""]
# arguments:
# - [ "call-chain", "loop" ]

# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#dot-imports
- name: dot-imports
severity: warning
Expand Down
2 changes: 1 addition & 1 deletion analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (iter *MetricsIterator) Next(ctx context.Context, resp *MetricsResponse) (m
return false
}

iter.opts.Pagination.Skip = iter.opts.Pagination.Skip + iter.opts.Pagination.Limit
iter.opts.Pagination.Skip += iter.opts.Pagination.Limit

return len(resp.Items) == iter.opts.Pagination.Limit
}
Expand Down
2 changes: 1 addition & 1 deletion attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func createAttachment(t *testing.T) string {
f, err := os.Create(name)
require.NoError(t, err)

_, err = f.Write([]byte(randomString(100, "")))
_, err = f.WriteString(randomString(100, ""))
require.NoError(t, err)
require.Nil(t, f.Close())
return name
Expand Down
6 changes: 3 additions & 3 deletions bounces.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (mg *MailgunImpl) GetBounce(ctx context.Context, address string) (Bounce, e
//
// Note that both code and error exist as strings, even though
// code will report as a number.
func (mg *MailgunImpl) AddBounce(ctx context.Context, address, code, error string) error {
func (mg *MailgunImpl) AddBounce(ctx context.Context, address, code, bounceError string) error {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
Expand All @@ -182,8 +182,8 @@ func (mg *MailgunImpl) AddBounce(ctx context.Context, address, code, error strin
if code != "" {
payload.addValue("code", code)
}
if error != "" {
payload.addValue("error", error)
if bounceError != "" {
payload.addValue("error", bounceError)
}
_, err := makePostRequest(ctx, r, payload)
return err
Expand Down
4 changes: 2 additions & 2 deletions credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (ri *CredentialsIterator) Next(ctx context.Context, items *[]Credential) bo
if len(ri.Items) == 0 {
return false
}
ri.offset = ri.offset + len(ri.Items)
ri.offset += len(ri.Items)
return true
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func (ri *CredentialsIterator) Previous(ctx context.Context, items *[]Credential
return false
}

ri.offset = ri.offset - (ri.limit * 2)
ri.offset -= ri.limit * 2
if ri.offset < 0 {
ri.offset = 0
}
Expand Down
6 changes: 3 additions & 3 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (ri *DomainsIterator) Next(ctx context.Context, items *[]Domain) bool {
if len(ri.Items) == 0 {
return false
}
ri.offset = ri.offset + len(ri.Items)
ri.offset += len(ri.Items)
return true
}

Expand Down Expand Up @@ -199,7 +199,7 @@ func (ri *DomainsIterator) Previous(ctx context.Context, items *[]Domain) bool {
return false
}

ri.offset = ri.offset - (ri.limit * 2)
ri.offset -= ri.limit * 2
if ri.offset < 0 {
ri.offset = 0
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func (mg *MailgunImpl) CreateDomain(ctx context.Context, name string, opts *Crea
if len(opts.IPS) != 0 {
payload.addValue("ips", strings.Join(opts.IPS, ","))
}
if len(opts.Password) != 0 {
if opts.Password != "" {
payload.addValue("smtp_password", opts.Password)
}
if opts.WebScheme != "" {
Expand Down
4 changes: 2 additions & 2 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func TestDomainTracking(t *testing.T) {
require.NoError(t, err)

require.False(t, info.Unsubscribe.Active)
require.True(t, len(info.Unsubscribe.HTMLFooter) != 0)
require.True(t, len(info.Unsubscribe.TextFooter) != 0)
require.True(t, info.Unsubscribe.HTMLFooter != "")
require.True(t, info.Unsubscribe.TextFooter != "")
require.True(t, info.Click.Active)
require.True(t, info.Open.Active)

Expand Down
5 changes: 3 additions & 2 deletions email_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,15 @@ func (m *EmailValidatorImpl) validateV4(ctx context.Context, email string, mailB
// as `https://api.mailgun.net/v3` or set `v.SetAPIBase("https://api.mailgun.net/v3")`
//
// Deprecated: /v3/address/parse is deprecated use ValidateEmail instead.
func (m *EmailValidatorImpl) ParseAddresses(ctx context.Context, addresses ...string) ([]string, []string, error) {
// TODO(v5): remove
func (m *EmailValidatorImpl) ParseAddresses(ctx context.Context, addresses ...string) (parsed, unparseable []string, err error) {
r := newHTTPRequest(m.getAddressURL("parse"))
r.setClient(m.Client())
r.addParameter("addresses", strings.Join(addresses, ","))
r.setBasicAuth(basicAuthUser, m.APIKey())

var response addressParseResult
err := getResponseFromJSON(ctx, r, &response)
err = getResponseFromJSON(ctx, r, &response)
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func (g *Generic) GetTimestamp() time.Time {
}

func (g *Generic) SetTimestamp(t time.Time) {
// convert := fmt.Sprintf("%d.%06d", t.Unix(), t.Nanosecond()/int(time.Microsecond))
// ts, err := strconv.ParseFloat(convert, 64)
g.Timestamp = float64(t.Unix()) + (float64(t.Nanosecond()/int(time.Microsecond)) / float64(1000000))
}

Expand Down
1 change: 0 additions & 1 deletion mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ func generateCredentialsUrl(m Mailgun, login string) string {
tail = fmt.Sprintf("/%s", login)
}
return generateDomainApiUrl(m, fmt.Sprintf("credentials%s", tail))
// return fmt.Sprintf("%s/domains/%s/credentials%s", apiBase, m.Domain(), tail)
}

// generatePublicApiUrl works as generateApiUrl, except that generatePublicApiUrl has no need for the domain.
Expand Down
2 changes: 1 addition & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestTimeStamp(t *testing.T) {
assert.Equal(t, ts, event.GetTimestamp())

event.Timestamp = 1546899001.019501
assert.Equal(t, time.Date(2019, 1, 7, 22, 10, 01, 19501056, time.UTC), event.GetTimestamp())
assert.Equal(t, time.Date(2019, 1, 7, 22, 10, 1, 19501056, time.UTC), event.GetTimestamp())
}

func TestEventNames(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (ri *RoutesIterator) Next(ctx context.Context, items *[]Route) bool {
if len(ri.Items) == 0 {
return false
}
ri.offset = ri.offset + len(ri.Items)
ri.offset += len(ri.Items)
return true
}

Expand Down Expand Up @@ -225,7 +225,7 @@ func (ri *RoutesIterator) Previous(ctx context.Context, items *[]Route) bool {
return false
}

ri.offset = ri.offset - (ri.limit * 2)
ri.offset -= ri.limit * 2
if ri.offset < 0 {
ri.offset = 0
}
Expand Down Expand Up @@ -273,10 +273,11 @@ func (mg *MailgunImpl) CreateRoute(ctx context.Context, prototype Route) (_ignor
p.addValue("action", action)
}
var resp createRouteResp
if err = postResponseFromJSON(ctx, r, p, &resp); err != nil {
if err := postResponseFromJSON(ctx, r, p, &resp); err != nil {
return _ignored, err
}
return resp.Route, err

return resp.Route, nil
}

// DeleteRoute removes the specified route from your domain's configuration.
Expand Down
1 change: 0 additions & 1 deletion spam_complaints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestGetComplaints(t *testing.T) {
it := mg.ListComplaints(nil)
var page []mailgun.Complaint
for it.Next(ctx, &page) {
// spew.Dump(page)
}
require.NoError(t, it.Err())
}
Expand Down
4 changes: 2 additions & 2 deletions subaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (ri *SubaccountsIterator) Next(ctx context.Context, items *[]Subaccount) bo
if len(ri.Items) == 0 {
return false
}
ri.offset = ri.offset + len(ri.Items)
ri.offset += len(ri.Items)
return true
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func (ri *SubaccountsIterator) Previous(ctx context.Context, items *[]Subaccount
return false
}

ri.offset = ri.offset - (ri.limit * 2)
ri.offset -= ri.limit * 2
if ri.offset < 0 {
ri.offset = 0
}
Expand Down
3 changes: 2 additions & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (mg *MailgunImpl) GetTag(ctx context.Context, tag string) (Tag, error) {
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var tagItem Tag
return tagItem, getResponseFromJSON(ctx, r, &tagItem)
err := getResponseFromJSON(ctx, r, &tagItem)
return tagItem, err
}

// ListTags returns a cursor used to iterate through a list of tags
Expand Down

0 comments on commit 8e3c3ac

Please sign in to comment.