Fix staticcheck lint errors

This commit is contained in:
Bjørn Erik Pedersen 2022-07-14 13:06:04 +02:00
parent 939bf3d6b2
commit 52b64170ec
17 changed files with 47 additions and 38 deletions

View File

@ -30,8 +30,8 @@ jobs:
shell: bash
- name: Vet
run: go vet ./...
#- name: Staticcheck
# if: matrix.go-version != '1.16.x'
# run: staticcheck ./...
- name: Staticcheck
if: matrix.go-version != '1.16.x'
run: staticcheck ./...
- name: Test
run: go test -race ./...

View File

@ -103,8 +103,8 @@ type Fs interface {
var (
ErrFileClosed = errors.New("File is closed")
ErrOutOfRange = errors.New("Out of range")
ErrTooLarge = errors.New("Too large")
ErrOutOfRange = errors.New("out of range")
ErrTooLarge = errors.New("too large")
ErrFileNotFound = os.ErrNotExist
ErrFileExists = os.ErrExist
ErrDestinationExists = os.ErrExist

View File

@ -98,7 +98,7 @@ func TestOpenFile(t *testing.T) {
io.WriteString(f, "|append")
f.Close()
f, err = fs.OpenFile(path, os.O_RDONLY, 0600)
f, _ = fs.OpenFile(path, os.O_RDONLY, 0600)
contents, _ := ioutil.ReadAll(f)
expectedContents := "initial|append"
if string(contents) != expectedContents {
@ -700,7 +700,7 @@ func removeAllTestFiles(t *testing.T) {
func equal(name1, name2 string) (r bool) {
switch runtime.GOOS {
case "windows":
r = strings.ToLower(name1) == strings.ToLower(name2)
r = strings.EqualFold(name1, name2)
default:
r = name1 == name2
}

View File

@ -79,7 +79,7 @@ func TestUnionCreateExisting(t *testing.T) {
fh.Close()
fh, _ = base.Open("/home/test/file.txt")
data, err = ioutil.ReadAll(fh)
data, _ = ioutil.ReadAll(fh)
if string(data) != "This is a test" {
t.Errorf("Got wrong data in base file")
}
@ -326,9 +326,9 @@ func TestUnionCacheWrite(t *testing.T) {
t.Errorf("Failed to write file")
}
fh.Seek(0, os.SEEK_SET)
fh.Seek(0, io.SeekStart)
buf := make([]byte, 4)
_, err = fh.Read(buf)
_, _ = fh.Read(buf)
fh.Write([]byte(" IS A"))
fh.Close()

View File

@ -302,6 +302,9 @@ func (fs *Fs) Remove(name string) error {
}
var infos []os.FileInfo
infos, err = dir.Readdir(0)
if err != nil {
return err
}
if len(infos) > 0 {
return syscall.ENOTEMPTY
}
@ -345,6 +348,9 @@ func (fs *Fs) RemoveAll(path string) error {
var infos []os.FileInfo
infos, err = dir.Readdir(0)
if err != nil {
return err
}
for _, info := range infos {
nameToRemove := fs.normSeparators(info.Name())
err = fs.RemoveAll(path + fs.separator + nameToRemove)

View File

@ -496,7 +496,7 @@ func TestGcsOpenFile(t *testing.T) {
t.Fatalf("failed to close a file \"%s\": %s", name, err)
}
file, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600)
_, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600)
if !errors.Is(err, syscall.EPERM) {
t.Errorf("%v: open for write: got %v, expected %v", name, err, syscall.EPERM)
}

View File

@ -29,7 +29,7 @@ type httpDir struct {
}
func (d httpDir) Open(name string) (http.File, error) {
if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
strings.Contains(name, "\x00") {
return nil, errors.New("http: invalid character in file path")
}

View File

@ -37,13 +37,13 @@ func TestReadFile(t *testing.T) {
testFS.Create("this_exists.go")
filename := "rumpelstilzchen"
contents, err := fsutil.ReadFile(filename)
_, err := fsutil.ReadFile(filename)
if err == nil {
t.Fatalf("ReadFile %s: error expected, none found", filename)
}
filename = "this_exists.go"
contents, err = fsutil.ReadFile(filename)
contents, err := fsutil.ReadFile(filename)
if err != nil {
t.Fatalf("ReadFile %s: %v", filename, err)
}

View File

@ -186,7 +186,7 @@ func (f *File) Readdirnames(n int) (names []string, err error) {
func (f *File) Read(b []byte) (n int, err error) {
f.fileData.Lock()
defer f.fileData.Unlock()
if f.closed == true {
if f.closed {
return 0, ErrFileClosed
}
if len(b) > 0 && int(f.at) == len(f.fileData.data) {
@ -214,7 +214,7 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
}
func (f *File) Truncate(size int64) error {
if f.closed == true {
if f.closed {
return ErrFileClosed
}
if f.readOnly {
@ -236,7 +236,7 @@ func (f *File) Truncate(size int64) error {
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
if f.closed == true {
if f.closed {
return 0, ErrFileClosed
}
switch whence {
@ -251,7 +251,7 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
}
func (f *File) Write(b []byte) (n int, err error) {
if f.closed == true {
if f.closed {
return 0, ErrFileClosed
}
if f.readOnly {
@ -330,8 +330,8 @@ func (s *FileInfo) Size() int64 {
var (
ErrFileClosed = errors.New("File is closed")
ErrOutOfRange = errors.New("Out of range")
ErrTooLarge = errors.New("Too large")
ErrOutOfRange = errors.New("out of range")
ErrTooLarge = errors.New("too large")
ErrFileNotFound = os.ErrNotExist
ErrFileExists = os.ErrExist
ErrDestinationExists = os.ErrExist

View File

@ -214,7 +214,7 @@ func TestMultipleOpenFiles(t *testing.T) {
if err != nil {
t.Error("fh.Write failed: " + err.Error())
}
_, err = fh1.Seek(0, os.SEEK_SET)
_, err = fh1.Seek(0, io.SeekStart)
if err != nil {
t.Error(err)
}

View File

@ -16,12 +16,12 @@ func TestFilterReadOnly(t *testing.T) {
func TestFilterReadonlyRemoveAndRead(t *testing.T) {
mfs := &MemMapFs{}
fh, err := mfs.Create("/file.txt")
fh, _ := mfs.Create("/file.txt")
fh.Write([]byte("content here"))
fh.Close()
fs := NewReadOnlyFs(mfs)
err = fs.Remove("/file.txt")
err := fs.Remove("/file.txt")
if err == nil {
t.Errorf("Did not fail to remove file")
}

View File

@ -215,6 +215,9 @@ func MakeSSHKeyPair(bits int, pubKeyPath, privateKeyPath string) error {
// generate and write private key as PEM
privateKeyFile, err := os.Create(privateKeyPath)
if err != nil {
return err
}
defer privateKeyFile.Close()
if err != nil {
return err

View File

@ -266,7 +266,7 @@ func TestOpenFile(t *testing.T) {
}
file.Close()
file, err = afs.OpenFile(f.name, os.O_CREATE, 0600)
_, err = afs.OpenFile(f.name, os.O_CREATE, 0600)
if !errors.Is(err, syscall.EPERM) {
t.Errorf("%v: open for write: got %v, expected %v", f.name, err, syscall.EPERM)
}

View File

@ -65,7 +65,7 @@ func (f *UnionFile) ReadAt(s []byte, o int64) (int, error) {
if f.Layer != nil {
n, err := f.Layer.ReadAt(s, o)
if (err == nil || err == io.EOF) && f.Base != nil {
_, err = f.Base.Seek(o+int64(n), os.SEEK_SET)
_, err = f.Base.Seek(o+int64(n), io.SeekStart)
}
return n, err
}

10
util.go
View File

@ -25,6 +25,7 @@ import (
"strings"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
@ -158,16 +159,12 @@ func UnicodeSanitize(s string) string {
// Transform characters with accents into plain forms.
func NeuterAccents(s string) string {
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(t, string(s))
return result
}
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func (a Afero) FileContainsBytes(filename string, subslice []byte) (bool, error) {
return FileContainsBytes(a.Fs, filename, subslice)
}
@ -299,6 +296,9 @@ func IsEmpty(fs Fs, path string) (bool, error) {
}
defer f.Close()
list, err := f.Readdir(-1)
if err != nil {
return false, err
}
return len(list) == 0, nil
}
return fi.Size() == 0, nil

View File

@ -185,7 +185,7 @@ func createZeroSizedFileInTempDir() (File, error) {
func createNonZeroSizedFileInTempDir() (File, error) {
f, err := createZeroSizedFileInTempDir()
if err != nil {
// no file ??
return nil, err
}
byteString := []byte("byteString")
err = WriteFile(testFS, f.Name(), byteString, 0644)
@ -200,7 +200,7 @@ func createNonZeroSizedFileInTempDir() (File, error) {
func deleteFileInTempDir(f File) {
err := testFS.Remove(f.Name())
if err != nil {
// now what?
panic(err)
}
}
@ -217,7 +217,7 @@ func createEmptyTempDir() (string, error) {
func createTempDirWithZeroLengthFiles() (string, error) {
d, dirErr := createEmptyTempDir()
if dirErr != nil {
//now what?
return "", dirErr
}
filePrefix := "_path_test_"
_, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir()
@ -235,7 +235,7 @@ func createTempDirWithZeroLengthFiles() (string, error) {
func createTempDirWithNonZeroLengthFiles() (string, error) {
d, dirErr := createEmptyTempDir()
if dirErr != nil {
//now what?
return "", dirErr
}
filePrefix := "_path_test_"
f, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir()
@ -406,7 +406,7 @@ func TestGetTempDir(t *testing.T) {
func deleteTempDir(d string) {
err := os.RemoveAll(d)
if err != nil {
// now what?
panic(err)
}
}

View File

@ -85,10 +85,10 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
return 0, afero.ErrFileClosed
}
switch whence {
case os.SEEK_SET:
case os.SEEK_CUR:
case io.SeekStart:
case io.SeekCurrent:
offset += f.offset
case os.SEEK_END:
case io.SeekEnd:
offset += int64(f.zipfile.UncompressedSize64)
default:
return 0, syscall.EINVAL