From 376dba5810c5a4a68424719d814e429e10fceebb Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Fri, 27 Nov 2020 18:06:43 +0000 Subject: [PATCH] staticcheck guided refactoring --- Makefile | 4 +++- cmd/magneticod/bittorrent/metadata/leech.go | 2 +- cmd/magneticod/dht/mainline/codec.go | 2 +- cmd/magneticod/dht/mainline/protocol.go | 1 + cmd/magneticow/handlers.go | 1 - pkg/persistence/sqlite3.go | 10 +++++----- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index d898752..43ebceb 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ magneticow: # TODO: minify files! # https://github.com/kevinburke/go-bindata go-bindata -o="cmd/magneticow/bindata.go" -prefix="cmd/magneticow/data/" cmd/magneticow/data/... + # Prepend the linter instruction to the beginning of the file + sed -i '1s;^;//lint:file-ignore * Ignore file altogether\n;' cmd/magneticow/bindata.go go install --tags fts5 "-ldflags=-s -w -X main.compiledOn=`date -u +%Y-%m-%dT%H:%M:%SZ`" ./cmd/magneticow .PHONY: docker @@ -38,7 +40,7 @@ vet: go vet ./... staticcheck: - ./misc/staticcheck/staticcheck ./... + ./misc/staticcheck/staticcheck -fail all ./... test: go test ./... diff --git a/cmd/magneticod/bittorrent/metadata/leech.go b/cmd/magneticod/bittorrent/metadata/leech.go index 438891e..c045217 100644 --- a/cmd/magneticod/bittorrent/metadata/leech.go +++ b/cmd/magneticod/bittorrent/metadata/leech.go @@ -471,7 +471,7 @@ func toBigEndian(i uint, n int) []byte { binary.BigEndian.PutUint32(b, uint32(i)) default: - panic(fmt.Sprintf("n must be 1, 2, or 4!")) + panic("n must be 1, 2, or 4!") } if len(b) != n { diff --git a/cmd/magneticod/dht/mainline/codec.go b/cmd/magneticod/dht/mainline/codec.go index d067b92..1e641e5 100644 --- a/cmd/magneticod/dht/mainline/codec.go +++ b/cmd/magneticod/dht/mainline/codec.go @@ -273,7 +273,7 @@ func (e *Error) UnmarshalBencode(b []byte) (err error) { } if len(matches[2]) != msgLen { - return fmt.Errorf("error message have different lengths (%d vs %d) \"%s\"!", len(matches[2]), msgLen, matches[2]) + return fmt.Errorf("error message have different lengths (%d vs %d) \"%s\"", len(matches[2]), msgLen, matches[2]) } e.Code = code diff --git a/cmd/magneticod/dht/mainline/protocol.go b/cmd/magneticod/dht/mainline/protocol.go index b58e05c..e2371ce 100644 --- a/cmd/magneticod/dht/mainline/protocol.go +++ b/cmd/magneticod/dht/mainline/protocol.go @@ -327,6 +327,7 @@ func validatePingORannouncePeerResponseMessage(msg *Message) bool { } func validateFindNodeResponseMessage(msg *Message) bool { + //lint:ignore S1008 to be done later if len(msg.R.ID) != 20 { return false } diff --git a/cmd/magneticow/handlers.go b/cmd/magneticow/handlers.go index b1cac06..8fcddf7 100644 --- a/cmd/magneticow/handlers.go +++ b/cmd/magneticow/handlers.go @@ -39,7 +39,6 @@ func torrentsInfohashHandler(w http.ResponseWriter, r *http.Request) { // Cache static resources for a day w.Header().Set("Cache-Control", "max-age=86400") _, _ = w.Write(data) - return } func statisticsHandler(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/persistence/sqlite3.go b/pkg/persistence/sqlite3.go index 74b7267..ff32a6d 100644 --- a/pkg/persistence/sqlite3.go +++ b/pkg/persistence/sqlite3.go @@ -218,8 +218,8 @@ func (db *sqlite3Database) GetNumberOfTorrents() (uint, error) { } defer rows.Close() - if rows.Next() != true { - return 0, fmt.Errorf("No rows returned from `SELECT MAX(ROWID)`") + if !rows.Next() { + return 0, fmt.Errorf("no rows returned from `SELECT MAX(ROWID)`") } var n *uint @@ -384,7 +384,7 @@ func (db *sqlite3Database) GetTorrent(infoHash []byte) (*TorrentMetadata, error) return nil, err } - if rows.Next() != true { + if !rows.Next() { return nil, nil } @@ -547,8 +547,8 @@ func (db *sqlite3Database) setupDatabase() error { } defer rows.Close() var userVersion int - if rows.Next() != true { - return fmt.Errorf("sql.Rows.Next (user_version): PRAGMA user_version did not return any rows!") + if !rows.Next() { + return fmt.Errorf("sql.Rows.Next (user_version): PRAGMA user_version did not return any rows") } if err = rows.Scan(&userVersion); err != nil { return errors.Wrap(err, "sql.Rows.Scan (user_version)")