Skip to content

Commit

Permalink
Fix staticcheck lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 14, 2022
1 parent 939bf3d commit 52b6417
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 38 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Expand Up @@ -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 ./...
4 changes: 2 additions & 2 deletions afero.go
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions afero_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions composite_test.go
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions gcsfs/fs.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gcsfs/gcs_test.go
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion httpFs.go
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions ioutil_test.go
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions mem/file.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion memmap_test.go
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions ro_regexp_test.go
Expand Up @@ -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")
}
Expand Down
3 changes: 3 additions & 0 deletions sftpfs/sftp_test.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tarfs/tarfs_test.go
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion unionFile.go
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions util.go
Expand Up @@ -25,6 +25,7 @@ import (
"strings"
"unicode"

"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions util_test.go
Expand Up @@ -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)
Expand All @@ -200,7 +200,7 @@ func createNonZeroSizedFileInTempDir() (File, error) {
func deleteFileInTempDir(f File) {
err := testFS.Remove(f.Name())
if err != nil {
// now what?
panic(err)
}
}

Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -406,7 +406,7 @@ func TestGetTempDir(t *testing.T) {
func deleteTempDir(d string) {
err := os.RemoveAll(d)
if err != nil {
// now what?
panic(err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions zipfs/file.go
Expand Up @@ -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
Expand Down

0 comments on commit 52b6417

Please sign in to comment.