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