all: address a bunch more staticcheck warnings

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>
This commit is contained in:
Brad Fitzpatrick 2024-01-15 10:42:15 -08:00
parent 6472da9a44
commit 4ba112378f
24 changed files with 57 additions and 53 deletions

View File

@ -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")
}

View File

@ -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))

View File

@ -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

View File

@ -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
}
for n := range w.workc {
w.fn(n, true)
}
}
w.donec <- true
}

View File

@ -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
}

View File

@ -228,6 +228,9 @@ func init() {
}
defer r.Close()
testIm, err = png.Decode(r)
if err != nil {
panic(err)
}
}
func fillTestImage(im image.Image) {

View File

@ -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
}

View File

@ -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 {

View File

@ -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) {

View File

@ -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{}) {
@ -1339,9 +1339,7 @@ func (pk *packer) writeAZip(ctx context.Context, trunc blob.Ref) (err error) {
fh := &zip.FileHeader{
Name: baseFileName,
Method: zip.Store, // uncompressed
}
if m := pk.fr.ModTime(); !m.IsZero() {
fh.SetModTime(m)
Modified: pk.fr.ModTime(),
}
fh.SetMode(0644)
fw, err := zw.CreateHeader(fh)

View File

@ -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)
}

View File

@ -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{

View File

@ -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

View File

@ -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")

View File

@ -28,6 +28,7 @@ import (
"os"
client "google.golang.org/api/drive/v2"
"google.golang.org/api/option"
)
const (
@ -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
}

View File

@ -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 {

View File

@ -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) {

View File

@ -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) {
@ -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

View File

@ -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

View File

@ -74,6 +74,7 @@ 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]
@ -81,7 +82,7 @@ func StatBlobsParallelHelper(ctx context.Context, blobs []blob.Ref, fn func(blob
select {
case <-ctx.Done():
// If a previous failed, stop.
break
break Blobs
default:
}

View File

@ -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
}

View File

@ -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():

View File

@ -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

View File

@ -3,4 +3,10 @@ checks = ["all",
"-ST1005",
"-ST1003",
"-ST1019",
"-ST1016",
"-ST1000",
"-ST1020",
"-ST1021",
]