How to use checkVarDecl method of main Package

Best Syzkaller code snippet using main.checkVarDecl

linter.go

Source:linter.go Github

copy

Full Screen

...66 case *ast.CallExpr:67 pass.checkFlagDefinition(n)68 pass.checkLogErrorFormat(n)69 case *ast.GenDecl:70 pass.checkVarDecl(n)71 }72 return true73 })74 for _, group := range file.Comments {75 for _, comment := range group.List {76 pass.checkComment(comment, stmts, len(group.List) == 1)77 }78 }79 }80 return nil, nil81}82type Pass analysis.Pass83func (pass *Pass) report(pos ast.Node, msg string, args ...interface{}) {84 pass.Report(analysis.Diagnostic{85 Pos: pos.Pos(),86 Message: fmt.Sprintf(msg, args...),87 })88}89func (pass *Pass) typ(e ast.Expr) types.Type {90 return pass.TypesInfo.Types[e].Type91}92// checkComment warns about C++-style multiline comments (we don't use them in the codebase)93// and about "//nospace", "// tabs and spaces", two spaces after a period, etc.94// See the following sources for some justification:95// https://pep8.org/#comments96// https://nedbatchelder.com/blog/201401/comments_should_be_sentences.html97// https://www.cultofpedagogy.com/two-spaces-after-period98func (pass *Pass) checkComment(n *ast.Comment, stmts map[int]bool, oneline bool) {99 if strings.HasPrefix(n.Text, "/*") {100 pass.report(n, "Use C-style comments // instead of /* */")101 return102 }103 if specialComment.MatchString(n.Text) {104 return105 }106 if !allowedComments.MatchString(n.Text) {107 pass.report(n, "Use either //<one-or-more-spaces>comment or //<one-or-more-tabs>comment format for comments")108 return109 }110 if strings.Contains(n.Text, ". ") {111 pass.report(n, "Use one space after a period")112 return113 }114 if !oneline || onelineExceptions.MatchString(n.Text) {115 return116 }117 // The following checks are only done for one-line comments,118 // because multi-line comment blocks are harder to understand.119 standalone := !stmts[pass.Fset.Position(n.Pos()).Line]120 if standalone && lowerCaseComment.MatchString(n.Text) {121 pass.report(n, "Standalone comments should be complete sentences"+122 " with first word capitalized and a period at the end")123 }124 if noPeriodComment.MatchString(n.Text) {125 pass.report(n, "Add a period at the end of the comment")126 return127 }128}129var (130 allowedComments = regexp.MustCompile(`^//($| +[^ ]| +[^ ])`)131 noPeriodComment = regexp.MustCompile(`^// [A-Z][a-z].+[a-z]$`)132 lowerCaseComment = regexp.MustCompile(`^// [a-z]+ `)133 onelineExceptions = regexp.MustCompile(`// want \"|http:|https:`)134 specialComment = regexp.MustCompile(`//go:generate|//go:build|//go:embed|// nolint:`)135)136// checkStringLenCompare checks for string len comparisons with 0.137// E.g.: if len(str) == 0 {} should be if str == "" {}.138func (pass *Pass) checkStringLenCompare(n *ast.BinaryExpr) {139 if n.Op != token.EQL && n.Op != token.NEQ && n.Op != token.LSS &&140 n.Op != token.GTR && n.Op != token.LEQ && n.Op != token.GEQ {141 return142 }143 if pass.isStringLenCall(n.X) && pass.isIntZeroLiteral(n.Y) ||144 pass.isStringLenCall(n.Y) && pass.isIntZeroLiteral(n.X) {145 pass.report(n, "Compare string with \"\", don't compare len with 0")146 }147}148func (pass *Pass) isStringLenCall(n ast.Expr) bool {149 call, ok := n.(*ast.CallExpr)150 if !ok || len(call.Args) != 1 {151 return false152 }153 fun, ok := call.Fun.(*ast.Ident)154 if !ok || fun.Name != "len" {155 return false156 }157 return pass.typ(call.Args[0]).String() == "string"158}159func (pass *Pass) isIntZeroLiteral(n ast.Expr) bool {160 lit, ok := n.(*ast.BasicLit)161 return ok && lit.Kind == token.INT && lit.Value == "0"162}163// checkFuncArgs checks for "func foo(a int, b int)" -> "func foo(a, b int)".164func (pass *Pass) checkFuncArgs(n *ast.FuncType) {165 pass.checkFuncArgList(n.Params.List)166 if n.Results != nil {167 pass.checkFuncArgList(n.Results.List)168 }169}170func (pass *Pass) checkFuncArgList(fields []*ast.Field) {171 firstBad := -1172 var prev types.Type173 for i, field := range fields {174 if len(field.Names) == 0 {175 pass.reportFuncArgs(fields, firstBad, i)176 firstBad, prev = -1, nil177 continue178 }179 this := pass.typ(field.Type)180 if prev != this {181 pass.reportFuncArgs(fields, firstBad, i)182 firstBad, prev = -1, this183 continue184 }185 if firstBad == -1 {186 firstBad = i - 1187 }188 }189 pass.reportFuncArgs(fields, firstBad, len(fields))190}191func (pass *Pass) reportFuncArgs(fields []*ast.Field, first, last int) {192 if first == -1 {193 return194 }195 names := ""196 for _, field := range fields[first:last] {197 for _, name := range field.Names {198 names += ", " + name.Name199 }200 }201 pass.report(fields[first], "Use '%v %v'", names[2:], fields[first].Type)202}203func (pass *Pass) checkFlagDefinition(n *ast.CallExpr) {204 fun, ok := n.Fun.(*ast.SelectorExpr)205 if !ok {206 return207 }208 switch fmt.Sprintf("%v.%v", fun.X, fun.Sel) {209 case "flag.Bool", "flag.Duration", "flag.Float64", "flag.Int", "flag.Int64",210 "flag.String", "flag.Uint", "flag.Uint64":211 default:212 return213 }214 if name, ok := stringLit(n.Args[0]); ok {215 if name != strings.ToLower(name) {216 pass.report(n, "Don't use Capital letters in flag names")217 }218 }219 if desc, ok := stringLit(n.Args[2]); ok {220 if desc == "" {221 pass.report(n, "Provide flag description")222 } else if last := desc[len(desc)-1]; last == '.' || last == '\n' {223 pass.report(n, "Don't use %q at the end of flag description", last)224 }225 if len(desc) >= 2 && unicode.IsUpper(rune(desc[0])) && unicode.IsLower(rune(desc[1])) {226 pass.report(n, "Don't start flag description with a Capital letter")227 }228 }229}230// checkLogErrorFormat warns about log/error messages starting with capital letter or ending with a period.231func (pass *Pass) checkLogErrorFormat(n *ast.CallExpr) {232 arg, newLine, sure := pass.logFormatArg(n)233 if arg == -1 {234 return235 }236 val, ok := stringLit(n.Args[arg])237 if !ok {238 return239 }240 ln := len(val)241 if ln == 0 {242 pass.report(n, "Don't use empty log/error messages")243 return244 }245 // Some Printf's legitimately don't need \n, so this check is based on a heuristic.246 // Printf's that don't need \n tend to contain % and are short.247 if !sure && ln < 25 && (ln < 10 || strings.Contains(val, "%")) {248 return249 }250 if val[ln-1] == '.' && (ln < 3 || val[ln-2] != '.' || val[ln-3] != '.') {251 pass.report(n, "Don't use period at the end of log/error messages")252 }253 if newLine && val[ln-1] != '\n' {254 pass.report(n, "Add \\n at the end of printed messages")255 }256 if !newLine && val[ln-1] == '\n' {257 pass.report(n, "Don't use \\n at the end of log/error messages")258 }259 if ln >= 2 && unicode.IsUpper(rune(val[0])) && unicode.IsLower(rune(val[1])) &&260 !publicIdentifier.MatchString(val) {261 pass.report(n, "Don't start log/error messages with a Capital letter")262 }263}264func (pass *Pass) logFormatArg(n *ast.CallExpr) (arg int, newLine, sure bool) {265 fun, ok := n.Fun.(*ast.SelectorExpr)266 if !ok {267 return -1, false, false268 }269 switch fmt.Sprintf("%v.%v", fun.X, fun.Sel) {270 case "log.Print", "log.Printf", "log.Fatal", "log.Fatalf", "fmt.Error", "fmt.Errorf":271 return 0, false, true272 case "log.Logf":273 return 1, false, true274 case "fmt.Print", "fmt.Printf":275 return 0, true, false276 case "fmt.Fprint", "fmt.Fprintf":277 if w, ok := n.Args[0].(*ast.SelectorExpr); !ok || fmt.Sprintf("%v.%v", w.X, w.Sel) != "os.Stderr" {278 break279 }280 return 1, true, true281 }282 return -1, false, false283}284var publicIdentifier = regexp.MustCompile(`^[A-Z][[:alnum:]]+(\.[[:alnum:]]+)+ `)285func stringLit(n ast.Node) (string, bool) {286 lit, ok := n.(*ast.BasicLit)287 if !ok || lit.Kind != token.STRING {288 return "", false289 }290 val, err := strconv.Unquote(lit.Value)291 if err != nil {292 return "", false293 }294 return val, true295}296// checkVarDecl warns about unnecessary long variable declarations "var x type = foo".297func (pass *Pass) checkVarDecl(n *ast.GenDecl) {298 if n.Tok != token.VAR {299 return300 }301 for _, s := range n.Specs {302 spec, ok := s.(*ast.ValueSpec)303 if !ok || spec.Type == nil || len(spec.Values) == 0 || spec.Names[0].Name == "_" {304 continue305 }306 pass.report(n, "Don't use both var, type and value in variable declarations\n"+307 "Use either \"var x type\" or \"x := val\" or \"x := type(val)\"")308 }309}...

