Skip to content

Commit 6fe248a

Browse files
Merge pull request #6942 from Luap99/lint
golangci-lint: enable nilnesserr, modernize and more govet checks
2 parents 8398106 + 605324b commit 6fe248a

18 files changed

Lines changed: 92 additions & 127 deletions

File tree

.golangci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ formatters:
1212

1313
linters:
1414
enable:
15+
- modernize
16+
- nilnesserr
1517
- nolintlint
1618
- revive
1719
- unconvert
@@ -27,3 +29,12 @@ linters:
2729
checks:
2830
- all
2931
- -QF1008 # https://staticcheck.dev/docs/checks/#QF1008 Omit embedded fields from selector expression.
32+
govet:
33+
enable-all: true
34+
disable:
35+
- fieldalignment
36+
- shadow
37+
38+
issues:
39+
max-issues-per-linter: 0
40+
max-same-issues: 0

add.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
636636
}()
637637
}
638638

639-
wg.Add(1)
640-
go func() {
639+
wg.Go(func() {
641640
b.ContentDigester.Start("")
642641
hashCloser := b.ContentDigester.Hash()
643642
hasher := io.Writer(hashCloser)
@@ -661,8 +660,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
661660
}
662661
hashCloser.Close()
663662
pipeReader.Close()
664-
wg.Done()
665-
}()
663+
})
666664
wg.Wait()
667665
if getErr != nil {
668666
getErr = fmt.Errorf("reading %q: %w", src, getErr)
@@ -739,8 +737,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
739737
latestTimestamp = st.ModTime
740738
}
741739
pipeReader, pipeWriter := io.Pipe()
742-
wg.Add(1)
743-
go func() {
740+
wg.Go(func() {
744741
renamedItems := 0
745742
writer := io.WriteCloser(pipeWriter)
746743
if renameTarget != "" {
@@ -796,10 +793,8 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
796793
if renameTarget != "" && renamedItems > 1 {
797794
renameErr = fmt.Errorf("internal error: renamed %d items when we expected to only rename 1", renamedItems)
798795
}
799-
wg.Done()
800-
}()
801-
wg.Add(1)
802-
go func() {
796+
})
797+
wg.Go(func() {
803798
if st.IsDir {
804799
b.ContentDigester.Start("dir")
805800
} else {
@@ -829,8 +824,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
829824
}
830825
hashCloser.Close()
831826
pipeReader.Close()
832-
wg.Done()
833-
}()
827+
})
834828

835829
wg.Wait()
836830
if getErr != nil {

bind/mount.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,6 @@ func UnmountMountpoints(mountpoint string, mountpointsToRemove []string) error {
254254
// find the top of the tree we're unmounting
255255
top := getMountByPoint(mountpoint)
256256
if top == nil {
257-
if err != nil {
258-
return fmt.Errorf("%q is not mounted: %w", mountpoint, err)
259-
}
260257
return nil
261258
}
262259
// add all of the mounts that are hanging off of it

chroot/run_common.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ func RunUsingChroot(spec *specs.Spec, bundlePath, homeDir string, stdin io.Reade
146146
}
147147

148148
logrus.Debugf("Running %#v in %#v", cmd.Cmd, cmd)
149-
confwg.Add(1)
150-
go func() {
149+
confwg.Go(func() {
151150
_, conferr = io.Copy(pwriter, bytes.NewReader(config))
152151
pwriter.Close()
153-
confwg.Done()
154-
}()
152+
})
155153
cmd.ExtraFiles = append([]*os.File{preader}, cmd.ExtraFiles...)
156154
err = cmd.Run()
157155
confwg.Wait()
@@ -541,12 +539,10 @@ func runUsingChroot(spec *specs.Spec, bundlePath string, ctty *os.File, stdin io
541539
}
542540

543541
logrus.Debugf("Running %#v in %#v", cmd.Cmd, cmd)
544-
confwg.Add(1)
545-
go func() {
542+
confwg.Go(func() {
546543
_, conferr = io.Copy(pwriter, bytes.NewReader(config))
547544
pwriter.Close()
548-
confwg.Done()
549-
}()
545+
})
550546
err = cmd.Run()
551547
confwg.Wait()
552548
signal.Stop(interrupted)

convertcw.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ func CWConvertImage(ctx context.Context, systemContext *types.SystemContext, sto
140140
}
141141
}()
142142
sourceInfo := GetBuildInfo(source)
143-
if err != nil {
144-
return "", nil, "", fmt.Errorf("retrieving info about source image: %w", err)
145-
}
146143
sourceImageID := sourceInfo.FromImageID
147144
sourceSize, err := store.ImageSize(sourceImageID)
148145
if err != nil {

copier/copier.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -837,20 +837,16 @@ func copierWithSubprocess(bulkReader io.Reader, bulkWriter io.Writer, req reques
837837
stdoutRead = nil
838838
var wg sync.WaitGroup
839839
var readError, writeError error
840-
wg.Add(1)
841-
go func() {
840+
wg.Go(func() {
842841
_, writeError = io.Copy(bulkWriter, bulkWriterRead)
843842
bulkWriterRead.Close()
844843
bulkWriterRead = nil
845-
wg.Done()
846-
}()
847-
wg.Add(1)
848-
go func() {
844+
})
845+
wg.Go(func() {
849846
_, readError = io.Copy(bulkReaderWrite, bulkReader)
850847
bulkReaderWrite.Close()
851848
bulkReaderWrite = nil
852-
wg.Done()
853-
}()
849+
})
854850
wg.Wait()
855851
cmdToWaitFor = nil
856852
if err = cmd.Wait(); err != nil {
@@ -1359,8 +1355,6 @@ func checkLinks(item string, req request, info os.FileInfo) (string, os.FileInfo
13591355
}
13601356

13611357
func copierHandlerGet(bulkWriter io.Writer, req request, pm *fileutils.PatternMatcher, idMappings *idtools.IDMappings) (*response, func() error, error) {
1362-
statRequest := req
1363-
statRequest.Request = requestStat
13641358
statResponse := copierHandlerStat(req, pm, idMappings)
13651359
errorResponse := func(fmtspec string, args ...any) (*response, func() error, error) {
13661360
return &response{Error: fmt.Sprintf(fmtspec, args...), Stat: statResponse.Stat, Get: getResponse{}}, nil, nil

copier/copier_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,10 @@ func testGetSingle(t *testing.T) {
798798
pipeReader, pipeWriter := io.Pipe()
799799
var getErr error
800800
var wg sync.WaitGroup
801-
wg.Add(1)
802-
go func() {
801+
wg.Go(func() {
803802
getErr = Get(root, topdir, getOptions, []string{name}, pipeWriter)
804803
pipeWriter.Close()
805-
wg.Done()
806-
}()
804+
})
807805
tr := tar.NewReader(pipeReader)
808806
hdr, err := tr.Next()
809807
for err == nil {
@@ -823,12 +821,10 @@ func testGetSingle(t *testing.T) {
823821
getOptions.StripSetgidBit = stripSetgidBit
824822
getOptions.StripStickyBit = stripStickyBit
825823
pipeReader, pipeWriter := io.Pipe()
826-
wg.Add(1)
827-
go func() {
824+
wg.Go(func() {
828825
getErr = Get(root, topdir, getOptions, []string{name}, pipeWriter)
829826
pipeWriter.Close()
830-
wg.Done()
831-
}()
827+
})
832828
tr := tar.NewReader(pipeReader)
833829
hdr, err := tr.Next()
834830
for err == nil {
@@ -1605,12 +1601,10 @@ func testGetMultiple(t *testing.T) {
16051601
pipeReader, pipeWriter := io.Pipe()
16061602
var getErr error
16071603
var wg sync.WaitGroup
1608-
wg.Add(1)
1609-
go func() {
1610-
defer wg.Done()
1604+
wg.Go(func() {
16111605
getErr = Get(root, topdir, getOptions, []string{testCase.pattern}, pipeWriter)
16121606
pipeWriter.Close()
1613-
}()
1607+
})
16141608
tr := tar.NewReader(pipeReader)
16151609
hdr, err := tr.Next()
16161610
actualContents := []string{}
@@ -3096,13 +3090,11 @@ func testChmod(t *testing.T) {
30963090

30973091
pipeReader, pipeWriter := io.Pipe()
30983092
var wg sync.WaitGroup
3099-
wg.Add(1)
3100-
go func() {
3093+
wg.Go(func() {
31013094
opts := GetOptions{Chmod: v.chmod, ChmodDirs: v.chmodDirs, ChmodFiles: v.chmodFiles}
31023095
err = Get(testDir, "", opts, []string{name}, pipeWriter)
31033096
pipeWriter.Close()
3104-
wg.Done()
3105-
}()
3097+
})
31063098
tr := tar.NewReader(pipeReader)
31073099
hdr, tarErr := tr.Next()
31083100
for tarErr == nil {

digester.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"hash"
88
"io"
9+
"strings"
910
"sync"
1011
"time"
1112

@@ -100,8 +101,7 @@ func newTarFilterer(writeCloser io.WriteCloser, filter func(hdr *tar.Header) (sk
100101
filterer := &tarFilterer{
101102
pipeWriter: pipeWriter,
102103
}
103-
filterer.wg.Add(1)
104-
go func() {
104+
filterer.wg.Go(func() {
105105
filterer.closedLock.Lock()
106106
closed := filterer.closed
107107
filterer.closedLock.Unlock()
@@ -161,8 +161,7 @@ func newTarFilterer(writeCloser io.WriteCloser, filter func(hdr *tar.Header) (sk
161161
} else {
162162
pipeReader.Close()
163163
}
164-
filterer.wg.Done()
165-
}()
164+
})
166165
return filterer
167166
}
168167

@@ -270,17 +269,17 @@ func (c *CompositeDigester) Digest() (string, digest.Digest) {
270269
case 1:
271270
return c.digesters[0].ContentType(), c.digesters[0].Digest()
272271
default:
273-
content := ""
272+
var content strings.Builder
274273
for i, digester := range c.digesters {
275274
if i > 0 {
276-
content += ","
275+
content.WriteString(",")
277276
}
278277
contentType := digester.ContentType()
279278
if contentType != "" {
280279
contentType += ":"
281280
}
282-
content += contentType + digester.Digest().Encoded()
281+
content.WriteString(contentType + digester.Digest().Encoded())
283282
}
284-
return "multi", digest.Canonical.FromString(content)
283+
return "multi", digest.Canonical.FromString(content.String())
285284
}
286285
}

digester_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ func TestTarFilterer(t *testing.T) {
280280
output := make(map[string]string)
281281
pipeReader, pipeWriter := io.Pipe()
282282
var wg sync.WaitGroup
283-
wg.Add(1)
284-
go func() {
283+
wg.Go(func() {
285284
tr := tar.NewReader(pipeReader)
286285
hdr, err := tr.Next()
287286
for err == nil {
@@ -295,8 +294,7 @@ func TestTarFilterer(t *testing.T) {
295294
}
296295
require.Equal(t, io.EOF, err, "unexpected error ended our tarstream read")
297296
pipeReader.Close()
298-
wg.Done()
299-
}()
297+
})
300298
filterer := newTarFilterer(pipeWriter, test.filter)
301299
_, err := io.Copy(filterer, &buffer)
302300
require.Nil(t, err, "unexpected error copying archive through filter to reader")

imagebuildah/executor.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,32 +600,34 @@ func (b *executor) buildStage(ctx context.Context, cleanupStages map[int]*stageE
600600
// processed like regular steps, and if no modification is done to
601601
// layers, its easier to reuse cached layers.
602602
if len(b.labels) > 0 {
603-
labelLine := "LABEL"
603+
var labelLine strings.Builder
604+
labelLine.WriteString("LABEL")
604605
for _, labelSpec := range b.labels {
605606
key, value, _ := strings.Cut(labelSpec, "=")
606607
// check only for an empty key since docker allows empty values
607608
if key != "" {
608-
labelLine += fmt.Sprintf(" %q=%q", key, value)
609+
fmt.Fprintf(&labelLine, " %q=%q", key, value)
609610
}
610611
}
611-
appendInstructions = slices.Concat(appendInstructions, []string{labelLine})
612+
appendInstructions = slices.Concat(appendInstructions, []string{labelLine.String()})
612613
}
613614
}
614615

615616
// If we were given environment variables to set via the API, add them as instructions
616617
// at the beginning of the stage so that they affect subsequent RUN instructions and
617618
// factor into the image history.
618619
if len(b.envs) > 0 {
619-
envLine := "ENV"
620+
var envLine strings.Builder
621+
envLine.WriteString("ENV")
620622
for _, envSpec := range b.envs {
621623
key, value, hasValue := strings.Cut(envSpec, "=")
622624
if hasValue {
623-
envLine += fmt.Sprintf(" %q=%q", key, value)
625+
fmt.Fprintf(&envLine, " %q=%q", key, value)
624626
} else {
625627
return "", nil, false, fmt.Errorf("BUG: unresolved environment variable: %q", key)
626628
}
627629
}
628-
prependInstructions = slices.Concat([]string{envLine}, prependInstructions)
630+
prependInstructions = slices.Concat([]string{envLine.String()}, prependInstructions)
629631
}
630632

631633
// Create stage labels for all stage images including final stage

0 commit comments

Comments
 (0)