Skip to content

Commit 58a9f2b

Browse files
authored
refact: remove unused struct fields and params / 3; enable linter "unused" (#3334)
* lint: configure unused linter * Remove unused AlertContext.Log field * unused consoleOptions * unused isSwarmManager * unused withCurrentYear - rfc5424 has it by spec * unused collectionName * unused notificationConfigsByPluginType * unused local vars * unused: windows build
1 parent 813e2c5 commit 58a9f2b

File tree

11 files changed

+39
-48
lines changed

11 files changed

+39
-48
lines changed

.golangci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ linters:
168168
ifElseChain:
169169
minThreshold: 4
170170

171+
unused:
172+
# Mark all struct fields that have been written to as used.
173+
# Default: true
174+
field-writes-are-uses: false
175+
# Treat IncDec statement (e.g. `i++` or `i--`) as both read and write operation instead of just write.
176+
# Default: false
177+
post-statements-are-reads: false
178+
# Mark all exported identifiers as used.
179+
# Default: true
180+
# exported-is-used: false
181+
# Mark all exported fields as used.
182+
# default: true
183+
exported-fields-are-used: false
184+
# Mark all function parameters as used.
185+
# default: true
186+
parameters-are-used: true
187+
# Mark all local variables as used.
188+
# default: true
189+
local-variables-are-used: false
190+
# Mark all identifiers inside generated files as used.
191+
# Default: true
192+
generated-is-used: false
193+
171194
gomoddirectives:
172195
replace-allow-list:
173196
- golang.org/x/time

cmd/crowdsec-cli/clihubtest/table.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ func hubTestCoverageTable(out io.Writer, wantColor string, headers []string, cov
5858
t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)
5959
t.SetAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft)
6060

61-
parserTested := 0
62-
6361
for _, test := range coverage {
6462
status := emoji.RedCircle
6563
if test.TestsCount > 0 {
6664
status = emoji.GreenCircle
67-
parserTested++
6865
}
6966

7067
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))

cmd/crowdsec-cli/dashboard_unsupported.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ import (
1010
)
1111

1212
type cliDashboard struct{
13-
cfg configGetter
1413
}
1514

