Skip to content

Commit

Permalink
Reduce number of errcheck lint messages from 117 to 2.
Browse files Browse the repository at this point in the history
- Ran github.com/kisielk/errcheck on the code base, and either trapped errors or
  marked them as ignored explicitly. The stats was calculated via:
    errcheck ./... | grep -v defer | wc -l
- Stop registering gpio.INVALID, it can't be registered.
- Fix a go vet issue in cap1188.
  • Loading branch information
maruel committed Dec 22, 2017
1 parent 9d428d9 commit 67f6a11
Show file tree
Hide file tree
Showing 45 changed files with 350 additions and 227 deletions.
5 changes: 4 additions & 1 deletion cmd/gpio-read/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func mainImpl() error {
if *edges {
for {
p.WaitForEdge(-1)
printLevel(p.Read())
if err := printLevel(p.Read()); err != nil {
// Do not return an error on pipe fail, just exit.
return nil
}
}
}
return printLevel(p.Read())
Expand Down
15 changes: 12 additions & 3 deletions cmd/ir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ func mainImpl() error {
return nil
}
if msg.Repeat {
os.Stdout.Write([]byte("*"))
if _, err := os.Stdout.Write([]byte("*")); err != nil {
// Do not return an error on pipe fail, just exit.
return nil
}
} else {
if first {
first = false
} else {
os.Stdout.Write([]byte("\n"))
if _, err := os.Stdout.Write([]byte("\n")); err != nil {
// Do not return an error on pipe fail, just exit.
return nil
}
}
if _, err := fmt.Printf("%s %s ", msg.RemoteType, msg.Key); err != nil {
// Do not return an error on pipe fail, just exit.
return nil
}
fmt.Printf("%s %s ", msg.RemoteType, msg.Key)
}
case <-ctrlC:
return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/periph-smoketest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ var tests = []SmokeTest{
}

func usage(fs *flag.FlagSet) {
io.WriteString(os.Stderr, "Usage: periph-smoketest <args> <name> ...\n\n")
_, _ = io.WriteString(os.Stderr, "Usage: periph-smoketest <args> <name> ...\n\n")
fs.PrintDefaults()
io.WriteString(os.Stderr, "\nTests available:\n")
_, _ = io.WriteString(os.Stderr, "\nTests available:\n")
names := make([]string, len(tests))
desc := make(map[string]string, len(tests))
l := 0
Expand Down Expand Up @@ -96,7 +96,7 @@ func mainImpl() error {
}
if fs.NArg() == 0 {
fs.Usage()
io.WriteString(os.Stdout, "\n")
_, _ = io.WriteString(os.Stdout, "\n")
return errors.New("please specify a test to run or use -help")
}
cmd := fs.Arg(0)
Expand Down
8 changes: 4 additions & 4 deletions conn/conntest/conntest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestPlayback_Close_panic(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}()
p.Close()
_ = p.Close()
t.Fatal("shouldn't run")
}

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestPlayback_Tx_panic_count(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}()
p.Tx([]byte{0}, nil)
_ = p.Tx([]byte{0}, nil)
t.Fatal("shouldn't run")
}

Expand All @@ -154,7 +154,7 @@ func TestPlayback_Tx_panic_write(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}()
p.Tx([]byte{0}, nil)
_ = p.Tx([]byte{0}, nil)
t.Fatal("shouldn't run")
}

Expand All @@ -170,7 +170,7 @@ func TestPlayback_Tx_panic_read(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}()
p.Tx(nil, []byte{0, 1})
_ = p.Tx(nil, []byte{0, 1})
t.Fatal("shouldn't run")
}

Expand Down
4 changes: 0 additions & 4 deletions conn/gpio/gpioreg/gpioreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,3 @@ type pinList []gpio.PinIO
func (p pinList) Len() int { return len(p) }
func (p pinList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p pinList) Less(i, j int) bool { return p[i].Number() < p[j].Number() }

