Skip to content

Commit

Permalink
all: address a bunch more staticcheck warnings
Browse files Browse the repository at this point in the history
And temporarily disable a few classes of errors in staticcheck.conf
for now, to be addressed later together.

Signed-off-by: Brad Fitzpatrick <brad@danga.com>
  • Loading branch information
bradfitz committed Jan 15, 2024
1 parent 6472da9 commit 4ba1123
Show file tree
Hide file tree
Showing 24 changed files with 57 additions and 53 deletions.
2 changes: 1 addition & 1 deletion dev/devcam/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func setCamdevVarsFor(e *Env, altkey bool) {
}

func (e *Env) wipeCacheDir() {
cacheDir, _ := e.m["CAMLI_CACHE_DIR"]
cacheDir := e.m["CAMLI_CACHE_DIR"]
if cacheDir == "" {
log.Fatal("Could not wipe cache dir, CAMLI_CACHE_DIR not defined")
}
Expand Down
4 changes: 1 addition & 3 deletions dev/devcam/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ func (c *hookCmd) runGofmt() (files []string, err error) {
// TODO(mpl): it would be nice to TrimPrefix the pwd from each file to get a shorter output.
// However, since git sets the pwd to GIT_DIR before running the pre-commit hook, we lost
// the actual pwd from when we ran `git commit`, so no dice so far.
for _, file := range indexFiles {
args = append(args, file)
}
args = append(args, indexFiles...)

if c.verbose {
fmt.Fprintln(cmdmain.Stderr, commandString("gofmt", args))
Expand Down
5 changes: 2 additions & 3 deletions internal/azure/storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,8 @@ func (c *Client) ListBlobs(ctx context.Context, container string, maxResults int
return nil, err
}

for _, it := range bres.Blobs.Blob {
blobs = append(blobs, it)
}
blobs = append(blobs, bres.Blobs.Blob...)

if bres.NextMarker == "" {
// No more blobs to list
break
Expand Down
12 changes: 3 additions & 9 deletions internal/chanworker/chanworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,8 @@ func (w *chanWorker) pump() {
}

func (w *chanWorker) work() {
for {
select {
case n, ok := <-w.workc:
if !ok {
w.donec <- true
return
}
w.fn(n, true)
}
for n := range w.workc {
w.fn(n, true)
}
w.donec <- true
}
12 changes: 6 additions & 6 deletions internal/httputil/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ func GenSelfTLS(hostname string) (certPEM, keyPEM []byte, err error) {
if err != nil {
return certPEM, keyPEM, fmt.Errorf("failed to create certificate: %s", err)
}
var buf bytes.Buffer
if err := pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
var certBuf bytes.Buffer
if err := pem.Encode(&certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
return certPEM, keyPEM, fmt.Errorf("error writing self-signed HTTPS cert: %v", err)
}
certPEM = []byte(buf.String())
certPEM = certBuf.Bytes()

buf.Reset()
if err := pem.Encode(&buf, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
var keyBuf bytes.Buffer
if err := pem.Encode(&keyBuf, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil {
return certPEM, keyPEM, fmt.Errorf("error writing self-signed HTTPS private key: %v", err)
}
keyPEM = buf.Bytes()
keyPEM = keyBuf.Bytes()
return certPEM, keyPEM, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/images/resize/resize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func init() {
}
defer r.Close()
testIm, err = png.Decode(r)
if err != nil {
panic(err)
}
}

func fillTestImage(im image.Image) {
Expand Down
4 changes: 1 addition & 3 deletions internal/osutil/restart_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func RestartProcess(arg ...string) error {
var args []string
if len(arg) > 0 {
args = append(args, os.Args[0])
for _, v := range arg {
args = append(args, v)
}
args = append(args, arg...)
} else {
args = os.Args
}
Expand Down
4 changes: 2 additions & 2 deletions misc/release/make-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,8 @@ func committers() (map[string]string, error) {
committerByName[name] = email
continue
}
c1, _ := commitCountByEmail[firstEmail]
c2, _ := commitCountByEmail[email]
c1 := commitCountByEmail[firstEmail]
c2 := commitCountByEmail[email]
if c1 < c2 {
delete(committers, firstEmail)
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/blobserver/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type azureStorage struct {
cache *memory.Storage // or nil for no cache
}

func (s *azureStorage) String() string {
return fmt.Sprintf("\"azure\" blob storage at host %q, container %q", s.hostname, s.container)
func (sto *azureStorage) String() string {
return fmt.Sprintf("\"azure\" blob storage at host %q, container %q", sto.hostname, sto.container)
}

func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
Expand Down
10 changes: 4 additions & 6 deletions pkg/blobserver/blobpacked/blobpacked.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ var (
)

func (s *storage) String() string {
return fmt.Sprintf("\"blobpacked\" storage")
return `"blobpacked" storage`
}

func (s *storage) Logf(format string, args ...interface{}) {
Expand Down Expand Up @@ -1337,11 +1337,9 @@ func (pk *packer) writeAZip(ctx context.Context, trunc blob.Ref) (err error) {
return fmt.Errorf("File schema blob %v filename had a slash in it: %q", pk.fr.SchemaBlobRef(), baseFileName)
}
fh := &zip.FileHeader{
Name: baseFileName,
Method: zip.Store, // uncompressed
}
if m := pk.fr.ModTime(); !m.IsZero() {
fh.SetModTime(m)
Name: baseFileName,
Method: zip.Store, // uncompressed
Modified: pk.fr.ModTime(),
}
fh.SetMode(0644)
fw, err := zw.CreateHeader(fh)
Expand Down
2 changes: 1 addition & 1 deletion pkg/blobserver/blobpacked/subfetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCapOffsetLength(t *testing.T) {
if gotErr != tt.wantErr || gotLen != tt.wantLen {
var want string
if tt.wantErr {
want = fmt.Sprintf("some error")
want = "some error"
} else {
want = fmt.Sprintf("length %d, no error", tt.wantLen)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/blobserver/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Storage struct {

// SetNewFileGate sets a gate (counting semaphore) on the number of new files
// that may be opened for writing at a time.
func (s *Storage) SetNewFileGate(g *syncutil.Gate) { s.tmpFileGate = g }
func (ds *Storage) SetNewFileGate(g *syncutil.Gate) { ds.tmpFileGate = g }

func NewStorage(fs VFS, root string) *Storage {
return &Storage{
Expand Down
2 changes: 1 addition & 1 deletion pkg/blobserver/gethandler/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ServeBlobRef(rw http.ResponseWriter, req *http.Request, blobRef blob.Ref, f
rw.Header().Set("Content-Type", "application/octet-stream")
rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, immutable", int(HTTP_CACHE_DURATION.Seconds())))

var content io.ReadSeeker = readerutil.NewFakeSeeker(rc, int64(size))
var content = readerutil.NewFakeSeeker(rc, int64(size))
rangeHeader := req.Header.Get("Range") != ""
const small = 32 << 10
var b *blob.Blob
Expand Down
4 changes: 2 additions & 2 deletions pkg/blobserver/google/cloudstorage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ func (s *Storage) Fetch(ctx context.Context, br blob.Ref) (rc io.ReadCloser, siz
if err != nil {
return nil, 0, err
}
if r.Size() >= 1<<32 {
if r.Attrs.Size >= 1<<32 {
r.Close()
return nil, 0, errors.New("object larger than a uint32")
}
size = uint32(r.Size())
size = uint32(r.Attrs.Size)
if size > constants.MaxBlobSize {
r.Close()
return nil, size, errors.New("object too big")
Expand Down
3 changes: 2 additions & 1 deletion pkg/blobserver/google/drive/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"

client "google.golang.org/api/drive/v2"
"google.golang.org/api/option"
)

const (
Expand All @@ -48,7 +49,7 @@ type DriveService struct {
// DriveService (such as Get). If empty, it defaults to the root of the
// drive.
func New(oauthClient *http.Client, parentID string) (*DriveService, error) {
apiservice, err := client.New(oauthClient)
apiservice, err := client.NewService(context.TODO(), option.WithHTTPClient(oauthClient))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/blobserver/handlers/enumerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func handleEnumerateBlobs(rw http.ResponseWriter, req *http.Request, storage blo
waitSeconds, _ = strconv.Atoi(formValueMaxWaitSec)
if waitSeconds != 0 && formValueAfter != "" {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, errMsgMaxWaitSecWithAfter)
fmt.Fprint(rw, errMsgMaxWaitSecWithAfter)
return
}
switch {
Expand Down
2 changes: 1 addition & 1 deletion pkg/blobserver/localdisk/localdisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (ds *DiskStorage) checkFS() (ret error) {
if err != nil {
return fmt.Errorf("localdisk check: unable to read from %s, err=%v", tempfile, err)
}
if bytes.Compare(out, data) != 0 {
if !bytes.Equal(out, data) {
return fmt.Errorf("localdisk check: tempfile contents didn't match, got=%q", out)
}
if _, err := os.Lstat(filename); !os.IsNotExist(err) {
Expand Down
13 changes: 8 additions & 5 deletions pkg/blobserver/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ type s3Storage struct {
hostname string
}

func (s *s3Storage) String() string {
if s.dirPrefix != "" {
return fmt.Sprintf("\"S3\" blob storage at host %q, bucket %q, directory %q", s.hostname, s.bucket, s.dirPrefix)
func (sto *s3Storage) String() string {
if sto.dirPrefix != "" {
return fmt.Sprintf("\"S3\" blob storage at host %q, bucket %q, directory %q", sto.hostname, sto.bucket, sto.dirPrefix)
}
return fmt.Sprintf("\"S3\" blob storage at host %q, bucket %q", s.hostname, s.bucket)
return fmt.Sprintf("\"S3\" blob storage at host %q, bucket %q", sto.hostname, sto.bucket)
}

func newFromConfig(l blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
Expand Down Expand Up @@ -116,7 +116,10 @@ func newFromConfigWithTransport(_ blobserver.Loader, config jsonconfig.Obj, tran
httpClient.Transport = transport
s3Cfg.WithHTTPClient(&httpClient)
}
awsSession := session.New(s3Cfg)
awsSession, err := session.NewSession(s3Cfg)
if err != nil {
return nil, err
}

bucket := config.RequiredString("bucket")
var dirPrefix string
Expand Down
2 changes: 1 addition & 1 deletion pkg/blobserver/s3/s3_preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func determineEndpoint(ctx context.Context, svc s3iface.S3API, endpoint, bucket,
func endpointIsOfficial(endpoint string) (bool, string, error) {
for _, partition := range endpoints.DefaultPartitions() {
for _, region := range partition.Regions() {
s3Endpoint, err := region.ResolveEndpoint(endpoints.S3ServiceID)
s3Endpoint, err := region.ResolveEndpoint(endpoints.S3ServiceID) //lint:ignore SA1019 TODO fix this and caller
if err != nil {
// S3 isn't available in this region yet; unlikely to ever happen
continue
Expand Down
3 changes: 2 additions & 1 deletion pkg/blobserver/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func StatBlobsParallelHelper(ctx context.Context, blobs []blob.Ref, fn func(blob
var fnMu sync.Mutex // serializes calls to fn

var wg syncutil.Group
Blobs:
for i := range blobs {
gate.Start()
b := blobs[i]

select {
case <-ctx.Done():
// If a previous failed, stop.
break
break Blobs
default:
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/importer/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ type OAuthURIs struct {

// NewOAuthClient returns an oauth Client configured with uris and the
// credentials obtained from ctx.
func (ctx *SetupContext) NewOAuthClient(uris OAuthURIs) (*oauth.Client, error) {
clientID, secret, err := ctx.Credentials()
func (sc *SetupContext) NewOAuthClient(uris OAuthURIs) (*oauth.Client, error) {
clientID, secret, err := sc.Credentials()
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/importer/picasa/picasa.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ func (r *run) importAlbums(ctx context.Context) error {
return fmt.Errorf("importAlbums: error listing albums: %v", err)
}
albumsNode, err := r.getTopLevelNode("albums", "Albums")
if err != nil {
return fmt.Errorf("getting picasa top-level Albums node: %w", err)
}
for _, album := range albums {
select {
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion pkg/index/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ func (c *Corpus) PermanodeTime(pn blob.Ref) (t time.Time, ok bool) {
var fi camtypes.FileInfo
ccRef, ccTime, ok := c.pnCamliContent(pn)
if ok {
fi, _ = c.files[ccRef]
fi = c.files[ccRef]
}
if fi.Time != nil {
return time.Time(*fi.Time), true
Expand Down
6 changes: 6 additions & 0 deletions staticcheck.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ checks = ["all",
"-ST1005",
"-ST1003",
"-ST1019",

"-ST1016",

"-ST1000",
"-ST1020",
"-ST1021",
]

0 comments on commit 4ba1123

Please sign in to comment.