16-
func NewCLIDashboard(cfg configGetter) *cliDashboard {
17-
return &cliDashboard{
18-
cfg: cfg,
19-
}
15+
func NewCLIDashboard(_ configGetter) *cliDashboard {
16+
return &cliDashboard{}
2017
}
2118

2219
func (cli cliDashboard) NewCommand() *cobra.Command {

cmd/crowdsec/lpmetrics.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ type staticMetrics struct {
5252
osVersion string
5353
startupTS int64
5454
featureFlags []string
55-
consoleOptions []string
5655
datasourceMap map[string]int64
5756
hubState models.HubItems
5857
}
@@ -104,7 +103,6 @@ func newStaticMetrics(consoleOptions []string, datasources []acquisition.DataSou
104103
osVersion: osVersion,
105104
startupTS: time.Now().UTC().Unix(),
106105
featureFlags: fflag.Crowdsec.GetEnabledFeatures(),
107-
consoleOptions: consoleOptions,
108106
datasourceMap: datasourceMap,
109107
hubState: getHubState(hub),
110108
}

pkg/acquisition/modules/docker/docker_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ func TestConfigureDSN(t *testing.T) {
200200

201201
type mockDockerCli struct {
202202
client.Client
203-
isSwarmManager bool
204203
services []dockerTypesSwarm.Service
205204
}
206205

@@ -348,8 +347,7 @@ service_name_regexp:
348347
return
349348
}
350349

351-
mockClient := &mockDockerCli{isSwarmManager: ts.isSwarmManager}
352-
dockerSource.Client = mockClient
350+
dockerSource.Client = &mockDockerCli{}
353351

354352
// Manually set swarm manager flag for testing since Info() mock is simplified
355353
dockerSource.isSwarmManager = ts.isSwarmManager
@@ -496,8 +494,7 @@ use_service_labels: true`,
496494
err := f.Configure(ctx, []byte(test.config), subLogger, metrics.AcquisitionMetricsLevelNone)
497495
require.NoError(t, err)
498496

499-
mockClient := &mockDockerCli{isSwarmManager: test.isSwarmManager}
500-
f.Client = mockClient
497+
f.Client = &mockDockerCli{}
501498

502499
// Manually set swarm manager flag for testing
503500
f.isSwarmManager = test.isSwarmManager
@@ -567,10 +564,9 @@ service_name:
567564
for _, test := range tests {
568565
t.Run(test.name, func(t *testing.T) {
569566
subLogger := log.WithField("type", "docker")
570-
f := DockerSource{}
571-
572-
mockClient := &mockDockerCli{isSwarmManager: test.isSwarmManager}
573-
f.Client = mockClient
567+
f := DockerSource{
568+
Client: &mockDockerCli{},
569+
}
574570

575571
err := f.Configure(ctx, []byte(test.config), subLogger, metrics.AcquisitionMetricsLevelNone)
576572
require.NoError(t, err)

pkg/acquisition/modules/syslog/internal/parser/rfc5424/parse.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type RFC5424 struct {
2121
len int
2222
position int
2323
buf []byte
24-
useCurrentYear bool //If no year is specified in the timestamp, use the current year
2524
strictHostname bool //If the hostname contains invalid characters or is not an IP, return an error
2625
}
2726

@@ -35,12 +34,6 @@ var VALID_TIMESTAMPS = []string{
3534

3635
const VALID_TIMESTAMP = time.RFC3339Nano
3736

38-
func WithCurrentYear() RFC5424Option {
39-
return func(r *RFC5424) {
40-
r.useCurrentYear = true
41-
}
42-
}
43-
4437
func WithStrictHostname() RFC5424Option {
4538
return func(r *RFC5424) {
4639
r.strictHostname = true

pkg/alertcontext/alertcontext.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type Context struct {
2525
ContextToSend map[string][]string
2626
ContextValueLen int
2727
ContextToSendCompiled map[string][]*vm.Program
28-
Log *log.Logger
2928
}
3029

3130
func ValidateContextExpr(key string, expressions []string) error {
@@ -44,25 +43,19 @@ func ValidateContextExpr(key string, expressions []string) error {
4443
}
4544

4645
func NewAlertContext(contextToSend map[string][]string, valueLength int) error {
47-
clog := log.New()
48-
if err := types.ConfigureLogger(clog, 0); err != nil {
49-
return fmt.Errorf("couldn't create logger for alert context: %w", err)
50-
}
51-
5246
if valueLength == 0 {
53-
clog.Debugf("No console context value length provided, using default: %d", MaxContextValueLen)
47+
log.Debugf("No console context value length provided, using default: %d", MaxContextValueLen)
5448
valueLength = MaxContextValueLen
5549
}
5650

5751
if valueLength > MaxContextValueLen {
58-
clog.Debugf("Provided console context value length (%d) is higher than the maximum, using default: %d", valueLength, MaxContextValueLen)
52+
log.Debugf("Provided console context value length (%d) is higher than the maximum, using default: %d", valueLength, MaxContextValueLen)
5953
valueLength = MaxContextValueLen
6054
}
6155

6256
alertContext = Context{
6357
ContextToSend: contextToSend,
6458
ContextValueLen: valueLength,
65-
Log: clog,
6659
ContextToSendCompiled: make(map[string][]*vm.Program),
6760
}
6861

pkg/appsec/appsec_rules_collection.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
)
1414

1515
type AppsecCollection struct {
16-
collectionName string
1716
Rules []string
1817
NativeRules []string
1918
}
@@ -70,9 +69,7 @@ func LoadCollection(pattern string, logger *log.Entry) ([]AppsecCollection, erro
7069
continue
7170
}
7271

73-
appsecCol := AppsecCollection{
74-
collectionName: appsecRule.Name,
75-
}
72+
appsecCol := AppsecCollection{}
7673

7774
if appsecRule.SecLangFilesRules != nil {
7875
for _, rulesFile := range appsecRule.SecLangFilesRules {

pkg/csplugin/broker.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type PluginBroker struct {
4545
profileConfigs []*csconfig.ProfileCfg
4646
pluginConfigByName map[string]PluginConfig
4747
pluginMap map[string]plugin.Plugin
48-
notificationConfigsByPluginType map[string][][]byte // "slack" -> []{config1, config2}
4948
notificationPluginByName map[string]protobufs.NotifierServer
5049
watcher PluginWatcher
5150
pluginKillMethods []func()
@@ -96,7 +95,6 @@ type PluginConfigList []PluginConfig
9695

9796
func (pb *PluginBroker) Init(ctx context.Context, pluginCfg *csconfig.PluginCfg, profileConfigs []*csconfig.ProfileCfg, configPaths *csconfig.ConfigurationPaths) error {
9897
pb.PluginChannel = make(chan models.ProfileAlert)
99-
pb.notificationConfigsByPluginType = make(map[string][][]byte)
10098
pb.notificationPluginByName = make(map[string]protobufs.NotifierServer)
10199
pb.pluginMap = make(map[string]plugin.Plugin)
102100
pb.pluginConfigByName = make(map[string]PluginConfig)

pkg/csplugin/utils_windows.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import (
2121

2222
var (
2323
advapi32 = syscall.NewLazyDLL("advapi32.dll")
24-
2524
procGetAce = advapi32.NewProc("GetAce")
2625
)
2726

27+
var _ = func() any {
28+
var pb PluginBroker
29+
_ = pb.pluginProcConfig // reference to silence unused linter
30+
return nil
31+
}()
32+
2833
type AclSizeInformation struct {
2934
AceCount uint32
3035
AclBytesInUse uint32

0 commit comments

Comments
 (0)