1// Test for name linting.
2
3// Package pkg_with_underscores ...
4package pkg_with_underscores // MATCH /underscore.*package name/
5
6import (
7	"io"
8	"net"
9	net_http "net/http" // renamed deliberately
10	"net/url"
11)
12
13import "C"
14
15var var_name int // MATCH /underscore.*var.*var_name/
16
17type t_wow struct { // MATCH /underscore.*type.*t_wow/
18	x_damn int      // MATCH /underscore.*field.*x_damn/
19	Url    *url.URL // MATCH /struct field.*Url.*URL/
20}
21
22const fooId = "blah" // MATCH /fooId.*fooID/
23
24func f_it() { // MATCH /underscore.*func.*f_it/
25	more_underscore := 4 // MATCH /underscore.*var.*more_underscore/
26	_ = more_underscore
27	var err error
28	if isEof := (err == io.EOF); isEof { // MATCH /var.*isEof.*isEOF/
29		more_underscore = 7 // should be okay
30	}
31
32	x := net_http.Request{} // should be okay
33	_ = x
34
35	var ips []net.IP
36	for _, theIp := range ips { // MATCH /range var.*theIp.*theIP/
37		_ = theIp
38	}
39
40	switch myJson := g(); { // MATCH /var.*myJson.*myJSON/
41	default:
42		_ = myJson
43	}
44	var y net_http.ResponseWriter // an interface
45	switch tApi := y.(type) {     // MATCH /var.*tApi.*tAPI/
46	default:
47		_ = tApi
48	}
49
50	var c chan int
51	select {
52	case qId := <-c: // MATCH /var.*qId.*qID/
53		_ = qId
54	}
55}
56
57// Common styles in other languages that don't belong in Go.
58const (
59	CPP_CONST   = 1 // MATCH /ALL_CAPS.*CamelCase/
60	kLeadingKay = 2 // MATCH /k.*leadingKay/
61
62	HTML    = 3 // okay; no underscore
63	X509B   = 4 // ditto
64	V1_10_5 = 5 // okay; fewer than two uppercase letters
65)
66
67var kVarsAreSometimesUsedAsConstants = 0 // MATCH /k.*varsAreSometimesUsedAsConstants/
68var (
69	kVarsAreSometimesUsedAsConstants2 = 0 // MATCH /k.*varsAreSometimesUsedAsConstants2/
70)
71
72var kThisIsNotOkay = struct { // MATCH /k.*thisIsNotOkay/
73	kThisIsOkay bool
74}{}
75
76func kThisIsOkay() { // this is okay because this is a function name
77	var kThisIsAlsoOkay = 1 // this is okay because this is a non-top-level variable
78	_ = kThisIsAlsoOkay
79	const kThisIsNotOkay = 2 // MATCH /k.*thisIsNotOkay/
80}
81
82var anotherFunctionScope = func() {
83	var kThisIsOkay = 1 // this is okay because this is a non-top-level variable
84	_ = kThisIsOkay
85	const kThisIsNotOkay = 2 // MATCH /k.*thisIsNotOkay/}
86}
87
88func f(bad_name int)                    {}            // MATCH /underscore.*func parameter.*bad_name/
89func g() (no_way int)                   { return 0 }  // MATCH /underscore.*func result.*no_way/
90func (t *t_wow) f(more_under string)    {}            // MATCH /underscore.*method parameter.*more_under/
91func (t *t_wow) g() (still_more string) { return "" } // MATCH /underscore.*method result.*still_more/
92
93type i interface {
94	CheckHtml() string // okay; interface method names are often constrained by the concrete types' method names
95
96	F(foo_bar int) // MATCH /foo_bar.*fooBar/
97}
98
99// All okay; underscore between digits
100const case1_1 = 1
101
102type case2_1 struct {
103	case2_2 int
104}
105
106func case3_1(case3_2 int) (case3_3 string) {
107	case3_4 := 4
108	_ = case3_4
109
110	return ""
111}
112
113type t struct{}
114
115func (t) LastInsertId() (int64, error) { return 0, nil } // okay because it matches a known style violation
116
117//export exported_to_c
118func exported_to_c() {} // okay: https://github.com/golang/lint/issues/144
119
120//export exported_to_c_with_arg
121func exported_to_c_with_arg(but_use_go_param_names int) // MATCH /underscore.*func parameter.*but_use_go_param_names/
122
123// This is an exported C function with a leading doc comment.
124//
125//export exported_to_c_with_comment
126func exported_to_c_with_comment() {} // okay: https://github.com/golang/lint/issues/144
127
128//export maybe_exported_to_CPlusPlusWithCamelCase
129func maybe_exported_to_CPlusPlusWithCamelCase() {} // okay: https://github.com/golang/lint/issues/144
130
131// WhyAreYouUsingCapitalLetters_InACFunctionName is a Go-exported function that
132// is also exported to C as a name with underscores.
133//
134// Don't do that. If you want to use a C-style name for a C export, make it
135// lower-case and leave it out of the Go-exported API.
136//
137//export WhyAreYouUsingCapitalLetters_InACFunctionName
138func WhyAreYouUsingCapitalLetters_InACFunctionName() {} // MATCH /underscore.*func.*Why.*CFunctionName/
139