all: Fix some linter errors (#5499)

I'm working through linter complaints, these are some fixes. Broad
categories:

1) Ignore errors where we can ignore errors: add "_ = ..." construct.
you can argue that this is annoying noise, but apart from silencing the
linter it *does* serve the purpose of highlighting that an error is
being ignored. I think this is OK, because the linter highlighted some
error cases I wasn't aware of (starting CPU profiles, for example).

2) Untyped constants where we though we had set the type.

3) A real bug where we ineffectually assigned to a shadowed err.

4) Some dead code removed.

There'll be more of these, because not all packages are fixed, but the
diff was already large enough.
This commit is contained in:
Jakob Borg
2019-02-02 10:11:42 +01:00
committed by GitHub
parent 583172dc8d
commit 2111386ee4
19 changed files with 103 additions and 107 deletions

View File

@@ -44,7 +44,7 @@ func (s *auditService) Serve() {
for {
select {
case ev := <-sub.C():
enc.Encode(ev)
_ = enc.Encode(ev)
case <-s.stop:
return
}

View File

@@ -13,6 +13,6 @@ import "time"
func cpuUsage() time.Duration {
var rusage syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
_ = syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
return time.Duration(rusage.Utime.Nano() + rusage.Stime.Nano())
}

View File

@@ -27,7 +27,7 @@ import (
"strings"
"time"
"github.com/rcrowley/go-metrics"
metrics "github.com/rcrowley/go-metrics"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/connections"
"github.com/syncthing/syncthing/lib/db"
@@ -966,7 +966,7 @@ func (s *apiService) postSystemShutdown(w http.ResponseWriter, r *http.Request)
}
func (s *apiService) flushResponse(resp string, w http.ResponseWriter) {
w.Write([]byte(resp + "\n"))
_, _ = w.Write([]byte(resp + "\n"))
f := w.(http.Flusher)
f.Flush()
}
@@ -1121,15 +1121,17 @@ func (s *apiService) getSupportBundle(w http.ResponseWriter, r *http.Request) {
var heapBuffer, cpuBuffer bytes.Buffer
filename := fmt.Sprintf("syncthing-heap-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss
runtime.GC()
pprof.WriteHeapProfile(&heapBuffer)
files = append(files, fileEntry{name: filename, data: heapBuffer.Bytes()})
if err := pprof.WriteHeapProfile(&heapBuffer); err == nil {
files = append(files, fileEntry{name: filename, data: heapBuffer.Bytes()})
}
const duration = 4 * time.Second
filename = fmt.Sprintf("syncthing-cpu-%s-%s-%s-%s.pprof", runtime.GOOS, runtime.GOARCH, Version, time.Now().Format("150405")) // hhmmss
pprof.StartCPUProfile(&cpuBuffer)
time.Sleep(duration)
pprof.StopCPUProfile()
files = append(files, fileEntry{name: filename, data: cpuBuffer.Bytes()})
if err := pprof.StartCPUProfile(&cpuBuffer); err == nil {
time.Sleep(duration)
pprof.StopCPUProfile()
files = append(files, fileEntry{name: filename, data: cpuBuffer.Bytes()})
}
// Add buffer files to buffer zip
var zipFilesBuffer bytes.Buffer
@@ -1151,7 +1153,7 @@ func (s *apiService) getSupportBundle(w http.ResponseWriter, r *http.Request) {
// Serve the buffer zip to client for download
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment; filename="+zipFileName)
io.Copy(w, &zipFilesBuffer)
_, _ = io.Copy(w, &zipFilesBuffer)
}
func (s *apiService) getSystemHTTPMetrics(w http.ResponseWriter, r *http.Request) {
@@ -1171,7 +1173,7 @@ func (s *apiService) getSystemHTTPMetrics(w http.ResponseWriter, r *http.Request
}
})
bs, _ := json.MarshalIndent(stats, "", " ")
w.Write(bs)
_, _ = w.Write(bs)
}
func (s *apiService) getSystemDiscovery(w http.ResponseWriter, r *http.Request) {
@@ -1463,7 +1465,7 @@ func (s *apiService) getQR(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "image/png")
w.Write(code.PNG())
_, _ = w.Write(code.PNG())
}
func (s *apiService) getPeerCompletion(w http.ResponseWriter, r *http.Request) {
@@ -1561,7 +1563,7 @@ func (s *apiService) getSystemBrowse(w http.ResponseWriter, r *http.Request) {
// Default value or in case of error unmarshalling ends up being basic fs.
var fsType fs.FilesystemType
fsType.UnmarshalText([]byte(qs.Get("filesystem")))
_ = fsType.UnmarshalText([]byte(qs.Get("filesystem")))
sendJSON(w, browseFiles(current, fsType))
}
@@ -1645,9 +1647,10 @@ func (s *apiService) getCPUProf(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
pprof.StartCPUProfile(w)
time.Sleep(duration)
pprof.StopCPUProfile()
if err := pprof.StartCPUProfile(w); err == nil {
time.Sleep(duration)
pprof.StopCPUProfile()
}
}
func (s *apiService) getHeapProf(w http.ResponseWriter, r *http.Request) {
@@ -1657,7 +1660,7 @@ func (s *apiService) getHeapProf(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
runtime.GC()
pprof.WriteHeapProfile(w)
_ = pprof.WriteHeapProfile(w)
}
func toJsonFileInfoSlice(fs []db.FileInfoTruncated) []jsonDBFileInfo {

View File

@@ -20,7 +20,7 @@ import (
"github.com/syncthing/syncthing/lib/rand"
"github.com/syncthing/syncthing/lib/sync"
"golang.org/x/crypto/bcrypt"
"gopkg.in/ldap.v2"
ldap "gopkg.in/ldap.v2"
)
var (
@@ -80,11 +80,10 @@ func basicAuthAndSessionMiddleware(cookieName string, guiCfg config.GUIConfigura
return
}
authOk := false
username := string(fields[0])
password := string(fields[1])
authOk = auth(username, password, guiCfg, ldapCfg)
authOk := auth(username, password, guiCfg, ldapCfg)
if !authOk {
usernameIso := string(iso88591ToUTF8([]byte(username)))
passwordIso := string(iso88591ToUTF8([]byte(password)))

View File

@@ -160,7 +160,7 @@ func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
w.Write(bs)
_, _ = w.Write(bs)
}
func (s *staticsServer) serveThemes(w http.ResponseWriter, r *http.Request) {

View File

@@ -114,13 +114,13 @@ func TestAssetsDir(t *testing.T) {
// The asset map contains compressed assets, so create a couple of gzip compressed assets here.
buf := new(bytes.Buffer)
gw := gzip.NewWriter(buf)
gw.Write([]byte("default"))
_, _ = gw.Write([]byte("default"))
gw.Close()
def := buf.Bytes()
buf = new(bytes.Buffer)
gw = gzip.NewWriter(buf)
gw.Write([]byte("foo"))
_, _ = gw.Write([]byte("foo"))
gw.Close()
foo := buf.Bytes()

View File

@@ -127,7 +127,6 @@ func setBuildMetadata() {
var (
myID protocol.DeviceID
stop = make(chan int)
lans []*net.IPNet
)
const (
@@ -436,7 +435,9 @@ func main() {
}
if options.resetDatabase {
resetDB()
if err := resetDB(); err != nil {
l.Fatalln("Resetting database:", err)
}
return
}
@@ -450,7 +451,9 @@ func main() {
func openGUI() {
cfg, _ := loadOrDefaultConfig()
if cfg.GUI().Enabled {
openURL(cfg.GUI().URL())
if err := openURL(cfg.GUI().URL()); err != nil {
l.Fatalln("Open URL:", err)
}
} else {
l.Warnln("Browser: GUI is currently disabled")
}
@@ -631,7 +634,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
// Attempt to increase the limit on number of open files to the maximum
// allowed, in case we have many peers. We don't really care enough to
// report the error if there is one.
osutil.MaximizeOpenFileLimit()
_, _ = osutil.MaximizeOpenFileLimit()
// Ensure that we have a certificate and key.
cert, err := tls.LoadX509KeyPair(locations[locCertFile], locations[locKeyFile])
@@ -754,7 +757,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
// Add and start folders
for _, folderCfg := range cfg.Folders() {
if folderCfg.Paused {
folderCfg.CreateRoot()
_ = folderCfg.CreateRoot()
continue
}
m.AddFolder(folderCfg)
@@ -823,9 +826,11 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
if runtimeOptions.cpuProfile {
f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid()))
if err != nil {
log.Fatal(err)
l.Fatalln("Creating profile:", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
l.Fatalln("Starting profile:", err)
}
pprof.StartCPUProfile(f)
}
myDev, _ := cfg.Device(myID)
@@ -842,8 +847,8 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
l.Infoln("Anonymous usage reporting is always enabled for candidate releases.")
if opts.URAccepted != usageReportVersion {
opts.URAccepted = usageReportVersion
cfg.SetOptions(opts)
cfg.Save()
_, _ = cfg.SetOptions(opts)
_ = cfg.Save()
// Unique ID will be set and config saved below if necessary.
}
}
@@ -851,8 +856,8 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
// If we are going to do usage reporting, ensure we have a valid unique ID.
if opts := cfg.Options(); opts.URAccepted > 0 && opts.URUniqueID == "" {
opts.URUniqueID = rand.String(8)
cfg.SetOptions(opts)
cfg.Save()
_, _ = cfg.SetOptions(opts)
_ = cfg.Save()
}
usageReportingSvc := newUsageReportingService(cfg, m, connectionsService)
@@ -872,8 +877,8 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
opts.AutoUpgradeIntervalH = 12
// Set the option into the config as well, as the auto upgrade
// loop expects to read a valid interval from there.
cfg.SetOptions(opts)
cfg.Save()
_, _ = cfg.SetOptions(opts)
_ = cfg.Save()
}
// We don't tweak the user's choice of upgrading to pre-releases or
// not, as otherwise they cannot step off the candidate channel.
@@ -954,7 +959,7 @@ func loadConfigAtStartup() *config.Wrapper {
cfg, err := config.Load(cfgFile, myID)
if os.IsNotExist(err) {
cfg = defaultConfig(cfgFile)
cfg.Save()
_ = cfg.Save()
l.Infof("Default config saved. Edit %s to taste or use the GUI\n", cfg.ConfigPath())
} else if err == io.EOF {
l.Fatalln("Failed to load config: unexpected end of file. Truncated or empty configuration?")
@@ -1058,7 +1063,7 @@ func setupGUI(mainService *suture.Supervisor, cfg *config.Wrapper, m *model.Mode
// Can potentially block if the utility we are invoking doesn't
// fork, and just execs, hence keep it in its own routine.
<-api.startedOnce
go openURL(guiCfg.URL())
go func() { _ = openURL(guiCfg.URL()) }()
}
}

View File

@@ -30,7 +30,7 @@ func (c *mockedConfig) LDAP() config.LDAPConfiguration {
func (c *mockedConfig) RawCopy() config.Configuration {
cfg := config.Configuration{}
util.SetDefaults(&cfg.Options)
_ = util.SetDefaults(&cfg.Options)
return cfg
}

View File

@@ -127,13 +127,13 @@ func monitorMain(runtimeOptions RuntimeOptions) {
select {
case s := <-stopSign:
l.Infof("Signal %d received; exiting", s)
cmd.Process.Signal(sigTerm)
_ = cmd.Process.Signal(sigTerm)
<-exit
return
case s := <-restartSign:
l.Infof("Signal %d received; restarting", s)
cmd.Process.Signal(sigHup)
_ = cmd.Process.Signal(sigHup)
err = <-exit
case err = <-exit:
@@ -179,7 +179,7 @@ func copyStderr(stderr io.Reader, dst io.Writer) {
}
if panicFd == nil {
dst.Write([]byte(line))
_, _ = dst.Write([]byte(line))
if strings.Contains(line, "SIGILL") {
l.Warnln(`
@@ -226,20 +226,20 @@ func copyStderr(stderr io.Reader, dst io.Writer) {
stdoutMut.Lock()
for _, line := range stdoutFirstLines {
panicFd.WriteString(line)
_, _ = panicFd.WriteString(line)
}
panicFd.WriteString("...\n")
_, _ = panicFd.WriteString("...\n")
for _, line := range stdoutLastLines {
panicFd.WriteString(line)
_, _ = panicFd.WriteString(line)
}
stdoutMut.Unlock()
}
panicFd.WriteString("Panic at " + time.Now().Format(time.RFC3339) + "\n")
_, _ = panicFd.WriteString("Panic at " + time.Now().Format(time.RFC3339) + "\n")
}
if panicFd != nil {
panicFd.WriteString(line)
_, _ = panicFd.WriteString(line)
}
}
}
@@ -263,7 +263,7 @@ func copyStdout(stdout io.Reader, dst io.Writer) {
}
stdoutMut.Unlock()
dst.Write([]byte(line))
_, _ = dst.Write([]byte(line))
}
}

View File

@@ -17,7 +17,7 @@ import (
func TestAutoClosedFile(t *testing.T) {
os.RemoveAll("_autoclose")
defer os.RemoveAll("_autoclose")
os.Mkdir("_autoclose", 0755)
_ = os.Mkdir("_autoclose", 0755)
file := filepath.FromSlash("_autoclose/tmp")
data := []byte("hello, world\n")

View File

@@ -38,7 +38,10 @@ func savePerfStats(file string) {
t0 := time.Now()
for t := range time.NewTicker(250 * time.Millisecond).C {
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil {
continue
}
curTime := time.Now().UnixNano()
timeDiff := curTime - prevTime
curUsage := rusage.Utime.Nano() + rusage.Stime.Nano()

View File

@@ -19,11 +19,11 @@ func optionTable(w io.Writer, rows [][]string) {
for _, row := range rows {
for i, cell := range row {
if i > 0 {
tw.Write([]byte("\t"))
_, _ = tw.Write([]byte("\t"))
}
tw.Write([]byte(cell))
_, _ = tw.Write([]byte(cell))
}
tw.Write([]byte("\n"))
_, _ = tw.Write([]byte("\n"))
}
tw.Flush()
}

View File

@@ -348,7 +348,9 @@ func newUsageReportingService(cfg *config.Wrapper, model *model.Model, connectio
func (s *usageReportingService) sendUsageReport() error {
d := reportData(s.cfg, s.model, s.connectionsService, s.cfg.Options().URAccepted, false)
var b bytes.Buffer
json.NewEncoder(&b).Encode(d)
if err := json.NewEncoder(&b).Encode(d); err != nil {
return err
}
client := &http.Client{
Transport: &http.Transport{
@@ -417,7 +419,7 @@ func (s *usageReportingService) Stop() {
s.stopMut.RUnlock()
}
func (usageReportingService) String() string {
func (*usageReportingService) String() string {
return "usageReportingService"
}
@@ -425,7 +427,7 @@ func (usageReportingService) String() string {
func cpuBench(iterations int, duration time.Duration, useWeakHash bool) float64 {
dataSize := 16 * protocol.MinBlockSize
bs := make([]byte, dataSize)
rand.Reader.Read(bs)
_, _ = rand.Reader.Read(bs)
var perf float64
for i := 0; i < iterations; i++ {