Skip to content

Commit fe88914

Browse files
committed
tests: add cgo tests
1 parent e1fbf35 commit fe88914

File tree

106 files changed

+4781
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+4781
-0
lines changed

Diff for: pkg/golinters/asasalint/testdata/asasalint_cgo.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//golangcitest:args -Easasalint
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"fmt"
16+
"unsafe"
17+
)
18+
19+
func _() {
20+
cs := C.CString("Hello from stdio\n")
21+
C.myprint(cs)
22+
C.free(unsafe.Pointer(cs))
23+
}
24+
25+
func getArgsLength(args ...interface{}) int {
26+
// this line will not report as error
27+
fmt.Println(args)
28+
return len(args)
29+
}
30+
31+
func checkArgsLength(args ...interface{}) int {
32+
return getArgsLength(args) // want `pass \[\]any as any to func getArgsLength func\(args \.\.\.interface\{\}\)`
33+
}
34+
35+
func someCall() {
36+
var a = []interface{}{1, 2, 3}
37+
fmt.Println(checkArgsLength(a...) == getArgsLength(a)) // want `pass \[\]any as any to func getArgsLength func\(args \.\.\.interface\{\}\)`
38+
fmt.Println(checkArgsLength(a...) == getArgsLength(a...))
39+
}

Diff for: pkg/golinters/asciicheck/testdata/asasalint_cgo.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//golangcitest:args -Easciicheck
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"fmt"
16+
"time"
17+
"unsafe"
18+
)
19+
20+
func _() {
21+
cs := C.CString("Hello from stdio\n")
22+
C.myprint(cs)
23+
C.free(unsafe.Pointer(cs))
24+
}
25+
26+
type AsciicheckTеstStruct struct { // want `identifier "AsciicheckTеstStruct" contain non-ASCII character: U\+0435 'е'`
27+
Date time.Time
28+
}
29+
30+
type AsciicheckField struct{}
31+
32+
type AsciicheckJustStruct struct {
33+
Tеst AsciicheckField // want `identifier "Tеst" contain non-ASCII character: U\+0435 'е'`
34+
}
35+
36+
func AsciicheckTеstFunc() { // want `identifier "AsciicheckTеstFunc" contain non-ASCII character: U\+0435 'е'`
37+
var tеstVar int // want `identifier "tеstVar" contain non-ASCII character: U\+0435 'е'`
38+
tеstVar = 0
39+
fmt.Println(tеstVar)
40+
}

Diff for: pkg/golinters/bidichk/testdata/bidichk_cgo.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//golangcitest:args -Ebidichk
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"fmt"
16+
"unsafe"
17+
)
18+
19+
func _() {
20+
cs := C.CString("Hello from stdio\n")
21+
C.myprint(cs)
22+
C.free(unsafe.Pointer(cs))
23+
}
24+
25+
func _() {
26+
fmt.Println("LEFT-TO-RIGHT-OVERRIDE: '‭', it is between the single quotes, but it is not visible with a regular editor") // want "found dangerous unicode character sequence LEFT-TO-RIGHT-OVERRIDE"
27+
}

