Skip to content

Commit

Permalink
Fix(build): linter findings of "gosimple", "govet" and "staticcheck" (h…
Browse files Browse the repository at this point in the history
…ybridgroup#917)

Fix also encoder and dps overflow in gopigo3 "GetMotorStatus()". Problem found by "staticcheck".
  • Loading branch information
gen2thomas authored May 19, 2023
1 parent 6f8cff5 commit fcdf286
Show file tree
Hide file tree
Showing 72 changed files with 233 additions and 253 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ jobs:
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
# mostly there is no problem locally, but on server: "could not import C (cgo preprocessing failed) (typecheck)"
args: --skip-files platforms/digispark/littleWire.go

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
Expand Down
24 changes: 21 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,26 @@ linters:
# all issues step by step to enable at least the default linters
disable:
- errcheck
- gosimple
#- govet
- ineffassign
- staticcheck
#- staticcheck
- unused

enable:
- nolintlint

linters-settings:
nolintlint:
# Enable to require an explanation of nonzero length after each nolint directive.
# Default: false
require-explanation: true
# Enable to require nolint directives to mention the specific linter being suppressed.
# Default: false
require-specific: true

revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unexported-return
# disable this rule, because sometimes it has its justification
- name: unexported-return
severity: warning
disabled: true
6 changes: 1 addition & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ func (a *API) Start() {

// StartWithoutDefaults initializes the api without setting up the default routes.
// Good for custom web interfaces.
//
func (a *API) StartWithoutDefaults() {
a.start(a)
}

// AddC3PIORoutes adds all of the standard C3PIO routes to the API.
// For more information, please see:
// http://cppp.io/
//
func (a *API) AddC3PIORoutes() {
mcpCommandRoute := "/api/commands/:command"
robotDeviceCommandRoute := "/api/robots/:robot/devices/:device/commands/:command"
Expand Down Expand Up @@ -250,10 +248,8 @@ func (a *API) robotDevice(res http.ResponseWriter, req *http.Request) {

func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
f, _ := res.(http.Flusher)
c, _ := res.(http.CloseNotifier)

dataChan := make(chan string)
closer := c.CloseNotify()

res.Header().Set("Content-Type", "text/event-stream")
res.Header().Set("Cache-Control", "no-cache")
Expand All @@ -275,7 +271,7 @@ func (a *API) robotDeviceEvent(res http.ResponseWriter, req *http.Request) {
case data := <-dataChan:
fmt.Fprintf(res, "data: %v\n\n", data)
f.Flush()
case <-closer:
case <-req.Context().Done():
log.Println("Closing connection")
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestIndex(t *testing.T) {
a.ServeHTTP(response, request)

gobottest.Assert(t, http.StatusMovedPermanently, response.Code)
gobottest.Assert(t, "/index.html", response.HeaderMap["Location"][0])
gobottest.Assert(t, "/index.html", response.Header()["Location"][0])
}

func TestMcp(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Generate() cli.Command {
valid = true
}
}
if valid == false {
if !valid {
fmt.Println("Invalid/no subcommand supplied.")
fmt.Println("Usage:")
fmt.Println(" gobot generate adaptor <name> [package] # generate a new Gobot adaptor")
Expand Down Expand Up @@ -102,7 +102,7 @@ func generate(c config, file string, tmpl string) error {
fmt.Println("Creating", fileLocation)

f, err := os.Create(fileLocation)
defer f.Close()
defer f.Close() //nolint:staticcheck // for historical reasons
if err != nil {
return err
}
Expand Down Expand Up @@ -149,11 +149,11 @@ func generatePlatform(c config) error {

c.dir = dir

if exp, err := ioutil.ReadFile(exampleDir + "/main.go"); err != nil {
exp, err := ioutil.ReadFile(exampleDir + "/main.go")
if err != nil {
return err
} else {
c.Example = string(exp)
}
c.Example = string(exp)

return generate(c, "README.md", readme())
}
Expand Down
7 changes: 3 additions & 4 deletions commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ func NewCommander() Commander {
}
}

// Command returns the command interface whene passed a valid command name
func (c *commander) Command(name string) (command func(map[string]interface{}) interface{}) {
command, _ = c.commands[name]
return
// Command returns the command interface when passed a valid command name
func (c *commander) Command(name string) func(map[string]interface{}) interface{} {
return c.commands[name]
}

// Commands returns the entire map of valid commands
Expand Down
3 changes: 3 additions & 0 deletions drivers/common/mfrc522/mfrc522_pcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ func (d *MFRC522Common) writeFifo(fifoData []byte) error {

func (d *MFRC522Common) readFifo(backData []byte) (uint8, error) {
n, err := d.readByteData(regFIFOLevel) // Number of bytes in the FIFO
if err != nil {
return 0, err
}
if n > uint8(len(backData)) {
return 0, fmt.Errorf("more data in FIFO (%d) than expected (%d)", n, len(backData))
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/gpio/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestButtonDriver(t *testing.T) {
}

func TestButtonDriverStart(t *testing.T) {
sem := make(chan bool, 0)
sem := make(chan bool)
a := newGpioTestAdaptor()
d := NewButtonDriver(a, "1")

Expand Down Expand Up @@ -107,7 +107,7 @@ func TestButtonDriverStart(t *testing.T) {
}

func TestButtonDriverDefaultState(t *testing.T) {
sem := make(chan bool, 0)
sem := make(chan bool)
a := newGpioTestAdaptor()
d := NewButtonDriver(a, "1")
d.DefaultState = 1
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpio/pir_motion_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPIRMotionDriver(t *testing.T) {
}

func TestPIRMotionDriverStart(t *testing.T) {
sem := make(chan bool, 0)
sem := make(chan bool)
a := newGpioTestAdaptor()
d := NewPIRMotionDriver(a, "1")

Expand Down
2 changes: 1 addition & 1 deletion drivers/gpio/servo_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *ServoDriver) Halt() (err error) { return }

// Move sets the servo to the specified angle. Acceptable angles are 0-180
func (s *ServoDriver) Move(angle uint8) (err error) {
if !(angle >= 0 && angle <= 180) {
if angle > 180 {
return ErrServoOutOfRange
}
s.CurrentAngle = angle
Expand Down
6 changes: 3 additions & 3 deletions drivers/gpio/stepper_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *StepperDriver) Start() (err error) { return }
// Run continuously runs the stepper
func (s *StepperDriver) Run() (err error) {
//halt if already moving
if s.moving == true {
if s.moving {
s.Halt()
}

Expand All @@ -123,7 +123,7 @@ func (s *StepperDriver) Run() (err error) {

go func() {
for {
if s.moving == false {
if !s.moving {
break
}
s.step()
Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *StepperDriver) Move(stepsToMove int) error {
return s.Halt()
}

if s.moving == true {
if s.moving {
//stop previous motion
s.Halt()
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpio/stepper_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestStepperDriverDefaultDirection(t *testing.T) {
func TestStepperDriverInvalidDirection(t *testing.T) {
d := initStepperMotorDriver()
err := d.SetDirection("reverse")
gobottest.Assert(t, err.(error), errors.New("Invalid direction. Value should be forward or backward"))
gobottest.Assert(t, err, errors.New("Invalid direction. Value should be forward or backward"))
}

func TestStepperDriverMoveForward(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions drivers/i2c/ads1x15_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,12 @@ func (d *ADS1x15Driver) AnalogRead(pin string) (value int, err error) {
switch pin {
case "0-1":
channel = 0
break
case "0-3":
channel = 1
break
case "1-3":
channel = 2
break
case "2-3":
channel = 3
break
default:
// read the voltage at a specific pin, compared to the ground
channel, err = strconv.Atoi(pin)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ads1x15_driver_1015_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestADS1015AnalogRead(t *testing.T) {
gobottest.Assert(t, val, 32767)
gobottest.Assert(t, err, nil)

val, err = d.AnalogRead("3-2")
_, err = d.AnalogRead("3-2")
gobottest.Refute(t, err.Error(), nil)
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ads1x15_driver_1115_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestADS1115AnalogRead(t *testing.T) {
gobottest.Assert(t, val, 32767)
gobottest.Assert(t, err, nil)

val, err = d.AnalogRead("3-2")
_, err = d.AnalogRead("3-2")
gobottest.Refute(t, err.Error(), nil)
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/i2c/ads1x15_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func TestADS1x15CommandsAnalogRead(t *testing.T) {
}

func TestADS1x15_ads1x15BestGainForVoltage(t *testing.T) {
g, err := ads1x15BestGainForVoltage(1.5)
g, _ := ads1x15BestGainForVoltage(1.5)
gobottest.Assert(t, g, 2)

g, err = ads1x15BestGainForVoltage(20.0)
_, err := ads1x15BestGainForVoltage(20.0)
gobottest.Assert(t, err, errors.New("The maximum voltage which can be read is 6.144000"))
}
4 changes: 2 additions & 2 deletions drivers/i2c/blinkm_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestBlinkMFirmwareVersion(t *testing.T) {
return 0, errors.New("write error")
}

version, err := d.FirmwareVersion()
_, err := d.FirmwareVersion()
gobottest.Assert(t, err, errors.New("write error"))
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func TestBlinkMColor(t *testing.T) {
return 0, errors.New("write error")
}

color, err := d.Color()
_, err := d.Color()
gobottest.Assert(t, err, errors.New("write error"))

}
Expand Down
8 changes: 4 additions & 4 deletions drivers/i2c/ccs811_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type CCS811DriveMode uint8
// Operating modes which dictate how often measurements are being made. If 0x00 is used as an operating mode, measurements will be disabled
const (
CCS811DriveModeIdle CCS811DriveMode = 0x00
CCS811DriveMode1Sec = 0x01
CCS811DriveMode10Sec = 0x02
CCS811DriveMode60Sec = 0x03
CCS811DriveMode250MS = 0x04
CCS811DriveMode1Sec CCS811DriveMode = 0x01
CCS811DriveMode10Sec CCS811DriveMode = 0x02
CCS811DriveMode60Sec CCS811DriveMode = 0x03
CCS811DriveMode250MS CCS811DriveMode = 0x04
)

const (
Expand Down
14 changes: 7 additions & 7 deletions drivers/i2c/drv2605l_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ type DRV2605Mode uint8
// Operating modes, for use in SetMode()
const (
DRV2605ModeIntTrig DRV2605Mode = 0x00
DRV2605ModeExtTrigEdge = 0x01
DRV2605ModeExtTrigLvl = 0x02
DRV2605ModePWMAnalog = 0x03
DRV2605ModeAudioVibe = 0x04
DRV2605ModeRealtime = 0x05
DRV2605ModeDiagnose = 0x06
DRV2605ModeAutocal = 0x07
DRV2605ModeExtTrigEdge DRV2605Mode = 0x01
DRV2605ModeExtTrigLvl DRV2605Mode = 0x02
DRV2605ModePWMAnalog DRV2605Mode = 0x03
DRV2605ModeAudioVibe DRV2605Mode = 0x04
DRV2605ModeRealtime DRV2605Mode = 0x05
DRV2605ModeDiagnose DRV2605Mode = 0x06
DRV2605ModeAutocal DRV2605Mode = 0x07
)

const (
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/grovepi_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (d *GrovePiDriver) SetPinMode(pin byte, mode string) error {
func getPin(pin string) string {
if len(pin) > 1 {
if strings.ToUpper(pin[0:1]) == "A" || strings.ToUpper(pin[0:1]) == "D" {
return pin[1:len(pin)]
return pin[1:]
}
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/i2c_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type bitState uint8

const (
clear bitState = 0x00
set = 0x01
set bitState = 0x01
)

// Connection is a connection to an I2C device with a specified address
Expand Down
6 changes: 3 additions & 3 deletions drivers/i2c/mcp23017_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,19 @@ func TestMCP23017SetGPIOPolarityErr(t *testing.T) {

func TestMCP23017_write(t *testing.T) {
// clear bit
d, a := initTestMCP23017WithStubbedAdaptor(0)
d, _ := initTestMCP23017WithStubbedAdaptor(0)
port := d.getPort("A")
err := d.write(port.IODIR, uint8(7), 0)
gobottest.Assert(t, err, nil)

// set bit
d, a = initTestMCP23017WithStubbedAdaptor(0)
d, _ = initTestMCP23017WithStubbedAdaptor(0)
port = d.getPort("B")
err = d.write(port.IODIR, uint8(7), 1)
gobottest.Assert(t, err, nil)

// write error
d, a = initTestMCP23017WithStubbedAdaptor(0)
d, a := initTestMCP23017WithStubbedAdaptor(0)
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
Expand Down
20 changes: 9 additions & 11 deletions drivers/i2c/sht2x_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ func (d *SHT2xDriver) initialize() error {
return nil
}

func (d *SHT2xDriver) sendAccuracy() (err error) {
if err = d.connection.WriteByte(SHT2xReadUserReg); err != nil {
return
func (d *SHT2xDriver) sendAccuracy() error {
if err := d.connection.WriteByte(SHT2xReadUserReg); err != nil {
return err
}
userRegister, err := d.connection.ReadByte()
if err != nil {
return
return err
}

userRegister &= 0x7e //Turn off the resolution bits
Expand All @@ -226,15 +226,13 @@ func (d *SHT2xDriver) sendAccuracy() (err error) {
userRegister |= acc //Mask in the requested resolution bits

//Request a write to user register
_, err = d.connection.Write([]byte{SHT2xWriteUserReg, userRegister})
if err != nil {
return
if _, err := d.connection.Write([]byte{SHT2xWriteUserReg, userRegister}); err != nil {
return err
}

userRegister, err = d.connection.ReadByte()
if err != nil {
return
if _, err := d.connection.ReadByte(); err != nil {
return err
}

return
return nil
}
Loading

0 comments on commit fcdf286

Please sign in to comment.