rsc / grind

Grind polishes Go programs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Var declaration moving is not always polished

calmh opened this issue · comments

This code is not be beautiful to start with (there are indeed many things wrong with it), but the change proposed by grind doesn't make it better I think. Perhaps a threshold of some kind where the moving of a vardecl is skipped if it results in more than x new vardecls? Or recognizing the zero value so the early returns could be modified to return nil, ... instead?

@@ -829,26 +830,29 @@ func (m *Model) ConnectedTo(deviceID protocol.DeviceID) bool {
 }

 func (m *Model) GetIgnores(folder string) ([]string, []string, error) {
-   var lines []string

    m.fmut.RLock()
    cfg, ok := m.folderCfgs[folder]
    m.fmut.RUnlock()
    if !ok {
+       var lines []string
        return lines, nil, fmt.Errorf("Folder %s does not exist", folder)
    }

    fd, err := os.Open(filepath.Join(cfg.Path, ".stignore"))
    if err != nil {
        if os.IsNotExist(err) {
+           var lines []string
            return lines, nil, nil
        }
        l.Warnln("Loading .stignore:", err)
+       var lines []string
        return lines, nil, err
    }
    defer fd.Close()

    scanner := bufio.NewScanner(fd)
+   var lines []string
    for scanner.Scan() {
        lines = append(lines, strings.TrimSpace(scanner.Text()))
    }

The variable splitting is working exactly as I intended. It is making it much clearer that the variable being returned is uninitialized (zero) in those early cases. I agree it's not a fantastic way to say that, but a different pass may in the future do the kind of zero value propagation you suggest.

Ok! :)