Skip to content

Commit 76adb8f

Browse files
authored
Merge pull request moby#49894 from thaJeztah/daemon_less_output_vars
daemon/*: reduce named (error)-returns, naked returns, and some minor linting-fixes
2 parents fa23123 + 19ccb75 commit 76adb8f

29 files changed

+121
-122
lines changed

daemon/archive.go

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

1111
// ContainerStatPath stats the filesystem resource at the specified path in the
1212
// container identified by the given name.
13-
func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *container.PathStat, err error) {
13+
func (daemon *Daemon) ContainerStatPath(name string, path string) (*container.PathStat, error) {
1414
ctr, err := daemon.GetContainer(name)
1515
if err != nil {
1616
return nil, err
1717
}
1818

19-
stat, err = daemon.containerStatPath(ctr, path)
19+
stat, err := daemon.containerStatPath(ctr, path)
2020
if err != nil {
2121
if os.IsNotExist(err) {
2222
return nil, containerFileNotFound{path, name}
@@ -33,7 +33,7 @@ func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *contain
3333
// ContainerArchivePath creates an archive of the filesystem resource at the
3434
// specified path in the container identified by the given name. Returns a
3535
// tar archive of the resource and whether it was a directory or a single file.
36-
func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *container.PathStat, err error) {
36+
func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *container.PathStat, _ error) {
3737
ctr, err := daemon.GetContainer(name)
3838
if err != nil {
3939
return nil, nil, err

daemon/archive_unix.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
// containerStatPath stats the filesystem resource at the specified path in this
2222
// container. Returns stat info about the resource.
23-
func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *containertypes.PathStat, err error) {
23+
func (daemon *Daemon) containerStatPath(container *container.Container, path string) (*containertypes.PathStat, error) {
2424
container.Lock()
2525
defer container.Unlock()
2626

@@ -36,11 +36,11 @@ func (daemon *Daemon) containerStatPath(container *container.Container, path str
3636
// containerArchivePath creates an archive of the filesystem resource at the specified
3737
// path in this container. Returns a tar archive of the resource and stat info
3838
// about the resource.
39-
func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *containertypes.PathStat, err error) {
39+
func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *containertypes.PathStat, retErr error) {
4040
container.Lock()
4141

4242
defer func() {
43-
if err != nil {
43+
if retErr != nil {
4444
// Wait to unlock the container until the archive is fully read
4545
// (see the ReadCloseWrapper func below) or if there is an error
4646
// before that occurs.
@@ -54,8 +54,8 @@ func (daemon *Daemon) containerArchivePath(container *container.Container, path
5454
}
5555

5656
defer func() {
57-
if err != nil {
58-
cfs.Close()
57+
if retErr != nil {
58+
_ = cfs.Close()
5959
}
6060
}()
6161

daemon/archive_windows.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// containerStatPath stats the filesystem resource at the specified path in this
1919
// container. Returns stat info about the resource.
20-
func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *containertypes.PathStat, err error) {
20+
func (daemon *Daemon) containerStatPath(container *container.Container, path string) (*containertypes.PathStat, error) {
2121
container.Lock()
2222
defer container.Unlock()
2323

@@ -26,12 +26,12 @@ func (daemon *Daemon) containerStatPath(container *container.Container, path str
2626
return nil, err
2727
}
2828

29-
if err = daemon.Mount(container); err != nil {
29+
if err := daemon.Mount(container); err != nil {
3030
return nil, err
3131
}
3232
defer daemon.Unmount(container)
3333

34-
err = daemon.mountVolumes(container)
34+
err := daemon.mountVolumes(container)
3535
defer container.DetachAndUnmount(daemon.LogVolumeEvent)
3636
if err != nil {
3737
return nil, err
@@ -51,11 +51,11 @@ func (daemon *Daemon) containerStatPath(container *container.Container, path str
5151
// containerArchivePath creates an archive of the filesystem resource at the specified
5252
// path in this container. Returns a tar archive of the resource and stat info
5353
// about the resource.
54-
func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *containertypes.PathStat, err error) {
54+
func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *containertypes.PathStat, retErr error) {
5555
container.Lock()
5656

5757
defer func() {
58-
if err != nil {
58+
if retErr != nil {
5959
// Wait to unlock the container until the archive is fully read
6060
// (see the ReadCloseWrapper func below) or if there is an error
6161
// before that occurs.
@@ -68,20 +68,20 @@ func (daemon *Daemon) containerArchivePath(container *container.Container, path
6868
return nil, nil, err
6969
}
7070

71-
if err = daemon.Mount(container); err != nil {
71+
if err := daemon.Mount(container); err != nil {
7272
return nil, nil, err
7373
}
7474

7575
defer func() {
76-
if err != nil {
76+
if retErr != nil {
7777
// unmount any volumes
7878
container.DetachAndUnmount(daemon.LogVolumeEvent)
7979
// unmount the container's rootfs
8080
daemon.Unmount(container)
8181
}
8282
}()
8383

84-
if err = daemon.mountVolumes(container); err != nil {
84+
if err := daemon.mountVolumes(container); err != nil {
8585
return nil, nil, err
8686
}
8787

@@ -223,14 +223,14 @@ func (daemon *Daemon) containerExtractToDir(container *container.Container, path
223223
return nil
224224
}
225225

226-
func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
226+
func (daemon *Daemon) containerCopy(container *container.Container, resource string) (_ io.ReadCloser, retErr error) {
227227
if resource[0] == '/' || resource[0] == '\\' {
228228
resource = resource[1:]
229229
}
230230
container.Lock()
231231

232232
defer func() {
233-
if err != nil {
233+
if retErr != nil {
234234
// Wait to unlock the container until the archive is fully read
235235
// (see the ReadCloseWrapper func below) or if there is an error
236236
// before that occurs.
@@ -248,7 +248,7 @@ func (daemon *Daemon) containerCopy(container *container.Container, resource str
248248
}
249249

250250
defer func() {
251-
if err != nil {
251+
if retErr != nil {
252252
// unmount any volumes
253253
container.DetachAndUnmount(daemon.LogVolumeEvent)
254254
// unmount the container's rootfs

daemon/cluster/controllers/plugin/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (p *Controller) Update(ctx context.Context, t *api.Task) error {
8787
}
8888

8989
// Prepare is the prepare phase from swarmkit
90-
func (p *Controller) Prepare(ctx context.Context) (err error) {
90+
func (p *Controller) Prepare(ctx context.Context) (retErr error) {
9191
p.logger.Debug("Prepare")
9292

9393
remote, err := reference.ParseNormalizedNamed(p.spec.Remote)
@@ -105,7 +105,7 @@ func (p *Controller) Prepare(ctx context.Context) (err error) {
105105
pl, err := p.backend.Get(p.spec.Name)
106106

107107
defer func() {
108-
if pl != nil && err == nil {
108+
if pl != nil && retErr == nil {
109109
pl.Acquire()
110110
}
111111
}()

daemon/container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *
226226

227227
// verifyContainerSettings performs validation of the hostconfig and config
228228
// structures.
229-
func (daemon *Daemon) verifyContainerSettings(daemonCfg *configStore, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, err error) {
229+
func (daemon *Daemon) verifyContainerSettings(daemonCfg *configStore, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, _ error) {
230230
// First perform verification of settings common across all platforms.
231-
if err = validateContainerConfig(config); err != nil {
232-
return warnings, err
231+
if err := validateContainerConfig(config); err != nil {
232+
return nil, err
233233
}
234234

235235
warns, err := validateHostConfig(hostConfig)

daemon/container_operations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,12 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
761761
if !ctr.Managed {
762762
// add container name/alias to DNS
763763
if err := daemon.ActivateContainerServiceBinding(ctr.Name); err != nil {
764-
return fmt.Errorf("Activate container service binding for %s failed: %v", ctr.Name, err)
764+
return fmt.Errorf("activate container service binding for %s failed: %v", ctr.Name, err)
765765
}
766766
}
767767

768768
if err := updateJoinInfo(ctr.NetworkSettings, n, ep); err != nil {
769-
return fmt.Errorf("Updating join info failed: %v", err)
769+
return fmt.Errorf("updating join info failed: %v", err)
770770
}
771771

772772
ctr.NetworkSettings.Ports = getPortMapInfo(sb)

daemon/containerd/image_delete.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force,
203203
// also deletes dangling parents if there is no conflict in doing so.
204204
// Parent images are removed quietly, and if there is any issue/conflict
205205
// it is logged but does not halt execution/an error is not returned.
206-
func (i *ImageService) deleteAll(ctx context.Context, imgID image.ID, all []c8dimages.Image, c conflictType, prune bool) (records []imagetypes.DeleteResponse, err error) {
206+
func (i *ImageService) deleteAll(ctx context.Context, imgID image.ID, all []c8dimages.Image, c conflictType, prune bool) (records []imagetypes.DeleteResponse, _ error) {
207207
// Workaround for: https://github.com/moby/buildkit/issues/3797
208208
possiblyDeletedConfigs := map[digest.Digest]struct{}{}
209209
if len(all) > 0 && i.content != nil {
@@ -236,6 +236,7 @@ func (i *ImageService) deleteAll(ctx context.Context, imgID image.ID, all []c8di
236236
var parents []c8dimages.Image
237237
if prune {
238238
// TODO(dmcgowan): Consider using GC labels to walk for deletion
239+
var err error
239240
parents, err = i.parents(ctx, imgID)
240241
if err != nil {
241242
log.G(ctx).WithError(err).Warn("failed to get image parents")
@@ -254,8 +255,7 @@ func (i *ImageService) deleteAll(ctx context.Context, imgID image.ID, all []c8di
254255
if !isDanglingImage(parent) {
255256
break
256257
}
257-
err = i.imageDeleteHelper(ctx, parent, all, &records, conflictSoft)
258-
if err != nil {
258+
if err := i.imageDeleteHelper(ctx, parent, all, &records, conflictSoft); err != nil {
259259
log.G(ctx).WithError(err).Warn("failed to remove image parent")
260260
break
261261
}

daemon/containerd/image_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ func setupLabelFilter(ctx context.Context, store content.Store, fltrs filters.Ar
654654
// It will be returned once a matching config is found.
655655
errFoundConfig := errors.New("success, found matching config")
656656

657-
err := c8dimages.Dispatch(ctx, presentChildrenHandler(store, c8dimages.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) (subdescs []ocispec.Descriptor, err error) {
657+
err := c8dimages.Dispatch(ctx, presentChildrenHandler(store, c8dimages.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) (subdescs []ocispec.Descriptor, _ error) {
658658
if !c8dimages.IsConfigType(desc.MediaType) {
659659
return nil, nil
660660
}

daemon/daemon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ func (daemon *Daemon) IsSwarmCompatible() error {
760760

761761
// NewDaemon sets up everything for the daemon to be able to service
762762
// requests from the webserver.
763-
func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.Store, authzMiddleware *authorization.Middleware) (daemon *Daemon, err error) {
763+
func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.Store, authzMiddleware *authorization.Middleware) (_ *Daemon, retErr error) {
764764
// Verify platform-specific requirements.
765765
// TODO(thaJeztah): this should be called before we try to create the daemon; perhaps together with the config validation.
766766
if err := checkSystem(); err != nil {
@@ -841,7 +841,7 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
841841
// Ensure the daemon is properly shutdown if there is a failure during
842842
// initialization
843843
defer func() {
844-
if err != nil {
844+
if retErr != nil {
845845
// Use a fresh context here. Passed context could be cancelled.
846846
if err := d.Shutdown(context.Background()); err != nil {
847847
log.G(ctx).Error(err)

daemon/daemon_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func adaptSharedNamespaceContainer(daemon containerGetter, hostConfig *container
413413
}
414414

415415
// verifyPlatformContainerResources performs platform-specific validation of the container's resource-configuration
416-
func verifyPlatformContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo, update bool) (warnings []string, err error) {
416+
func verifyPlatformContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo, update bool) (warnings []string, _ error) {
417417
fixMemorySwappiness(resources)
418418

419419
// memory subsystem checks and adjustments
@@ -650,7 +650,7 @@ func isRunningSystemd() bool {
650650

651651
// verifyPlatformContainerSettings performs platform-specific validation of the
652652
// hostconfig and config structures.
653-
func verifyPlatformContainerSettings(daemon *Daemon, daemonCfg *configStore, hostConfig *containertypes.HostConfig, update bool) (warnings []string, err error) {
653+
func verifyPlatformContainerSettings(daemon *Daemon, daemonCfg *configStore, hostConfig *containertypes.HostConfig, update bool) (warnings []string, _ error) {
654654
if hostConfig == nil {
655655
return nil, nil
656656
}

0 commit comments

Comments
 (0)