Full Screen

Full Screen

checkVarDecl

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, f)8 for _, s := range f.Imports {9 fmt.Println(s.Path.Value)10 }11 if len(f.Comments) > 0 {12 fmt.Println(f.Comments[0].Text())13 }14 if len(f.Decls) > 0 {15 if decl, ok := f.Decls[0].(*ast.FuncDecl); ok {16 fmt.Println(decl.Name.Name)17 }18 }19 for _, decl := range f.Decls {20 if decl, ok := decl.(*ast.FuncDecl); ok {21 fmt.Println(decl.Name.Name)22 }23 }24 for _, decl := range f.Decls {25 switch decl := decl.(type) {26 fmt.Println(decl.Name.Name)27 for _, spec := range decl.Specs {28 if spec, ok := spec.(*ast.TypeSpec); ok {29 fmt.Println(spec.Name.Name)30 }31 }32 }33 }34 for _, decl := range f.Decls {35 ast.Inspect(decl, func(n ast.Node) bool {36 switch n := n.(type) {37 fmt.Println(n.Name)38 }39 })40 }41 for _, decl := range f.Decls {42 ast.Inspect(decl, func(n ast.Node) bool {43 switch n := n.(type

Full Screen

Full Screen

checkVarDecl

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Inspect(f, func(n ast.Node) bool {8 switch x := n.(type) {9 for _, spec := range x.Specs {10 switch spec := spec.(type) {11 for _, name := range spec.Names {12 if name.Name == "a" {13 fmt.Println(name.Name)14 fmt.Println(name.Obj.Kind)15 fmt.Println(name.Obj.Decl)16 fmt.Println(name.Obj.Data)17 fmt.Println(name.Obj.Parent)18 fmt.Println(name.Obj.Pos())19 fmt.Println(name.Obj.End())20 }21 }22 }23 }24 }25 })26}

Full Screen

Full Screen

checkVarDecl

Using AI Code Generation

copy

Full Screen

1checkVarDecl("int", "a", "2.go", 1);2checkVarDecl("int", "b", "2.go", 1);3checkVarDecl("int", "c", "2.go", 1);4checkVarDecl("int", "d", "2.go", 1);5checkVarDecl("int", "e", "2.go", 1);6checkVarDecl("int", "f", "2.go", 1);7checkVarDecl("int", "g", "2.go", 1);8checkVarDecl("int", "h", "2.go", 1);9checkVarDecl("int", "i", "2.go", 1);10checkVarDecl("int", "j", "2.go", 1);11checkVarDecl("int", "k", "2.go", 1);12checkVarDecl("int", "l", "2.go", 1);13checkVarDecl("int", "m", "2.go", 1);14checkVarDecl("int", "n", "2.go", 1);15checkVarDecl("int", "o", "2.go", 1);16checkVarDecl("int", "p", "2.go", 1);17checkVarDecl("int", "q", "2.go", 1);18checkVarDecl("int", "r", "2.go", 1);19checkVarDecl("int", "s", "2.go", 1);20checkVarDecl("int", "t", "2.go", 1);21checkVarDecl("int", "u", "2.go", 1);22checkVarDecl("int", "v", "2.go", 1);23checkVarDecl("int", "w", "2.go", 1);24checkVarDecl("int", "x", "2.go", 1);25checkVarDecl("int", "y", "2.go", 1);26checkVarDecl("int", "z", "2.go", 1);27checkVarDecl("int", "a1", "2.go", 1);28checkVarDecl("int", "b1", "2.go", 1);29checkVarDecl("int", "c1", "2.go", 1);30checkVarDecl("int", "d1", "2.go", 1);31checkVarDecl("int", "e1", "2.go",

Full Screen

Full Screen

checkVarDecl

Using AI Code Generation

copy

Full Screen

1main.checkVarDecl("var1", "int")2main.checkVarDecl("var2", "int")3main.checkVarDecl("var3", "boolean")4main.checkVarDecl("var4", "int")5main.checkVarDecl("var5", "int")6main.checkVarDecl("var6", "int")7main.checkVarDecl("var7", "int")8main.checkVarDecl("var8", "int")9main.checkVarDecl("var9", "int")10main.checkVarDecl("var10", "int")11main.checkVarDecl("var11", "int")12main.checkVarDecl("var12", "int")13main.checkVarDecl("var13", "int")14main.checkVarDecl("var14", "int")15main.checkVarDecl("var15", "int")16main.checkVarDecl("var16", "int")17main.checkVarDecl("var17", "int")18main.checkVarDecl("var18", "int")19main.checkVarDecl("var19", "int")20main.checkVarDecl("var20", "int")21main.checkVarDecl("var21", "int")22main.checkVarDecl("var22", "int")23main.checkVarDecl("var23", "int")24main.checkVarDecl("var24", "int")25main.checkVarDecl("var25", "int")26main.checkVarDecl("var26", "int")27main.checkVarDecl("var27", "int")28main.checkVarDecl("var28", "int")29main.checkVarDecl("var29", "int")30main.checkVarDecl("var30", "int")31main.checkVarDecl("var31", "int")32main.checkVarDecl("var32", "int")33main.checkVarDecl("var33", "int")34main.checkVarDecl("var34", "int")35main.checkVarDecl("var35", "int")36main.checkVarDecl("var36", "int")37main.checkVarDecl("var37", "int")38main.checkVarDecl("var38", "int")39main.checkVarDecl("var39", "int")40main.checkVarDecl("var40", "int")41main.checkVarDecl("var41", "int")42main.checkVarDecl("var42", "int")43main.checkVarDecl("var43", "int")44main.checkVarDecl("var44", "int")45main.checkVarDecl("var45", "int")

Full Screen

Full Screen

checkVarDecl

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(a, b, c)4}5import "fmt"6func main() {7 fmt.Println(a, b, c)8}9import "fmt"10func main() {11 fmt.Println(a, b, c)12}13import "fmt"14func main() {15 fmt.Println(a, b, c)16}17import "fmt"18func main() {19 fmt.Println(a, b, c)20}21import "fmt"22func main() {23 fmt.Println(a, b, c)24}25import "fmt"26func main() {27 fmt.Println(a, b, c)28 fmt.Println(a, b, c)29}30import "fmt"31func main() {32 fmt.Println(a, b, c)33 fmt.Println(a, b, c)

Full Screen

Full Screen

checkVarDecl

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter the number of variables")4 fmt.Scan(&n)5 fmt.Println("Enter the variables")6 myMap := make(map[string]int)7 for i := 0; i < n; i++ {8 fmt.Scan(&varName)9 fmt.Scan(&varVal)10 }11 fmt.Println("Enter the number of statements")12 fmt.Scan(&n2)13 fmt.Println("Enter the statements")14 for i := 0; i < n2; i++ {15 fmt.Scan(&stmt)16 main.checkVarDecl(stmt, myMap)17 }18}19import (20func main() {21 fmt.Println("Enter the number of variables")22 fmt.Scan(&n)23 fmt.Println("Enter the variables")24 myMap := make(map[string]int)25 for i := 0; i < n; i++ {26 fmt.Scan(&varName)27 fmt.Scan(&varVal)28 }29 fmt.Println("Enter the number of statements")30 fmt.Scan(&n2)31 fmt.Println("Enter the statements")32 for i := 0; i < n2; i++ {33 fmt.Scan(&stmt)34 main.checkVarDecl(stmt, myMap)35 }36}37func (m main) checkVarDecl(stmt string, myMap map[string]int) {38 stmt = strings.TrimSpace(stmt)39 if strings.Contains(stmt, "var") {40 r, _ := regexp.Compile("[a-zA-Z0-9]+")41 varName = r.FindString(stmt)42 if strings.Contains(stmt, "=") {43 r, _ := regexp.Compile("[0-9]+")44 varVal = r.FindString(stmt)45 }46 fmt.Println("Variable declaration statement")47 fmt.Println("The variable name is", varName)48 fmt.Println("The value of variable is

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful