Skip to content

Commit

Permalink
refactor: enable linter copyloopvar (scionproto#4708)
Browse files Browse the repository at this point in the history
The loop variable problem is no more since Go 1.22 (see
https://go.dev/blog/loopvar-preview).

- Remove unnecessary variable copying.
- Remove the loopvar semgrep linter.
  • Loading branch information
romshark authored Feb 24, 2025
1 parent 17a6ae1 commit abd39af
Show file tree
Hide file tree
Showing 107 changed files with 128 additions and 394 deletions.
2 changes: 1 addition & 1 deletion .golangcilint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ linters:
- typecheck
- unused
# Extended linters.
# - copyloopvar
- copyloopvar
- rowserrcheck
- sqlclosecheck
- unconvert
Expand Down
2 changes: 0 additions & 2 deletions control/beaconing/extender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func TestDefaultExtenderExtend(t *testing.T) {
},
}
for name, tc := range testsCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
mctrl := gomock.NewController(t)
defer mctrl.Finish()
Expand Down Expand Up @@ -332,7 +331,6 @@ func TestDefaultExtenderExtend(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
mctrl := gomock.NewController(t)
defer mctrl.Finish()
Expand Down
2 changes: 0 additions & 2 deletions control/beaconing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func TestHandlerHandleBeacon(t *testing.T) {
}
b.Segment.ASEntries[b.Segment.MaxIdx()].Local = addr.MustParseIA("1-ff00:0:111")
return b

},
Peer: func() *snet.UDPAddr {
return &snet.UDPAddr{
Expand Down Expand Up @@ -255,7 +254,6 @@ func TestHandlerHandleBeacon(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mctrl := gomock.NewController(t)
Expand Down
5 changes: 0 additions & 5 deletions control/beaconing/propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ func (p *Propagator) run(ctx context.Context) error {
continue
}
wg.Add(1)
intf := intf
beacons := beacons
go func() {
defer log.HandlePanic()
defer wg.Done()
Expand Down Expand Up @@ -162,7 +160,6 @@ func (p *Propagator) beaconsPerInterface(
ctx context.Context,
intfs []*ifstate.Interface,
) (map[*ifstate.Interface][]beacon.Beacon, error) {

allBeacons, err := p.Provider.BeaconsToPropagate(ctx)
if err != nil {
return nil, serrors.Wrap("fetching beacons to propagate", err)
Expand Down Expand Up @@ -207,7 +204,6 @@ func (p *Propagator) logCandidateBeacons(
logger log.Logger,
beaconsPerInterface map[*ifstate.Interface][]beacon.Beacon,
) {

if !logger.Enabled(log.DebugLevel) {
return
}
Expand Down Expand Up @@ -282,7 +278,6 @@ func (p *propagator) Propagate(ctx context.Context) error {
var wg sync.WaitGroup
for _, b := range p.beacons {
wg.Add(1)
b := b
go func() {
defer log.HandlePanic()
defer wg.Done()
Expand Down
1 change: 0 additions & 1 deletion control/drkey/arc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestLevel1ARC(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

Expand Down
13 changes: 4 additions & 9 deletions control/drkey/grpc/drkey_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ var (
tcpHost2 = netip.MustParseAddrPort("127.0.0.2:12345")
)

var _ cppb.DRKeyInterServiceServer = &dk_grpc.Server{}
var _ cppb.DRKeyIntraServiceServer = &dk_grpc.Server{}
var (
_ cppb.DRKeyInterServiceServer = &dk_grpc.Server{}
_ cppb.DRKeyIntraServiceServer = &dk_grpc.Server{}
)

func TestDRKeySV(t *testing.T) {
sv, targetResp := getSVandResp(t)
Expand Down Expand Up @@ -100,7 +102,6 @@ func TestDRKeySV(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()

})
server := dk_grpc.Server{
LocalIA: ia111,
Expand Down Expand Up @@ -160,9 +161,7 @@ func TestValidateASHost(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {

err := dk_grpc.ValidateASHostReq(tc.req, tc.LocalIA, tc.peerAddr)
tc.assertErr(t, err)
})
Expand Down Expand Up @@ -216,9 +215,7 @@ func TestValidateHostASReq(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {

err := dk_grpc.ValidateHostASReq(tc.req, tc.LocalIA, tc.peerAddr)
tc.assertErr(t, err)
})
Expand Down Expand Up @@ -286,9 +283,7 @@ func TestValidateHostHostReq(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {

err := dk_grpc.ValidateHostHostReq(tc.req, tc.LocalIA, tc.peerAddr)
tc.assertErr(t, err)
})
Expand Down
2 changes: 0 additions & 2 deletions control/drkey/prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (f *Prefetcher) Run(ctx context.Context) {
logger.Debug("Prefetching level 1 DRKeys", "AS, proto:", keysMeta)
when := time.Now().Add(f.KeyDuration)
for _, key := range keysMeta {
key := key
wg.Add(1)
go func() {
defer log.HandlePanic()
Expand All @@ -73,7 +72,6 @@ func getLevel1Key(
proto drkey.Protocol,
valTime time.Time,
) {

meta := drkey.Level1Meta{
Validity: valTime,
SrcIA: srcIA,
Expand Down
6 changes: 3 additions & 3 deletions control/mgmtapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,8 @@ func TestAPI(t *testing.T) {
Base: 2,
Serial: 1,
ISD: 12,
}},
},
},
)
h.EXPECT().GetCAHealth(gomock.Any()).Return(
api.Available, true,
Expand Down Expand Up @@ -881,7 +882,6 @@ func TestAPI(t *testing.T) {
}

for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
Expand All @@ -902,7 +902,7 @@ func TestAPI(t *testing.T) {
goldenFile := "testdata/" + xtest.SanitizedName(t)
if *update {
raw := strings.ReplaceAll(rr.Body.String(), expiresAt, "EXPIRES_AT")
require.NoError(t, os.WriteFile(goldenFile, []byte(raw), 0666))
require.NoError(t, os.WriteFile(goldenFile, []byte(raw), 0o666))
}
goldenRaw, err := os.ReadFile(goldenFile)
require.NoError(t, err)
Expand Down
1 change: 0 additions & 1 deletion control/trust/crypto_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func TestChainLoaderChains(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
Expand Down
2 changes: 0 additions & 2 deletions control/trust/grpc/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func TestReqToTRCQuery(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
id, err := trustgrpc.RequestToTRCQuery(tc.Input)
Expand Down Expand Up @@ -136,7 +135,6 @@ func TestChainsToRep(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
chains := trustgrpc.ChainsToResponse(tc.Input)
Expand Down
1 change: 0 additions & 1 deletion control/trust/signer_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ func TestChachingSignerGen(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
mctrl := gomock.NewController(t)
Expand Down
1 change: 0 additions & 1 deletion daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TestAPIAddress(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
addr := daemon.APIAddress(tc.Input)
Expand Down
3 changes: 0 additions & 3 deletions gateway/control/configpublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (n *ConfigPublisher) Publish(sp SessionPolicies, rp *routing.Policy) {
n.sessionPolicies = sp.Copy()
wg.Add(len(n.sessionPoliciesSubscribers))
for _, c := range n.sessionPoliciesSubscribers {
c := c
go func() {
defer log.HandlePanic()
defer wg.Done()
Expand All @@ -57,7 +56,6 @@ func (n *ConfigPublisher) Publish(sp SessionPolicies, rp *routing.Policy) {
}
wg.Add(len(n.remoteIAsSubscribers))
for _, c := range n.remoteIAsSubscribers {
c := c
go func() {
defer log.HandlePanic()
defer wg.Done()
Expand All @@ -69,7 +67,6 @@ func (n *ConfigPublisher) Publish(sp SessionPolicies, rp *routing.Policy) {
n.routingPolicy = rp.Copy()
wg.Add(len(n.routingPoliciesSubscribers))
for _, c := range n.routingPoliciesSubscribers {
c := c
go func() {
defer log.HandlePanic()
defer wg.Done()
Expand Down
1 change: 0 additions & 1 deletion gateway/control/enginecontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ func TestBuildRoutingChains(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
chains, sm := control.BuildRoutingChains(tc.Input)
assert.Equal(t, tc.Chains, chains)
Expand Down
7 changes: 4 additions & 3 deletions gateway/control/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ var (
BuildRoutingChains = buildRoutingChains
)

type ConjunctionPathPol = conjuctionPathPol
type Diff = diff
type (
ConjunctionPathPol = conjuctionPathPol
Diff = diff
)

func (w *GatewayWatcher) RunOnce(ctx context.Context) {
w.run(ctx)
Expand All @@ -40,7 +42,6 @@ func (w *GatewayWatcher) RunOnce(ctx context.Context) {
func (w *GatewayWatcher) RunAllPrefixWatchersOnceForTest(ctx context.Context) error {
var eg errgroup.Group
for _, wi := range w.currentWatchers {
wi := wi
wi.prefixWatcher.resetRunMarker()
eg.Go(func() error {
return wi.prefixWatcher.Run(ctx)
Expand Down
1 change: 0 additions & 1 deletion gateway/control/grpc/prefix_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func TestIPPrefixServerPrefixes(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down
3 changes: 0 additions & 3 deletions gateway/control/grpc/probeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestControlDispatcher(t *testing.T) {

conn := mock_net.NewMockPacketConn(ctrl)
for _, raw := range requests {
raw := raw
conn.EXPECT().ReadFrom(gomock.Any()).DoAndReturn(
func(buf []byte) (int, net.Addr, error) {
copy(buf, raw)
Expand All @@ -67,7 +66,6 @@ func TestControlDispatcher(t *testing.T) {
)
}
for _, raw := range requests[:3] {
raw := raw
// Use the fact that protobuf serializes this to the same wire format.
conn.EXPECT().WriteTo(raw, src)
}
Expand Down Expand Up @@ -96,5 +94,4 @@ func TestControlDispatcher(t *testing.T) {

// Make sure that the dispatcher shuts down.
<-done

}
1 change: 0 additions & 1 deletion gateway/control/prefixesfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ accept 1-ff00:0:113 1-ff00:0:110 10.3.0.0/25`))
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
Expand Down
1 change: 0 additions & 1 deletion gateway/control/remotemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func (rm *RemoteMonitor) process(ctx context.Context, ias []addr.IA) {
logger := log.FromCtx(ctx)
newWatchers := make(map[addr.IA]watcherEntry)
for _, ia := range ias {
ia := ia
we, ok := rm.currentWatchers[ia]
if ok {
// Watcher for the remote IA exists. Move it to the new map of
Expand Down
1 change: 0 additions & 1 deletion gateway/control/routemgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func TestRouteString(t *testing.T) {
}

for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.String, tc.Route.String())
})
Expand Down
5 changes: 0 additions & 5 deletions gateway/control/sessionconfigurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ func TestBuildSessionConfigs(t *testing.T) {
},
RoutingUpdate: control.RemoteGateways{
Gateways: map[addr.IA][]control.RemoteGateway{

addr.MustParseIA("1-ff00:0:110"): {
{
Gateway: control.Gateway{
Expand Down Expand Up @@ -492,7 +491,6 @@ func TestBuildSessionConfigs(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -650,7 +648,6 @@ func TestConjuctionPolicy(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
pol := control.ConjunctionPathPol{Pol1: tc.Pol1, Pol2: tc.Pol2}
acceptedPaths := pol.Filter(tc.InputPaths)
Expand Down Expand Up @@ -723,7 +720,6 @@ func TestNewPathPolForEnteringAS(t *testing.T) {
},
},
path.Path{

Meta: snet.PathMetadata{
Interfaces: []snet.PathInterface{
{IA: addr.MustParseIA("1-ff00:0:112"), ID: 2},
Expand Down Expand Up @@ -776,7 +772,6 @@ func TestNewPathPolForEnteringAS(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
pol := control.NewPathPolForEnteringAS(tc.IA, tc.Interfaces)
Expand Down
3 changes: 0 additions & 3 deletions gateway/control/sessionpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func TestLegacySessionPolicyAdapterParse(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
parser := control.LegacySessionPolicyAdapter{}
p, err := parser.Parse(context.Background(), tc.Input)
Expand Down Expand Up @@ -164,7 +163,6 @@ func TestLoadSessionPolicies(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down Expand Up @@ -206,7 +204,6 @@ func TestSessionPoliciesRemoteIAs(t *testing.T) {
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
assert.ElementsMatch(t, tc.Expected, tc.Policies.RemoteIAs())
Expand Down
Loading

0 comments on commit abd39af

Please sign in to comment.