From 098dcc58445cc20a9ca43008052f1765eae19eba Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Tue, 25 May 2021 15:33:25 -0400 Subject: [PATCH] Enforce no shadowed variable It does result in a few awkward case but I feel the trade off is still worth it overall. Change two errors to wrap with %w, will do more in the future. --- .github/workflows/test.yml | 3 +-- bcm283x/dma.go | 18 ++++++++---------- netlink/onewire.go | 2 +- pmem/alloc.go | 2 +- pmem/view.go | 8 ++++---- sysfs/fs_linux_test.go | 11 ++++++----- sysfs/gpio.go | 2 +- sysfs/spi.go | 6 +++--- 8 files changed, 25 insertions(+), 27 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ecddaa67..befb304e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -78,13 +78,12 @@ jobs: if: always() run: go vet ./... - name: 'Check: go vet shadow; shadowed variables' - # TODO: Remove true if we make this check ever pass. run: | SHADOW_TOOL="$(which shadow)" if [ -f "${SHADOW_TOOL}.exe" ]; then SHADOW_TOOL="${SHADOW_TOOL}.exe" fi - go vet -vettool=$SHADOW_TOOL ./... || true + go vet -vettool=$SHADOW_TOOL ./... - name: 'Check: golint' if: always() # TODO: Remove true if we make this check ever pass. diff --git a/bcm283x/dma.go b/bcm283x/dma.go index cad4bfc2..980bbcfb 100644 --- a/bcm283x/dma.go +++ b/bcm283x/dma.go @@ -711,7 +711,7 @@ func dmaWriteStreamPCM(p *Pin, w gpiostream.Stream) error { return err } defer buf.Close() - if err := copyStreamToDMABuf(w, buf.Uint32()); err != nil { + if err = copyStreamToDMABuf(w, buf.Uint32()); err != nil { return err } @@ -845,7 +845,7 @@ func dmaReadStream(p *Pin, b *gpiostream.BitStream) error { if err != nil { return err } - if _, err := setPWMClockSource(); err != nil { + if _, err = setPWMClockSource(); err != nil { return err } @@ -867,7 +867,7 @@ func dmaReadStream(p *Pin, b *gpiostream.BitStream) error { defer pCB.Close() reg := drvGPIO.gpioBaseAddr + 0x34 + uint32Size*uint32(p.number/32) // GPIO Pin Level 0 - if err := cb[0].initBlock(reg, uint32(buf.PhysAddr()), uint32(l), true, false, false, true, dmaPWM); err != nil { + if err = cb[0].initBlock(reg, uint32(buf.PhysAddr()), uint32(l), true, false, false, true, dmaPWM); err != nil { return err } err = runIO(pCB, l <= maxLite) @@ -953,7 +953,7 @@ func dmaWriteStreamEdges(p *Pin, w gpiostream.Stream) error { stride = uint32(skip) for i := 1; i < l; i++ { if v := getBit(bits[i/8], i%8, msb); v != last || stride == maxLite { - if err := cb[index].initBlock(physBit, dest[last], stride*uint32Size, false, true, false, false, dmaPWM); err != nil { + if err = cb[index].initBlock(physBit, dest[last], stride*uint32Size, false, true, false, false, dmaPWM); err != nil { return err } // It is not necessary to use physToUncachedPhys() here. @@ -964,13 +964,12 @@ func dmaWriteStreamEdges(p *Pin, w gpiostream.Stream) error { } stride += uint32(skip) } - if err := cb[index].initBlock(physBit, dest[last], stride*uint32Size, false, true, false, false, dmaPWM); err != nil { + if err = cb[index].initBlock(physBit, dest[last], stride*uint32Size, false, true, false, false, dmaPWM); err != nil { return err } // Start clock before DMA - _, err = setPWMClockSource() - if err != nil { + if _, err = setPWMClockSource(); err != nil { return err } return runIO(buf, true) @@ -1010,13 +1009,12 @@ func dmaWriteStreamDualChannel(p *Pin, w gpiostream.Stream) error { // Needs 64x the memory since each write is 2 full uint32. On the other // hand one could write 32 contiguous pins simultaneously at no cost. mask := uint32(1) << uint(p.number&31) - if err := raster32(w, skip, bufClear.Uint32(), bufSet.Uint32(), mask); err != nil { + if err = raster32(w, skip, bufClear.Uint32(), bufSet.Uint32(), mask); err != nil { return err } // Start clock before DMA start - _, err = setPWMClockSource() - if err != nil { + if _, err = setPWMClockSource(); err != nil { return err } diff --git a/netlink/onewire.go b/netlink/onewire.go index 324eacaa..814fe8ca 100644 --- a/netlink/onewire.go +++ b/netlink/onewire.go @@ -471,7 +471,7 @@ func (d *driver1W) Init() (bool, error) { // Find bus masters. m := &w1Msg{typ: msgListMasters} - if err := s.sendMsg(m.serialize(), 0); err != nil { + if err = s.sendMsg(m.serialize(), 0); err != nil { return false, fmt.Errorf("netlink-onewire: failed to send list bus msg: %v", err) } diff --git a/pmem/alloc.go b/pmem/alloc.go index b17ee800..5cdde3c4 100644 --- a/pmem/alloc.go +++ b/pmem/alloc.go @@ -102,7 +102,7 @@ func uallocMemLocked(size int) ([]byte, error) { for i := range b { b[i] = 0 } - if err := mlock(b); err != nil { + if err = mlock(b); err != nil { // Ignore the unmap error. _ = munmap(b) return nil, wrapf("locking %d bytes failed: %v", size, err) diff --git a/pmem/view.go b/pmem/view.go index c43af950..6728f8be 100644 --- a/pmem/view.go +++ b/pmem/view.go @@ -180,13 +180,13 @@ func mapGPIOLinux() (*View, error) { if gpioMemView == nil && gpioMemErr == nil { if f, err := openFile("/dev/gpiomem", os.O_RDWR|os.O_SYNC); err == nil { defer f.Close() - if i, err := mmap(f.Fd(), 0, pageSize); err == nil { + if i, err2 := mmap(f.Fd(), 0, pageSize); err2 == nil { gpioMemView = &View{Slice: i, orig: i, phys: 0} } else { - gpioMemErr = wrapf("failed to memory map in user space GPIO memory: %v", err) + gpioMemErr = wrapf("failed to memory map in user space GPIO memory: %w", err2) } } else { - gpioMemErr = wrapf("failed to open GPIO memory: %v", err) + gpioMemErr = wrapf("failed to open GPIO memory: %w", err) } } return gpioMemView, gpioMemErr @@ -202,7 +202,7 @@ func mapLinux(base uint64, size int) (*View, error) { offset := int(base & 0xFFF) i, err := mmap(f.Fd(), int64(base&^0xFFF), (size+offset+0xFFF)&^0xFFF) if err != nil { - return nil, wrapf("mapping at 0x%x failed: %v", base, err) + return nil, wrapf("mapping at 0x%x failed: %w", base, err) } return &View{Slice: i[offset : offset+size], orig: i, phys: base + uint64(offset)}, nil } diff --git a/sysfs/fs_linux_test.go b/sysfs/fs_linux_test.go index 27b50bb0..af7e3ee0 100644 --- a/sysfs/fs_linux_test.go +++ b/sysfs/fs_linux_test.go @@ -45,8 +45,8 @@ func TestAddFd_File(t *testing.T) { t.Fatal(err) } defer func() { - if err := os.Remove(f.Name()); err != nil { - t.Fatal(err) + if err2 := os.Remove(f.Name()); err2 != nil { + t.Fatal(err2) } }() @@ -73,7 +73,7 @@ func TestManual_Listen_Pipe(t *testing.T) { c := make(chan time.Time) // Pipes do not support epollPRI, so use epollIN instead. const flags = epollET | epollIN - if err := ev.addFd(r.Fd(), c, flags); err != nil { + if err = ev.addFd(r.Fd(), c, flags); err != nil { t.Fatal(err) } notExpectChan(t, c, "should not have produced an event") @@ -86,7 +86,8 @@ func TestManual_Listen_Pipe(t *testing.T) { notExpectChan(t, c, "should have produced a single event") buf := [8]byte{} - if n, err := r.Read(buf[:]); err != nil { + n := 0 + if n, err = r.Read(buf[:]); err != nil { t.Fatal(err) } else if n != 3 { t.Fatal("expected foo") @@ -108,7 +109,7 @@ func TestManual_Listen_Pipe(t *testing.T) { default: } - if n, err := r.Read(buf[:]); err != nil { + if n, err = r.Read(buf[:]); err != nil { t.Fatal(err) } else if n != 6 { t.Fatal("expected foo") diff --git a/sysfs/gpio.go b/sysfs/gpio.go index 9894ab7e..7e947ad2 100644 --- a/sysfs/gpio.go +++ b/sysfs/gpio.go @@ -461,7 +461,7 @@ func (d *driverGPIO) Init() (bool, error) { // of an array. Pins = map[int]*Pin{} for _, item := range items { - if err := d.parseGPIOChip(item + "/"); err != nil { + if err = d.parseGPIOChip(item + "/"); err != nil { return true, err } } diff --git a/sysfs/spi.go b/sysfs/spi.go index 302c23ec..b45681ae 100644 --- a/sysfs/spi.go +++ b/sysfs/spi.go @@ -548,9 +548,9 @@ func (d *driverSPI) Init() (bool, error) { // Do not use "/sys/bus/spi/devices/spi" as Raspbian's provided udev rules // only modify the ACL of /dev/spidev* but not the ones in /sys/bus/... prefix := "/dev/spidev" - items, err := filepath.Glob(prefix + "*") - if err != nil { - return true, err + items, err2 := filepath.Glob(prefix + "*") + if err2 != nil { + return true, err2 } if len(items) == 0 { return false, errors.New("no SPI port found")