Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonboom committed May 10, 2024
1 parent a194112 commit e6945af
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 44 deletions.
11 changes: 8 additions & 3 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import (
)

func TestNilNil(t *testing.T) {
t.Parallel()

pkgs := []string{
"examples",
"strange",
"unsafe",
}
analysistest.Run(t, analysistest.TestData(), analyzer.New(), pkgs...)
}

func TestNilNil_Unsafe(t *testing.T) {
func TestNilNil_Flags(t *testing.T) {
t.Parallel()

anlzr := analyzer.New()
if err := anlzr.Flags.Set("checked-types", "uintptr,unsafeptr"); err != nil {
if err := anlzr.Flags.Set("checked-types", "ptr"); err != nil {
t.Fatal(err)
}
analysistest.Run(t, analysistest.TestData(), anlzr, "unsafe")
analysistest.Run(t, analysistest.TestData(), anlzr, "pointers-only")
}
16 changes: 16 additions & 0 deletions pkg/analyzer/testdata/src/examples/negative.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package examples
import (
"bytes"
"io"
"unsafe"
)

// Not checked at all.
Expand Down Expand Up @@ -38,6 +39,21 @@ func structPtrTypeValid() (*User, error) {
return new(User), nil
}

func unsafePtrValid() (unsafe.Pointer, error) {
if false {
return nil, io.EOF
}
var i int
return unsafe.Pointer(&i), nil
}

func uintPtrValid() (uintptr, error) {
if false {
return 0, io.EOF
}
return 0xc82000c290, nil
}

func channelTypeValid() (ChannelType, error) {
if false {
return nil, io.EOF
Expand Down
22 changes: 22 additions & 0 deletions pkg/analyzer/testdata/src/examples/positive.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package examples

import "unsafe"

type User struct{}

func primitivePtr() (*int, error) {
Expand All @@ -18,6 +20,26 @@ func anonymousStructPtr() (*struct{ ID string }, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func unsafePtr() (unsafe.Pointer, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func uintPtr() (uintptr, error) {
return 0, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func uintPtr0b() (uintptr, error) {
return 0b0, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func uintPtr0x() (uintptr, error) {
return 0x00, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func uintPtr0o() (uintptr, error) {
return 0o000, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func chBi() (chan int, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/analyzer/testdata/src/pointers-only/positive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package examples

type User struct{}

func primitivePtr() (*int, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func structPtr() (*User, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func uintPtr0o() (uintptr, error) {
return 0o000, nil
}

func chBi() (chan int, error) {
return nil, nil
}

func fun() (func(), error) {
return nil, nil
}

func anyType() (any, error) {
return nil, nil
}

func m1() (map[int]int, error) {
return nil, nil
}
41 changes: 0 additions & 41 deletions pkg/analyzer/testdata/src/unsafe/unsafe.go

This file was deleted.

0 comments on commit e6945af

Please sign in to comment.