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