From cc2f755cb38a2277c00c8ce1d42b188910c21caf Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 20 Sep 2017 14:15:38 +0100 Subject: [PATCH] Add gosimple linter (#242) --- linter.json | 3 ++- .../clientapi/auth/storage/devices/storage.go | 5 +---- .../dendrite/clientapi/producers/syncapi.go | 7 ++----- .../dendrite/clientapi/producers/userupdate.go | 7 ++----- .../dendrite/common/keydb/server_key_table.go | 5 +---- .../matrix-org/dendrite/common/test/config.go | 18 ++++++------------ .../dendrite/federationapi/writers/threepid.go | 6 +----- .../dendrite/mediaapi/fileutils/fileutils.go | 2 +- .../mediaapi/thumbnailer/thumbnailer.go | 6 +----- .../mediaapi/thumbnailer/thumbnailer_nfnt.go | 2 +- .../dendrite/mediaapi/writers/download.go | 6 +++--- .../dendrite/mediaapi/writers/upload.go | 2 +- .../publicroomsapi/directory/public_rooms.go | 5 +---- .../dendrite/roomserver/alias/alias.go | 6 +----- .../dendrite/roomserver/input/latest_events.go | 10 +++------- .../dendrite/syncapi/storage/syncserver.go | 4 +--- 16 files changed, 28 insertions(+), 66 deletions(-) diff --git a/linter.json b/linter.json index f8da9778..fb30f228 100644 --- a/linter.json +++ b/linter.json @@ -16,6 +16,7 @@ "misspell", "unparam", "errcheck", - "vet" + "vet", + "gosimple" ] } diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/storage.go index 6b7ff919..df8bf4f3 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/storage.go @@ -67,10 +67,7 @@ func (d *Database) CreateDevice( } dev, err = d.devices.insertDevice(ctx, txn, deviceID, localpart, accessToken) - if err != nil { - return err - } - return nil + return err }) return } diff --git a/src/github.com/matrix-org/dendrite/clientapi/producers/syncapi.go b/src/github.com/matrix-org/dendrite/clientapi/producers/syncapi.go index dba104f9..6bfcd51a 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/producers/syncapi.go +++ b/src/github.com/matrix-org/dendrite/clientapi/producers/syncapi.go @@ -45,9 +45,6 @@ func (p *SyncAPIProducer) SendData(userID string, roomID string, dataType string m.Key = sarama.StringEncoder(userID) m.Value = sarama.ByteEncoder(value) - if _, _, err := p.Producer.SendMessage(&m); err != nil { - return err - } - - return nil + _, _, err = p.Producer.SendMessage(&m) + return err } diff --git a/src/github.com/matrix-org/dendrite/clientapi/producers/userupdate.go b/src/github.com/matrix-org/dendrite/clientapi/producers/userupdate.go index 2f2ed756..2a5dfc70 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/producers/userupdate.go +++ b/src/github.com/matrix-org/dendrite/clientapi/producers/userupdate.go @@ -57,9 +57,6 @@ func (p *UserUpdateProducer) SendUpdate( } m.Value = sarama.ByteEncoder(value) - if _, _, err := p.Producer.SendMessage(&m); err != nil { - return err - } - - return nil + _, _, err = p.Producer.SendMessage(&m) + return err } diff --git a/src/github.com/matrix-org/dendrite/common/keydb/server_key_table.go b/src/github.com/matrix-org/dendrite/common/keydb/server_key_table.go index b1d6d65f..e89ebcda 100644 --- a/src/github.com/matrix-org/dendrite/common/keydb/server_key_table.go +++ b/src/github.com/matrix-org/dendrite/common/keydb/server_key_table.go @@ -116,10 +116,7 @@ func (s *serverKeyStatements) upsertServerKeys( string(request.ServerName), string(request.KeyID), nameAndKeyID(request), int64(keys.ValidUntilTS), keyJSON, ) - if err != nil { - return err - } - return nil + return err } func nameAndKeyID(request gomatrixserverlib.PublicKeyRequest) string { diff --git a/src/github.com/matrix-org/dendrite/common/test/config.go b/src/github.com/matrix-org/dendrite/common/test/config.go index d29b754e..ad878c5b 100644 --- a/src/github.com/matrix-org/dendrite/common/test/config.go +++ b/src/github.com/matrix-org/dendrite/common/test/config.go @@ -135,17 +135,14 @@ func NewMatrixKey(matrixKeyPath string) (err error) { err = keyOut.Close() })() - if err = pem.Encode(keyOut, &pem.Block{ + err = pem.Encode(keyOut, &pem.Block{ Type: "MATRIX PRIVATE KEY", Headers: map[string]string{ "Key-ID": "ed25519:" + base64.RawStdEncoding.EncodeToString(data[:3]), }, Bytes: data[3:], - }); err != nil { - return err - } - - return nil + }) + return err } const certificateDuration = time.Hour * 24 * 365 * 10 @@ -191,12 +188,9 @@ func NewTLSKey(tlsKeyPath, tlsCertPath string) error { return err } defer keyOut.Close() // nolint: errcheck - if err = pem.Encode(keyOut, &pem.Block{ + err = pem.Encode(keyOut, &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv), - }); err != nil { - return err - } - - return nil + }) + return err } diff --git a/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go b/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go index 1ab03a34..7c4ccfcf 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go +++ b/src/github.com/matrix-org/dendrite/federationapi/writers/threepid.go @@ -347,9 +347,5 @@ func fillDisplayName( // Use the m.room.third_party_invite event to fill the "displayname" and // update the m.room.member event's content with it content.ThirdPartyInvite.DisplayName = thirdPartyInviteContent.DisplayName - if err := builder.SetContent(content); err != nil { - return err - } - - return nil + return builder.SetContent(content) } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go b/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go index e9a1a519..97e5fe99 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/fileutils/fileutils.go @@ -55,7 +55,7 @@ func GetPathFromBase64Hash(base64Hash types.Base64Hash, absBasePath config.Path) // check if the absolute absBasePath is a prefix of the absolute filePath // if so, no directory escape has occurred and the filePath is valid // Note: absBasePath is already absolute - if strings.HasPrefix(filePath, string(absBasePath)) == false { + if !strings.HasPrefix(filePath, string(absBasePath)) { return "", fmt.Errorf("Invalid filePath (not within absBasePath %v): %v", absBasePath, filePath) } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go index ded71b65..6aa76cf0 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer.go @@ -80,11 +80,7 @@ func SelectThumbnail(desired types.ThumbnailSize, thumbnails []*types.ThumbnailM fitness := calcThumbnailFitness(types.ThumbnailSize(thumbnailSize), nil, desired) if isBetter := fitness.betterThan(bestFit, desired.ResizeMethod == "crop"); isBetter { bestFit = fitness - chosenThumbnailSize = &types.ThumbnailSize{ - Width: thumbnailSize.Width, - Height: thumbnailSize.Height, - ResizeMethod: thumbnailSize.ResizeMethod, - } + chosenThumbnailSize = (*types.ThumbnailSize)(&thumbnailSize) } } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go index 06607f71..a55a6d2a 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/thumbnailer/thumbnailer_nfnt.go @@ -156,7 +156,7 @@ func createThumbnail(src types.Path, img image.Image, config types.ThumbnailSize logger.WithFields(log.Fields{ "ActualWidth": width, "ActualHeight": height, - "processTime": time.Now().Sub(start), + "processTime": time.Since(start), }).Info("Generated thumbnail") stat, err := os.Stat(string(dst)) diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go index 49068580..c69f5ae9 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/download.go @@ -155,7 +155,7 @@ func (r *downloadRequest) jsonErrorResponse(w http.ResponseWriter, res util.JSON // Validate validates the downloadRequest fields func (r *downloadRequest) Validate() *util.JSONResponse { - if mediaIDRegex.MatchString(string(r.MediaMetadata.MediaID)) == false { + if !mediaIDRegex.MatchString(string(r.MediaMetadata.MediaID)) { return &util.JSONResponse{ Code: 404, JSON: jsonerror.NotFound(fmt.Sprintf("mediaId must be a non-empty string using only characters in %v", mediaIDCharacters)), @@ -337,7 +337,7 @@ func (r *downloadRequest) getThumbnailFile( thumbnail, thumbnailSize = thumbnailer.SelectThumbnail(r.ThumbnailSize, thumbnails, thumbnailSizes) // If dynamicThumbnails is true and we are not over-loaded then we would have generated what was requested above. // So we don't try to generate a pre-generated thumbnail here. - if thumbnailSize != nil && dynamicThumbnails == false { + if thumbnailSize != nil && !dynamicThumbnails { r.Logger.WithFields(log.Fields{ "Width": thumbnailSize.Width, "Height": thumbnailSize.Height, @@ -525,7 +525,7 @@ func (r *downloadRequest) fetchRemoteFileAndStoreMetadata( // If the file is a duplicate (has the same hash as an existing file) then // there is valid metadata in the database for that file. As such we only // remove the file if it is not a duplicate. - if duplicate == false { + if !duplicate { finalDir := filepath.Dir(string(finalPath)) fileutils.RemoveDir(types.Path(finalDir), r.Logger) } diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go index eaea8561..f7ffd368 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go @@ -226,7 +226,7 @@ func (r *uploadRequest) storeFileAndMetadata(tmpDir types.Path, absBasePath conf // If the file is a duplicate (has the same hash as an existing file) then // there is valid metadata in the database for that file. As such we only // remove the file if it is not a duplicate. - if duplicate == false { + if !duplicate { fileutils.RemoveDir(types.Path(path.Dir(string(finalPath))), r.Logger) } return &util.JSONResponse{ diff --git a/src/github.com/matrix-org/dendrite/publicroomsapi/directory/public_rooms.go b/src/github.com/matrix-org/dendrite/publicroomsapi/directory/public_rooms.go index 19ae90d8..772c0f6b 100644 --- a/src/github.com/matrix-org/dendrite/publicroomsapi/directory/public_rooms.go +++ b/src/github.com/matrix-org/dendrite/publicroomsapi/directory/public_rooms.go @@ -102,10 +102,7 @@ func fillPublicRoomsReq(httpReq *http.Request, request *publicRoomReq) *util.JSO request.Since = httpReq.FormValue("since") return nil } else if httpReq.Method == "POST" { - if reqErr := httputil.UnmarshalJSONRequest(httpReq, request); reqErr != nil { - return reqErr - } - return nil + return httputil.UnmarshalJSONRequest(httpReq, request) } return &util.JSONResponse{ diff --git a/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go b/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go index 91aa3a26..fed98dd8 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go +++ b/src/github.com/matrix-org/dendrite/roomserver/alias/alias.go @@ -217,11 +217,7 @@ func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent( var inputRes api.InputRoomEventsResponse // Send the request - if err := r.InputAPI.InputRoomEvents(ctx, &inputReq, &inputRes); err != nil { - return err - } - - return nil + return r.InputAPI.InputRoomEvents(ctx, &inputReq, &inputRes) } // SetupHTTP adds the RoomserverAliasAPI handlers to the http.ServeMux. diff --git a/src/github.com/matrix-org/dendrite/roomserver/input/latest_events.go b/src/github.com/matrix-org/dendrite/roomserver/input/latest_events.go index c20613db..30129a62 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/input/latest_events.go +++ b/src/github.com/matrix-org/dendrite/roomserver/input/latest_events.go @@ -102,8 +102,7 @@ type latestEventsUpdater struct { } func (u *latestEventsUpdater) doUpdateLatestEvents() error { - var prevEvents []gomatrixserverlib.EventReference - prevEvents = u.event.PrevEvents() + prevEvents := u.event.PrevEvents() oldLatest := u.updater.LatestEvents() u.lastEventIDSent = u.updater.LastEventIDSent() u.oldStateNID = u.updater.CurrentStateSnapshotNID() @@ -194,10 +193,7 @@ func (u *latestEventsUpdater) latestState() error { u.stateBeforeEventRemoves, u.stateBeforeEventAdds, err = state.DifferenceBetweeenStateSnapshots( u.ctx, u.db, u.newStateNID, u.stateAtEvent.BeforeStateSnapshotNID, ) - if err != nil { - return err - } - return nil + return err } func calculateLatest( @@ -211,7 +207,7 @@ func calculateLatest( for _, l := range oldLatest { keep := true for _, prevEvent := range prevEvents { - if l.EventID == prevEvent.EventID && bytes.Compare(l.EventSHA256, prevEvent.EventSHA256) == 0 { + if l.EventID == prevEvent.EventID && bytes.Equal(l.EventSHA256, prevEvent.EventSHA256) { // This event can be removed from the latest events cause we've found an event that references it. // (If an event is referenced by another event then it can't be one of the latest events in the room // because we have an event that comes after it) diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go index a26b14c0..e65639eb 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go @@ -461,9 +461,7 @@ func (d *SyncServerDatabase) fetchMissingStateEvents( if len(stateEvents) != len(missing) { return nil, fmt.Errorf("failed to map all event IDs to events: (got %d, wanted %d)", len(stateEvents), len(missing)) } - for _, e := range stateEvents { - events = append(events, e) - } + events = append(events, stateEvents...) return events, nil }