1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package lintertest
import (
"flag"
"fmt"
"log"
"os"
"testing"
"github.com/google/syzkaller/pkg/tool"
)
/* some comment */ // want "Use C-style comments // instead of /* */"
var comment = 1 /* some comment */ // want "Use C-style comments // instead of /* */"
func stringComparison() {
str := ""
if len(str) == 0 { // want "Compare string with \"\", don't compare len with 0"
}
if 0 != len(str) { // want "Compare string with \"\", don't compare len with 0"
}
if len(returnString()+"foo") > 0 { // want "Compare string with \"\", don't compare len with 0"
}
}
func returnString() string { return "foo" }
//
// One space.
// Two spaces.
// One tab.
// Two tabs.
//No space. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
// Tab and spaces. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
// Space and tab. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
func checkCommentSpace() {
checkCommentSpace() // lower-case comment is OK
// Capital letter comment.
checkCommentSpace()
// Don't use 2 spaces after dot. Like this. // want "Use one space after a period"
checkCommentSpace()
}
//No space. // want "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments"
func funcArgsGood(a, b int) (int, int) {
return 0, 0
}
func funcArgsGood2(a []int, b ...int) {
}
func funcArgsBad0(a int, b int) { // want "Use 'a, b int'"
}
func funcArgsBad1() (a int, b int) { // want "Use 'a, b int'"
return 0, 0 // lower-case comment is OK
}
func funcArgsBad2(a int16, b, c uint32, d uint32, e int16) { // want "Use 'b, c, d uint32'"
}
type Foo struct{}
func funcArgsBad3(s string, b *Foo, c *Foo) { // want "b, c \\*lintertest\\.Foo"
}
func flagDefinitions() {
flag.Int("good", 0, "fine description")
flag.Int("camelCase", 0, "fine description") // want "Don't use Capital letters in flag names"
flag.String("fine", "", "Capital Letter") // want "Don't start flag description with a Capital letter"
flag.Bool("fine", false, "dot at the end.") // want "Don't use '.' at the end of flag description"
}
func logErrorMessages() {
msg := "good message"
err := fmt.Errorf("good message")
fmt.Errorf("good message %v", 0)
fmt.Errorf(msg)
log.Printf("good message")
log.Print("good message")
log.Print("Using.An.Identifier is ok as well")
log.Print(msg)
fmt.Printf("%s", msg)
fmt.Printf("fragment")
fmt.Printf("Fragment Fragment %s", msg)
fmt.Fprintf(nil, "These can be anything")
tool.Fail(err)
tool.Failf("good message")
tool.Failf("good message %v", 0)
fmt.Errorf("Bad message") // want "Don't start log/error messages with a Capital letter"
log.Fatalf("Bad message %v", 1) // want "Don't start log/error messages with a Capital letter"
log.Printf("Bad message %v", 1) // want "Don't start log/error messages with a Capital letter"
log.Print("Bad message") // want "Don't start log/error messages with a Capital letter"
log.Print("also ad message.") // want "Don't use period at the end of log/error messages"
log.Print("no new lines\n") // want "Don't use \\\\n at the end of log/error messages"
log.Print("") // want "Don't use empty log/error messages"
fmt.Printf("Real output message with capital letter\n") // want "Don't start log/error messages with a Capital letter"
fmt.Printf("real output message without newline") // want "Add \\\\n at the end of printed messages"
fmt.Fprintf(os.Stderr, "Real output message with capital letter\n") // want "Don't start log/error messages with a Capital letter"
fmt.Fprintf(os.Stderr, "real output message without newline") // want "Add \\\\n at the end of printed messages"
fmt.Fprintf(os.Stderr, "%v", err) // want "Add \\\\n at the end of printed messages"
tool.Failf("Bad message") // want "Don't start log/error messages with a Capital letter"
}
func testMessages(t *testing.T) {
t.Logf("good message %v", 1)
t.Logf("Bad message %v", 1) // want "Don't start log/error messages with a Capital letter"
t.Errorf("bad message %v\n", 1) // want "Don't use \\\\n at the end of log/error messages"
t.Fatalf("Bad message %v", 1) // want "Don't start log/error messages with a Capital letter"
t.Fatalf("PublicFunc is ok %v", 1)
}
func varDecls() {
var a int
b := 0
c := int64(0)
var _ int = 0
var d int = 0 // want "Don't use both var, type and value in variable declarations"
_, _, _, _ = a, b, c, d
}
func minmax() {
x, y := 0, 0
if x < y + 1 { // want "Use max function instead"
x = y + 1
}
if x >= y { // want "Use max function instead"
y = x
}
if x > 10 { // want "Use min function instead"
x = 10
}
}
func loopvar() {
s := []int{1, 2, 3}
for i, v := range s {
i, v := i, v // want "Don't duplicate loop variables.*"
_, _ = i, v
}
}
func anyInterface() interface{} { // want "Use any instead of interface{}"
var v interface{} // want "Use any instead of interface{}"
func(interface{}) {} (v) // want "Use any instead of interface{}"
var y any
func(any) {} (y)
return v
}
|