Skip to content

Commit

Permalink
chore: Enable deferInLoop, exitAfterDefer and unnecessaryDefer checke…
Browse files Browse the repository at this point in the history
…rs for gocritic (influxdata#13280)

Co-authored-by: Pawel Zak <Pawel Zak>
  • Loading branch information
zak-pawel authored May 19, 2023
1 parent 129f888 commit 727533e
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,21 @@ linters-settings:
- builtinShadowDecl
- caseOrder
- codegenComment
- deferInLoop
- dupBranchBody
- dupCase
- dupSubExpr
- emptyDecl
- evalOrder
- exitAfterDefer
- externalErrorReassign
- nilValReturn
- regexpPattern
- sloppyTypeAssert
- sortSlice
- sqlQuery
- uncheckedInlineErr
- unnecessaryDefer
- weakCond
gosec:
# To select a subset of rules to run.
Expand Down
2 changes: 1 addition & 1 deletion agent/agent_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ func watchForFlushSignal(flushRequested chan os.Signal) {
}

func stopListeningForFlushSignal(flushRequested chan os.Signal) {
defer signal.Stop(flushRequested)
signal.Stop(flushRequested)
}
5 changes: 3 additions & 2 deletions config/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"testing"

"github.com/awnumar/memguard"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/secretstores"
"github.com/stretchr/testify/require"
)

func TestSecretConstantManually(t *testing.T) {
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestUninitializedEnclave(t *testing.T) {
retrieved, err := s.Get()
require.NoError(t, err)
require.Empty(t, retrieved)
defer ReleaseSecret(retrieved)
ReleaseSecret(retrieved)
}

func TestEnclaveOpenError(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/haproxy/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestHaproxyGeneratesMetricsUsingSocket(t *testing.T) {
}

sockets[i] = sock
defer sock.Close() //nolint:revive // done on purpose, closing will be executed properly
defer sock.Close() //nolint:revive,gocritic // done on purpose, closing will be executed properly

s := statServer{}
go s.serverSocket(sock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ func TestMain(m *testing.M) {
log.Fatalf("Failed to listen: %v", err)
}

exitCode := 0
defer func() {
os.Exit(exitCode)
}()

cfg.Servers = []string{lis.Addr().String()}

var opts []grpc.ServerOption
Expand All @@ -262,5 +267,6 @@ func TestMain(m *testing.M) {
grpcServer.Serve(lis) //nolint:errcheck // ignore the returned error as the tests will fail anyway
}()
defer grpcServer.Stop()
os.Exit(m.Run())

exitCode = m.Run()
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func main() {

endpoint, err := zipkin.NewEndpoint("Trivial", "127.0.0.1:0")
if err != nil {
log.Fatalf("Error: %v\n", err)
log.Panicf("Error: %v\n", err)
}

nativeTracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
if err != nil {
log.Fatalf("Error: %v\n", err)
log.Panicf("Error: %v\n", err)
}

tracer := zipkinot.Wrap(nativeTracer)
Expand Down
6 changes: 3 additions & 3 deletions plugins/outputs/groundwork/groundwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestWriteWithDefaults(t *testing.T) {
err := i.Write([]telegraf.Metric{intMetric})
require.NoError(t, err)

defer server.Close()
server.Close()
}

func TestWriteWithFields(t *testing.T) {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestWriteWithFields(t *testing.T) {
err := i.Write([]telegraf.Metric{floatMetric})
require.NoError(t, err)

defer server.Close()
server.Close()
}

func TestWriteWithTags(t *testing.T) {
Expand Down Expand Up @@ -189,5 +189,5 @@ func TestWriteWithTags(t *testing.T) {
err := i.Write([]telegraf.Metric{floatMetric})
require.NoError(t, err)

defer server.Close()
server.Close()
}
2 changes: 1 addition & 1 deletion plugins/outputs/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (p *SQL) Write(metrics []telegraf.Metric) error {
if err != nil {
return fmt.Errorf("prepare failed: %w", err)
}
defer stmt.Close() //nolint:revive // We cannot do anything about a failing close.
defer stmt.Close() //nolint:revive,gocritic // done on purpose, closing will be executed properly

_, err = stmt.Exec(values...)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/readme_config_includer/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,6 @@ func main() {
}
defer file.Close()
if _, err := output.WriteTo(file); err != nil {
log.Fatalf("Writing output file failed: %v", err)
log.Panicf("Writing output file failed: %v", err)
}
}

0 comments on commit 727533e

Please sign in to comment.