Diff for: pkg/golinters/bodyclose/testdata/bodyclose_cgo.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//golangcitest:args -Ebodyclose
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"io/ioutil"
16+
"net/http"
17+
"unsafe"
18+
)
19+
20+
func _() {
21+
cs := C.CString("Hello from stdio\n")
22+
C.myprint(cs)
23+
C.free(unsafe.Pointer(cs))
24+
}
25+
26+
func BodycloseNotClosed() {
27+
resp, _ := http.Get("https://google.com") // want "response body must be closed"
28+
_, _ = ioutil.ReadAll(resp.Body)
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//golangcitest:args -Ecanonicalheader
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"net/http"
16+
"unsafe"
17+
)
18+
19+
func _() {
20+
cs := C.CString("Hello from stdio\n")
21+
C.myprint(cs)
22+
C.free(unsafe.Pointer(cs))
23+
}
24+
25+
func canonicalheader() {
26+
v := http.Header{}
27+
28+
v.Get("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
29+
v.Set("Test-HEader", "value") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
30+
v.Add("Test-HEader", "value") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
31+
v.Del("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
32+
v.Values("Test-HEader") // want `non-canonical header "Test-HEader", instead use: "Test-Header"`
33+
34+
v.Values("Sec-WebSocket-Accept")
35+
36+
v.Set("Test-Header", "value")
37+
v.Add("Test-Header", "value")
38+
v.Del("Test-Header")
39+
v.Values("Test-Header")
40+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//golangcitest:args -Econtainedctx
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"context"
16+
"unsafe"
17+
)
18+
19+
func _() {
20+
cs := C.CString("Hello from stdio\n")
21+
C.myprint(cs)
22+
C.free(unsafe.Pointer(cs))
23+
}
24+
25+
type ok struct {
26+
i int
27+
s string
28+
}
29+
30+
type ng struct {
31+
ctx context.Context // want "found a struct that contains a context.Context field"
32+
}
33+
34+
type empty struct{}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//golangcitest:args -Econtextcheck
2+
package testdata
3+
4+
/*
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
void myprint(char* s) {
9+
printf("%d\n", s);
10+
}
11+
*/
12+
import "C"
13+
14+
import (
15+
"context"
16+
"unsafe"
17+
)
18+
19+
func _() {
20+
cs := C.CString("Hello from stdio\n")
21+
C.myprint(cs)
22+
C.free(unsafe.Pointer(cs))
23+
}
24+
25+
func contextcheckCase1(ctx context.Context) {
26+
funcWithoutCtx() // want "Function `funcWithoutCtx` should pass the context parameter"
27+
}
28+
29+
func funcWithCtx(ctx context.Context) {}
30+
31+
func funcWithoutCtx() {
32+
funcWithCtx(context.TODO())
33+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build go1.22
2+
3+
//golangcitest:args -Ecopyloopvar
4+
package testdata
5+
6+
/*
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
void myprint(char* s) {
11+
printf("%d\n", s);
12+
}
13+
*/
14+
import "C"
15+
16+
import (
17+
"fmt"
18+
"unsafe"
19+
)
20+
21+
func _() {
22+
cs := C.CString("Hello from stdio\n")
23+
C.myprint(cs)
24+
C.free(unsafe.Pointer(cs))
25+
}
26+
27+
func copyloopvarCase2() {
28+
loopCount := 3
29+
fns := make([]func(), 0, loopCount)
30+
for i := 1; i <= loopCount; i++ {
31+
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
32+
fns = append(fns, func() {
33+
fmt.Println(i)
34+
})
35+
}
36+
for _, fn := range fns {
37+
fn()
38+
}
39+
}

Diff for: pkg/golinters/cyclop/testdata/cyclop_cgo.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//golangcitest:args -Ecyclop
2+
//golangcitest:config_path testdata/cyclop.yml
3+
package testdata
4+
5+
/*
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
void myprint(char* s) {
10+
printf("%d\n", s);
11+
}
12+
*/
13+
import "C"
14+
15+
import (
16+
"unsafe"
17+
)
18+
19+
func _() {
20+
cs := C.CString("Hello from stdio\n")
21+
C.myprint(cs)
22+
C.free(unsafe.Pointer(cs))
23+
}
24+
25+
func cyclopComplexFunc(s string) { // want "calculated cyclomatic complexity for function cyclopComplexFunc is 22, max is 15"
26+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
27+
return
28+
}
29+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
30+
return
31+
}
32+
if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
33+
return
34+
}
35+
}

Diff for: pkg/golinters/decorder/testdata/decorder_cgo.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//golangcitest:args -Edecorder
2+
//golangcitest:config_path testdata/decorder_custom.yml
3+
package testdata
4+
5+
/*
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
void myprint(char* s) {
10+
printf("%d\n", s);
11+
}
12+
*/
13+
import "C"
14+
15+
import (
16+
"math"
17+
"unsafe"
18+
)
19+
20+
const (
21+
decoc = math.MaxInt64
22+
decod = 1
23+
)
24+
25+
var decoa = 1
26+
var decob = 1 // want "multiple \"var\" declarations are not allowed; use parentheses instead"
27+
28+
type decoe int // want "type must not be placed after const"
29+
30+
func decof() {
31+
const decog = 1
32+
}
33+
34+
func init() {} // want "init func must be the first function in file"
35+
36+
func _() {
37+
cs := C.CString("Hello from stdio\n")
38+
C.myprint(cs)
39+
C.free(unsafe.Pointer(cs))
40+
}

Diff for: pkg/golinters/depguard/testdata/depguard_cgo.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//golangcitest:args -Edepguard
2+
//golangcitest:config_path testdata/depguard.yml
3+
package testdata
4+
5+
/*
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
void myprint(char* s) {
10+
printf("%d\n", s);
11+
}
12+
*/
13+
import "C"
14+
15+
import (
16+
"compress/gzip" // want "import 'compress/gzip' is not allowed from list 'main': nope"
17+
"log" // want "import 'log' is not allowed from list 'main': don't use log"
18+
"unsafe"
19+
20+
"golang.org/x/tools/go/analysis" // want "import 'golang.org/x/tools/go/analysis' is not allowed from list 'main': example import with dot"
21+
)
22+
23+
func _() {
24+
cs := C.CString("Hello from stdio\n")
25+
C.myprint(cs)
26+
C.free(unsafe.Pointer(cs))
27+
}
28+
29+
func SpewDebugInfo() {
30+
log.Println(gzip.BestCompression)
31+
_ = analysis.Analyzer{}
32+
}

0 commit comments

Comments
 (0)