func init() {
Register(gpio.INVALID, true)
}
4 changes: 3 additions & 1 deletion conn/gpio/gpiosmoketest/gpiosmoketest.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) error {
pin2 := f.String("pin2", "", "second pin to use")
slow := f.Bool("s", false, "slow; insert a second between each step")
useSysfs := f.Bool("sysfs", false, "force the use of sysfs")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
Expand Down
4 changes: 2 additions & 2 deletions conn/gpio/gpiotest/gpiotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func (p *Pin) Read() gpio.Level {
// WaitForEdge implements gpio.PinIn.
func (p *Pin) WaitForEdge(timeout time.Duration) bool {
if timeout == -1 {
p.Out(<-p.EdgesChan)
_ = p.Out(<-p.EdgesChan)
return true
}
select {
case <-time.After(timeout):
return false
case l := <-p.EdgesChan:
p.Out(l)
_ = p.Out(l)
return true
}
}
Expand Down
10 changes: 6 additions & 4 deletions conn/i2c/i2creg/i2creg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func ExampleAll() {

func ExampleOpen() {
// On linux, the following calls will likely open the same bus.
Open("/dev/i2c-1")
Open("I2C1")
Open("1")
_, _ = Open("/dev/i2c-1")
_, _ = Open("I2C1")
_, _ = Open("1")

// How a command line tool may let the user choose an I²C bus, yet default to
// the first bus known.
Expand All @@ -57,7 +57,9 @@ func ExampleOpen() {
}
defer b.Close()
// Use b...
b.Tx(23, []byte("cmd"), nil)
if err := b.Tx(23, []byte("cmd"), nil); err != nil {
log.Fatal(err)
}
}

//
Expand Down
16 changes: 12 additions & 4 deletions conn/i2c/i2csmoketest/i2csmoketest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) error {
i2cID := f.String("bus", "", "I²C bus to use")
wc := f.String("wc", "", "gpio pin for EEPROM write-control pin")
seed := f.Int64("seed", 0, "random number seed, default is to use the time")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
Expand Down Expand Up @@ -159,9 +161,13 @@ func (s *SmokeTest) eeprom(bus i2c.Bus, wcPin gpio.PinIO) error {
return fmt.Errorf("eeprom: cannot init WC control pin: %v", err)
}
time.Sleep(time.Millisecond)
wcPin.Out(gpio.High)
if err := wcPin.Out(gpio.High); err != nil {
return err
}
time.Sleep(time.Millisecond)
wcPin.Out(gpio.Low)
if err := wcPin.Out(gpio.Low); err != nil {
return err
}

// Pick a byte in the first 256 bytes and try to write it and read it back a couple
// of times. Using a random byte for "wear leveling"...
Expand Down Expand Up @@ -223,7 +229,9 @@ func (s *SmokeTest) eeprom(bus i2c.Bus, wcPin gpio.PinIO) error {

// Disable write-control, attempt a write, and expect to get an i2c error.
// TODO: create a clearly identifiable error.
wcPin.Out(gpio.High)
if err := wcPin.Out(gpio.High); err != nil {
return err
}
if err := d.WriteUint8(0x10, 0xA5); err == nil {
return errors.New("eeprom: write with write-control disabled didn't return an error")
}
Expand Down
2 changes: 1 addition & 1 deletion conn/i2c/i2ctest/i2ctest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestPlayback_Close_panic(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}()
p.Close()
_ = p.Close()
t.Fatal("shouldn't run")
}

Expand Down
4 changes: 3 additions & 1 deletion conn/onewire/onewiresmoketest/onewiresmoketest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (s *SmokeTest) Description() string {
func (s *SmokeTest) Run(f *flag.FlagSet, args []string) error {
busName := f.String("i2cbus", "", "I²C bus name for the DS2483 1-wire interface chip")
seed := f.Int64("seed", 0, "random number seed, default is to use the time")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
Expand Down
2 changes: 1 addition & 1 deletion conn/onewire/onewiretest/onewiretest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestPlayback_Close_panic(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}()
p.Close()
_ = p.Close()
t.Fatal("shouldn't run")
}

Expand Down
10 changes: 6 additions & 4 deletions conn/spi/spireg/spireg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func ExampleAll() {

func ExampleOpen() {
// On linux, the following calls will likely open the same port.
Open("/dev/spidev1.0")
Open("SPI1.0")
Open("1")
_, _ = Open("/dev/spidev1.0")
_, _ = Open("SPI1.0")
_, _ = Open("1")

// How a command line tool may let the user choose a SPI port, yet
// default to the first port known.
Expand All @@ -67,7 +67,9 @@ func ExampleOpen() {
log.Fatal(err)
}
// Use b...
c.Tx([]byte("cmd"), nil)
if err := c.Tx([]byte("cmd"), nil); err != nil {
log.Fatal(err)
}
}

//
Expand Down
24 changes: 18 additions & 6 deletions conn/spi/spismoketest/spismoketest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) error {
spiID := f.String("spi", "", "SPI port to use")
wp := f.String("wp", "", "gpio pin for EEPROM write-protect")
seed := f.Int64("seed", 0, "random number seed, default is to use the time")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
Expand Down Expand Up @@ -153,13 +155,19 @@ func (s *SmokeTest) eeprom(d spi.Conn, wpPin gpio.PinIO) error {
log.Printf("%s: writing&reading EEPROM byte %#x", s, addr[1])
for _, v := range values {
// Write byte.
d.Tx([]byte{cmdWriteEnable}, rBuf[:1])
d.Tx([]byte{cmdWriteMemory, addr[0], addr[1], v}, rBuf[:4])
if err := d.Tx([]byte{cmdWriteEnable}, rBuf[:1]); err != nil {
return err
}
if err := d.Tx([]byte{cmdWriteMemory, addr[0], addr[1], v}, rBuf[:4]); err != nil {
return err
}
// Read byte back after the chip is ready.
if err := waitReady(d); err != nil {
return err
}
d.Tx([]byte{cmdReadMemory, addr[0], addr[1], 0}, rBuf[:4])
if err := d.Tx([]byte{cmdReadMemory, addr[0], addr[1], 0}, rBuf[:4]); err != nil {
return err
}
if rBuf[3] != v {
return fmt.Errorf("eeprom: wrote %#x but got %#v back", v, rBuf[3])
}
Expand Down Expand Up @@ -205,7 +213,9 @@ func (s *SmokeTest) eeprom(d spi.Conn, wpPin gpio.PinIO) error {
}

// Set write-protect, attempt a write, and expect it not to happen.
wpPin.Out(gpio.Low)
if err := wpPin.Out(gpio.Low); err != nil {
return err
}
if err := d.Tx([]byte{0x10, 0xA5}, nil); err == nil {
return errors.New("eeprom: write with write-control disabled didn't return an error")
}
Expand Down Expand Up @@ -236,7 +246,9 @@ func (s *SmokeTest) eeprom(d spi.Conn, wpPin gpio.PinIO) error {
func waitReady(d spi.Conn) error {
for start := time.Now(); time.Since(start) <= 100*time.Millisecond; {
var rBuf [2]byte
d.Tx([]byte{cmdReadStatus, 0}, rBuf[:])
if err := d.Tx([]byte{cmdReadStatus, 0}, rBuf[:]); err != nil {
return err
}
if rBuf[1]&1 == 0 {
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion devices/bmxx80/bmx280smoketest/bmx280smoketest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
i2cAddr := f.Uint("ia", 0x76, "I²C bus address to use; either 0x76 (BMx280, the default) or 0x77 (BMP180)")
spiID := f.String("spi", "", "SPI port to use")
record := f.Bool("r", false, "record operation (for playback unit testing)")
f.Parse(args)
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
Expand Down
Loading

0 comments on commit 67f6a11

Please sign in to comment.