diff options
author | Moritz Poldrack <git@moritz.sh> | 2022-07-31 14:32:48 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-08-04 21:58:01 +0200 |
commit | 978d35d356e8752bdd272884df48a6289d88b40a (patch) | |
tree | 3910243e688ef503159d07ce44b22cfea5d6c6fd /lib | |
parent | c882cf9960be691fe55617b87cdfcfbabd5d5557 (diff) | |
download | aerc-978d35d356e8752bdd272884df48a6289d88b40a.tar.gz |
lint: homogenize operations and minor fixes (gocritic)
Apply GoDoc comment policy (comments for humans should have a space
after the //; machine-readable comments shouldn't)
Use strings.ReplaceAll instead of strings.Replace when appropriate
Remove if/else chains by replacing them with switches
Use short assignment/increment notation
Replace single case switches with if statements
Combine else and if when appropriate
Signed-off-by: Moritz Poldrack <moritz@poldrack.dev>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/auth/auth.go | 8 | ||||
-rw-r--r-- | lib/format/format.go | 7 | ||||
-rw-r--r-- | lib/msgstore.go | 18 | ||||
-rw-r--r-- | lib/open.go | 2 | ||||
-rw-r--r-- | lib/statusline/renderer.go | 6 | ||||
-rw-r--r-- | lib/structure_helpers.go | 8 | ||||
-rw-r--r-- | lib/ui/borders.go | 3 | ||||
-rw-r--r-- | lib/ui/grid.go | 3 | ||||
-rw-r--r-- | lib/ui/popover.go | 7 | ||||
-rw-r--r-- | lib/ui/stack.go | 3 | ||||
-rw-r--r-- | lib/ui/tab.go | 19 | ||||
-rw-r--r-- | lib/ui/textinput.go | 18 | ||||
-rw-r--r-- | lib/ui/ui.go | 3 |
13 files changed, 46 insertions, 59 deletions
diff --git a/lib/auth/auth.go b/lib/auth/auth.go index 8a0a40fa..ea32ecd3 100644 --- a/lib/auth/auth.go +++ b/lib/auth/auth.go @@ -82,14 +82,16 @@ func CreateParser(m Method) func(*mail.Header, []string) (*Details, error) { } identifier, results, err := authres.Parse(headerText) - if err != nil && err.Error() == "msgauth: unsupported version" { + // TODO: refactor to use errors.Is + switch { + case err != nil && err.Error() == "msgauth: unsupported version": // Some MTA write their authres header without an identifier // which does not conform to RFC but still exists in the wild identifier, results, err = authres.Parse("unknown;" + headerText) if err != nil { return nil, err } - } else if err != nil && err.Error() == "msgauth: malformed authentication method and value" { + case err != nil && err.Error() == "msgauth: malformed authentication method and value": // the go-msgauth parser doesn't like semi-colons in the comments // as a work-around we remove those cleanHeader := cleaner.ReplaceAllString(headerText, "${1}${2}") @@ -97,7 +99,7 @@ func CreateParser(m Method) func(*mail.Header, []string) (*Details, error) { if err != nil { return nil, err } - } else if err != nil { + case err != nil: return nil, err } diff --git a/lib/format/format.go b/lib/format/format.go index 0d14cb19..42dd34a1 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -298,11 +298,12 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string, recent := false answered := false for _, flag := range ctx.MsgInfo.Flags { - if flag == models.SeenFlag { + switch flag { + case models.SeenFlag: seen = true - } else if flag == models.RecentFlag { + case models.RecentFlag: recent = true - } else if flag == models.AnsweredFlag { + case models.AnsweredFlag: answered = true } if flag == models.DeletedFlag { diff --git a/lib/msgstore.go b/lib/msgstore.go index dc000de7..efa6f732 100644 --- a/lib/msgstore.go +++ b/lib/msgstore.go @@ -121,8 +121,7 @@ func (store *MessageStore) FetchHeaders(uids []uint32, } if len(toFetch) > 0 { store.worker.PostAction(&types.FetchMessageHeaders{Uids: toFetch}, func(msg types.WorkerMessage) { - switch msg.(type) { - case *types.Error: + if _, ok := msg.(*types.Error); ok { for _, uid := range toFetch { delete(store.pendingHeaders, uid) delete(store.headerCallbacks, uid) @@ -153,8 +152,7 @@ func (store *MessageStore) FetchFull(uids []uint32, cb func(*types.FullMessage)) store.worker.PostAction(&types.FetchFullMessages{ Uids: toFetch, }, func(msg types.WorkerMessage) { - switch msg.(type) { - case *types.Error: + if _, ok := msg.(*types.Error); ok { for _, uid := range toFetch { delete(store.pendingBodies, uid) delete(store.bodyCallbacks, uid) @@ -244,10 +242,8 @@ func (store *MessageStore) Update(msg types.WorkerMessage) { case *types.MessageInfo: if existing, ok := store.Messages[msg.Info.Uid]; ok && existing != nil { merge(existing, msg.Info) - } else { - if msg.Info.Envelope != nil { - store.Messages[msg.Info.Uid] = msg.Info - } + } else if msg.Info.Envelope != nil { + store.Messages[msg.Info.Uid] = msg.Info } seen := false recent := false @@ -441,8 +437,7 @@ func (store *MessageStore) Delete(uids []uint32, store.worker.PostAction(&types.DeleteMessages{Uids: uids}, func(msg types.WorkerMessage) { - switch msg.(type) { - case *types.Error: + if _, ok := msg.(*types.Error); ok { store.revertDeleted(uids) } cb(msg) @@ -726,8 +721,7 @@ func (store *MessageStore) Search(args []string, cb func([]uint32)) { store.worker.PostAction(&types.SearchDirectory{ Argv: args, }, func(msg types.WorkerMessage) { - switch msg := msg.(type) { - case *types.SearchResults: + if msg, ok := msg.(*types.SearchResults); ok { allowedUids := store.Uids() uids := make([]uint32, 0, len(msg.Uids)) for _, uid := range msg.Uids { diff --git a/lib/open.go b/lib/open.go index c5d4b1c0..c29ed009 100644 --- a/lib/open.go +++ b/lib/open.go @@ -35,7 +35,7 @@ func NewXDGOpen(filename string) *xdgOpen { func (xdg *xdgOpen) SetArgs(args []string) { args = append([]string{}, args...) // don't overwrite array of caller filename := xdg.args[len(xdg.args)-1] - xdg.args = append(args, filename) + xdg.args = append(args, filename) //nolint:gocritic // intentional append to different slice } // Start the open handler. diff --git a/lib/statusline/renderer.go b/lib/statusline/renderer.go index 2ab05dd9..26c95c24 100644 --- a/lib/statusline/renderer.go +++ b/lib/statusline/renderer.go @@ -69,10 +69,8 @@ func contentInfo(acct *accountState, fldr *folderState, texter Texter) []string var status []string if fldr.FilterActivity != "" { status = append(status, fldr.FilterActivity) - } else { - if fldr.Filter != "" { - status = append(status, texter.FormatFilter(fldr.Filter)) - } + } else if fldr.Filter != "" { + status = append(status, texter.FormatFilter(fldr.Filter)) } if fldr.Search != "" { status = append(status, texter.FormatSearch(fldr.Search)) diff --git a/lib/structure_helpers.go b/lib/structure_helpers.go index 99a77a28..6f25e459 100644 --- a/lib/structure_helpers.go +++ b/lib/structure_helpers.go @@ -8,7 +8,7 @@ import ( func FindPlaintext(bs *models.BodyStructure, path []int) []int { for i, part := range bs.Parts { - cur := append(path, i+1) + cur := append(path, i+1) //nolint:gocritic // intentional append to different slice if strings.ToLower(part.MIMEType) == "text" && strings.ToLower(part.MIMESubType) == "plain" { return cur @@ -24,7 +24,7 @@ func FindPlaintext(bs *models.BodyStructure, path []int) []int { func FindCalendartext(bs *models.BodyStructure, path []int) []int { for i, part := range bs.Parts { - cur := append(path, i+1) + cur := append(path, i+1) //nolint:gocritic // intentional append to different slice if strings.ToLower(part.MIMEType) == "text" && strings.ToLower(part.MIMESubType) == "calendar" { return cur @@ -40,7 +40,7 @@ func FindCalendartext(bs *models.BodyStructure, path []int) []int { func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int { for i, part := range bs.Parts { - cur := append(path, i+1) + cur := append(path, i+1) //nolint:gocritic // intentional append to different slice mimetype := strings.ToLower(part.MIMEType) if mimetype != "multipart" { return cur @@ -55,7 +55,7 @@ func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int { func FindAllNonMultipart(bs *models.BodyStructure, path []int, pathlist [][]int) [][]int { for i, part := range bs.Parts { - cur := append(path, i+1) + cur := append(path, i+1) //nolint:gocritic // intentional append to different slice mimetype := strings.ToLower(part.MIMEType) if mimetype != "multipart" { tmp := make([]int, len(cur)) diff --git a/lib/ui/borders.go b/lib/ui/borders.go index 2c689886..92e29b06 100644 --- a/lib/ui/borders.go +++ b/lib/ui/borders.go @@ -76,8 +76,7 @@ func (bordered *Bordered) Draw(ctx *Context) { } func (bordered *Bordered) MouseEvent(localX int, localY int, event tcell.Event) { - switch content := bordered.content.(type) { - case Mouseable: + if content, ok := bordered.content.(Mouseable); ok { content.MouseEvent(localX, localY, event) } } diff --git a/lib/ui/grid.go b/lib/ui/grid.go index 2d195711..1eac2ee8 100644 --- a/lib/ui/grid.go +++ b/lib/ui/grid.go @@ -150,8 +150,7 @@ func (grid *Grid) Draw(ctx *Context) { } func (grid *Grid) MouseEvent(localX int, localY int, event tcell.Event) { - switch event := event.(type) { - case *tcell.EventMouse: + if event, ok := event.(*tcell.EventMouse); ok { invalid := grid.invalid grid.mutex.RLock() diff --git a/lib/ui/popover.go b/lib/ui/popover.go index 7a539de1..8c8c4cda 100644 --- a/lib/ui/popover.go +++ b/lib/ui/popover.go @@ -16,13 +16,14 @@ func (p *Popover) Draw(ctx *Context) { width = ctx.Width() - p.x } - if p.y+p.height+1 < ctx.Height() { + switch { + case p.y+p.height+1 < ctx.Height(): // draw below subcontext = ctx.Subcontext(p.x, p.y+1, width, p.height) - } else if p.y-p.height >= 0 { + case p.y-p.height >= 0: // draw above subcontext = ctx.Subcontext(p.x, p.y-p.height, width, p.height) - } else { + default: // can't fit entirely above or below, so find the largest available // vertical space and shrink to fit if p.y > ctx.Height()-p.y { diff --git a/lib/ui/stack.go b/lib/ui/stack.go index d3722354..5ccf13bc 100644 --- a/lib/ui/stack.go +++ b/lib/ui/stack.go @@ -43,8 +43,7 @@ func (stack *Stack) Draw(ctx *Context) { func (stack *Stack) MouseEvent(localX int, localY int, event tcell.Event) { if len(stack.children) > 0 { - switch element := stack.Peek().(type) { - case Mouseable: + if element, ok := stack.Peek().(Mouseable); ok { element.MouseEvent(localX, localY, event) } } diff --git a/lib/ui/tab.go b/lib/ui/tab.go index 0c6b3f5b..df76ccca 100644 --- a/lib/ui/tab.go +++ b/lib/ui/tab.go @@ -219,27 +219,28 @@ func (tabs *Tabs) moveTabPriv(to int, relative bool) { } tab := tabs.tabs[from] - if to > from { + switch { + case to > from: copy(tabs.tabs[from:to], tabs.tabs[from+1:to+1]) for i, h := range tabs.history { if h == from { tabs.history[i] = to } if h > from && h <= to { - tabs.history[i] -= 1 + tabs.history[i]-- } } - } else if from > to { + case from > to: copy(tabs.tabs[to+1:from+1], tabs.tabs[to:from]) for i, h := range tabs.history { if h == from { tabs.history[i] = to } if h >= to && h < from { - tabs.history[i] += 1 + tabs.history[i]++ } } - } else { + default: return } @@ -339,7 +340,7 @@ func (tabs *Tabs) removeHistory(index int) { continue } if item > index { - item = item - 1 + item-- } // dedup if i > 0 && len(newHist) > 0 && item == newHist[len(newHist)-1] { @@ -399,8 +400,7 @@ func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) { } unfocus := func() { changeFocus(false) } refocus := func() { changeFocus(true) } - switch event := event.(type) { - case *tcell.EventMouse: + if event, ok := event.(*tcell.EventMouse); ok { switch event.Buttons() { case tcell.Button1: selectedTab, ok := strip.clicked(localX, localY) @@ -484,8 +484,7 @@ func (content *TabContent) MouseEvent(localX int, localY int, event tcell.Event) content.parent.m.Lock() tab := content.tabs[content.curIndex] content.parent.m.Unlock() - switch tabContent := tab.Content.(type) { - case Mouseable: + if tabContent, ok := tab.Content.(Mouseable); ok { tabContent.MouseEvent(localX, localY, event) } } diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go index 8f8f00d0..70dcb3f5 100644 --- a/lib/ui/textinput.go +++ b/lib/ui/textinput.go @@ -148,10 +148,8 @@ func (ti *TextInput) drawPopover(ctx *Context) { } func (ti *TextInput) MouseEvent(localX int, localY int, event tcell.Event) { - switch event := event.(type) { - case *tcell.EventMouse: - switch event.Buttons() { - case tcell.Button1: + if event, ok := event.(*tcell.EventMouse); ok { + if event.Buttons() == tcell.Button1 { if localX >= len(ti.prompt)+1 && localX <= len(ti.text[ti.scroll:])+len(ti.prompt)+1 { ti.index = localX - len(ti.prompt) - 1 ti.ensureScroll() @@ -190,7 +188,7 @@ func (ti *TextInput) ensureScroll() { func (ti *TextInput) insert(ch rune) { left := ti.text[:ti.index] right := ti.text[ti.index:] - ti.text = append(left, append([]rune{ch}, right...)...) + ti.text = append(left, append([]rune{ch}, right...)...) //nolint:gocritic // intentional append to different slice ti.index++ ti.ensureScroll() ti.Invalidate() @@ -323,8 +321,7 @@ func (ti *TextInput) OnFocusLost(onFocusLost func(ti *TextInput)) { } func (ti *TextInput) Event(event tcell.Event) bool { - switch event := event.(type) { - case *tcell.EventKey: + if event, ok := event.(*tcell.EventKey); ok { switch event.Key() { case tcell.KeyBackspace, tcell.KeyBackspace2: ti.invalidateCompletions() @@ -464,8 +461,7 @@ func (c *completions) prev() { } func (c *completions) Event(e tcell.Event) bool { - switch e := e.(type) { - case *tcell.EventKey: + if e, ok := e.(*tcell.EventKey); ok { switch e.Key() { case tcell.KeyTab: if len(c.options) == 1 && c.idx >= 0 { @@ -496,7 +492,7 @@ func (c *completions) Event(e tcell.Event) bool { } func findStem(words []string) string { - if len(words) <= 0 { + if len(words) == 0 { return "" } if len(words) == 1 { @@ -519,7 +515,7 @@ func findStem(words []string) string { return stem } } - stem = stem + string(r) + stem += string(r) stemLen++ } } diff --git a/lib/ui/ui.go b/lib/ui/ui.go index 1f618a6f..596000a5 100644 --- a/lib/ui/ui.go +++ b/lib/ui/ui.go @@ -87,8 +87,7 @@ func (state *UI) Tick() bool { select { case event := <-state.tcEvents: - switch event := event.(type) { - case *tcell.EventResize: + if event, ok := event.(*tcell.EventResize); ok { state.screen.Clear() width, height := event.Size() state.ctx = NewContext(width, height, state.screen, state.onPopover) |