Skip to content

Commit

Permalink
enable linters: copyloopvar, intrange (#3184)
Browse files Browse the repository at this point in the history
* enable linters: copyloopvar, intrange

* lint
  • Loading branch information
mmetc committed Sep 17, 2024
1 parent f97b9c8 commit 5f22c78
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 22 deletions.
13 changes: 3 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,9 @@ linters:
- funlen # revive
- gocognit # revive

#
# Disabled until fixed for go 1.22
#
# Disabled atm

- copyloopvar # copyloopvar is a linter detects places where loop variables are copied
- intrange # intrange is a linter to find places where for loops could make use of an integer range.
- intrange # intrange is a linter to find places where for loops could make use of an integer range.

#
# Enabled
Expand All @@ -212,6 +209,7 @@ linters:
# - asciicheck # checks that all code identifiers does not have non-ASCII symbols in the name
# - bidichk # Checks for dangerous unicode character sequences
# - bodyclose # checks whether HTTP response body is closed successfully
# - copyloopvar # copyloopvar is a linter detects places where loop variables are copied
# - decorder # check declaration order and count of types, constants, variables and functions
# - depguard # Go linter that checks if package imports are in a list of acceptable packages
# - dupword # checks for duplicate words in the source code
Expand Down Expand Up @@ -490,11 +488,6 @@ issues:
path: "cmd/crowdsec-cli/idgen/password.go"
text: "deep-exit: .*"

- linters:
- revive
path: "cmd/crowdsec-cli/utils.go"
text: "deep-exit: .*"

- linters:
- revive
path: "pkg/leakybucket/overflows.go"
Expand Down
4 changes: 2 additions & 2 deletions pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg, prom *csconfig

func GetMetrics(sources []DataSource, aggregated bool) error {
var metrics []prometheus.Collector
for i := range len(sources) {
for i := range sources {
if aggregated {
metrics = sources[i].GetMetrics()
} else {
Expand Down Expand Up @@ -378,7 +378,7 @@ func StartAcquisition(sources []DataSource, output chan types.Event, AcquisTomb
return nil
}

for i := range len(sources) {
for i := range sources {
subsrc := sources[i] // ensure its a copy
log.Debugf("starting one source %d/%d ->> %T", i, len(sources), subsrc)

Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/docker/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func parseKeyToMap(m map[string]interface{}, key string, value string) {
return
}

for i := range len(parts) {
for i := range parts {
if parts[i] == "" {
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiclient/decisions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s *DecisionsService) FetchV3Decisions(ctx context.Context, url string) (*m
partialDecisions := make([]*models.Decision, len(decisionsGroup.Decisions))

for idx, decision := range decisionsGroup.Decisions {
decision := decision // fix exportloopref linter message
decision := decision //nolint:copyloopvar // fix exportloopref linter message
partialDecisions[idx] = &models.Decision{
Scenario: &scenarioDeleted,
Scope: decisionsGroup.Scope,
Expand Down
5 changes: 3 additions & 2 deletions pkg/apiserver/apic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ func TestAPICPush(t *testing.T) {
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
api := getAPIC(t)
api.pushInterval = time.Millisecond
Expand All @@ -1114,8 +1113,10 @@ func TestAPICPush(t *testing.T) {

httpmock.RegisterResponder("POST", "http://api.crowdsec.net/api/signals", httpmock.NewBytesResponder(200, []byte{}))

// capture the alerts to avoid datarace
alerts := tc.alerts
go func() {
api.AlertsAddChan <- tc.alerts
api.AlertsAddChan <- alerts

time.Sleep(time.Second)
api.Shutdown()
Expand Down
1 change: 0 additions & 1 deletion pkg/csplugin/broker_win_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func (s *PluginSuite) TestBrokerInit() {
}

for _, tc := range tests {
tc := tc
s.Run(tc.name, func() {
t := s.T()
if tc.action != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/csplugin/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func CheckPerms(path string) error {
*/
aceCount := rs.Field(3).Uint()

for i := uint64(0); i < aceCount; i++ {
for i := range aceCount {
ace := &AccessAllowedAce{}
ret, _, _ := procGetAce.Call(uintptr(unsafe.Pointer(dacl)), uintptr(i), uintptr(unsafe.Pointer(&ace)))
if ret == 0 {
Expand Down
1 change: 0 additions & 1 deletion pkg/csplugin/utils_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func TestGetPluginNameAndTypeFromPath(t *testing.T) {
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
got, got1, err := getPluginTypeAndSubtypeFromPath(tc.path)
cstest.RequireErrorContains(t, err, tc.expectedErr)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cwhub/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func isYAMLFileName(path string) bool {
// returns error if the symlink is dangling or too many symlinks are followed
func resolveSymlink(path string) (string, error) {
const maxSymlinks = 10 // Prevent infinite loops
for i := 0; i < maxSymlinks; i++ {
for range maxSymlinks {
fi, err := os.Lstat(path)
if err != nil {
return "", err // dangling link
Expand Down
2 changes: 1 addition & 1 deletion pkg/leakybucket/manager_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func PourItemToHolders(parsed types.Event, holders []BucketFactory, buckets *Buc
BucketPourCache["OK"] = append(BucketPourCache["OK"], evt.(types.Event))
}
//find the relevant holders (scenarios)
for idx := range len(holders) {
for idx := range holders {
//for idx, holder := range holders {

//evaluate bucket's condition
Expand Down
1 change: 0 additions & 1 deletion pkg/setup/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func TestNormalizeVersion(t *testing.T) {
}
for _, tc := range tests {
tc := tc
t.Run(tc.version, func(t *testing.T) {
t.Parallel()
actual := setup.NormalizeVersion(tc.version)
Expand Down

0 comments on commit 5f22c78

Please sign in to comment.