Skip to content

Commit

Permalink
Enforce no shadowed variable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maruel committed May 25, 2021
1 parent 828523e commit 098dcc5
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 27 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 8 additions & 10 deletions bcm283x/dma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion netlink/onewire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pmem/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pmem/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions sysfs/fs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion sysfs/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions sysfs/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 098dcc5

Please sign in to comment.