utils

package
v1.3.97 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package utils provides AST utility functions for analyzers.

Package utils provides AST utility functions for analyzers.

Package utils provides AST utility functions for analyzers.

Package utils provides AST utility functions for analyzers.

Package utils provides AST utility functions for analyzers.

Package utils provides AST utility functions for analyzers.

Package utils provides AST utility functions for analyzers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractVarName

func ExtractVarName(expr ast.Expr) string

ExtractVarName extracts the variable name from a complex expression.

Params:

  • expr: expression to analyze

Returns:

  • string: variable name or representation

func GetExprAsString

func GetExprAsString(expr ast.Expr) string

GetExprAsString converts an AST expression to its textual representation.

Params:

  • expr: the AST expression to convert

Returns:

  • string: the textual representation

func GetIdentName

func GetIdentName(expr ast.Expr) string

GetIdentName extracts the name of an identifier from an expression.

Params:

  • expr: expression to analyze

Returns:

  • string: identifier name or empty string

func GetTypeName

func GetTypeName(expr ast.Expr) string

GetTypeName extracts the type name from an expression.

Params:

  • expr: the expression representing the type

Returns:

  • string: the type name (e.g.: "map[string]int", "[]int", "chan int")

func GetTypeString

func GetTypeString(spec *ast.ValueSpec) string

GetTypeString extracts the textual representation of a ValueSpec's type.

Params:

  • spec: the ValueSpec from which to extract the type

Returns:

  • string: the textual representation of the type, or "<type>" if not specified

func HasPositiveLength

func HasPositiveLength(pass *analysis.Pass, lengthArg ast.Expr) bool

HasPositiveLength checks if a length argument is > 0.

Params:

  • pass: analysis context
  • lengthArg: length argument

Returns:

  • bool: true if the length is positive

func IsAllCaps

func IsAllCaps(s string) bool

IsAllCaps checks if a string is entirely uppercase.

Params:

  • s: the string to check

Returns:

  • bool: true if at least one letter is present and all are uppercase

func IsBuiltinCall

func IsBuiltinCall(call *ast.CallExpr, builtinName string) bool

IsBuiltinCall checks if it's a call to a builtin function.

Params:

  • call: call expression to check
  • builtinName: builtin function name

Returns:

  • bool: true if it's a call to the specified builtin

func IsByteSlice

func IsByteSlice(expr ast.Expr) bool

IsByteSlice checks if a type is []byte via AST.

Params:

  • expr: AST expression to check

Returns:

  • bool: true if it is a []byte based on AST

func IsByteSliceWithPass

func IsByteSliceWithPass(pass *analysis.Pass, expr ast.Expr) bool

IsByteSliceWithPass checks if a type is []byte.

Params:

  • pass: analysis context
  • expr: type expression to check

Returns:

  • bool: true if it is a []byte

func IsEmptySliceLiteral

func IsEmptySliceLiteral(lit *ast.CompositeLit) bool

IsEmptySliceLiteral checks if a CompositeLit is an empty slice literal.

Params:

  • lit: composite literal to check

Returns:

  • bool: true if it is an empty slice literal ([]T{})

func IsGeneratedFile

func IsGeneratedFile(file *ast.File) bool

IsGeneratedFile checks if a file is generated (contains "Code generated" comment).

Params:

  • file: the AST file to check

Returns:

  • bool: true if the file is generated

func IsIdentCall

func IsIdentCall(call *ast.CallExpr, name string) bool

IsIdentCall checks if a CallExpr is a call to a function with a specific name.

Params:

  • call: call expression to check
  • name: expected function name

Returns:

  • bool: true if it's a call to the named function

func IsMakeByteSliceCall

func IsMakeByteSliceCall(call *ast.CallExpr) bool

IsMakeByteSliceCall checks if it's a make call to create []byte.

Params:

  • call: call expression to check

Returns:

  • bool: true if it's make([]byte, ...)

func IsMakeCall

func IsMakeCall(call *ast.CallExpr) bool

IsMakeCall checks if a CallExpr is a call to make.

Params:

  • call: call expression to check

Returns:

  • bool: true if it's a call to make

func IsMakeCallWithLength

func IsMakeCallWithLength(call *ast.CallExpr, minArgs int) bool

IsMakeCallWithLength checks if it's a make call with a specified length.

Params:

  • call: call expression to check
  • minArgs: minimum number of expected arguments

Returns:

  • bool: true if it's a make call with at least minArgs arguments

func IsMakeMapCall

func IsMakeMapCall(call *ast.CallExpr) bool

IsMakeMapCall checks if it's a make call to create a map.

Params:

  • call: call expression to check

Returns:

  • bool: true if it's make(map[K]V, ...)

func IsMakeSliceCall

func IsMakeSliceCall(call *ast.CallExpr) bool

IsMakeSliceCall checks if it's a make call to create a slice.

Params:

  • call: call expression to check

Returns:

  • bool: true if it's make([]T, ...)

func IsMakeSliceZero

func IsMakeSliceZero(expr ast.Expr) bool

IsMakeSliceZero checks if an expression is make([]T, 0) or make([]T, 0, 0).

Params:

  • expr: the expression to check

Returns:

  • bool: true if it's make([]T, 0) or make([]T, 0, 0)

func IsMapType

func IsMapType(expr ast.Expr) bool

IsMapType checks if an expression is a map type using the AST.

Params:

  • expr: AST expression to check

Returns:

  • bool: true if it is a map type based on AST

func IsMapTypeWithPass

func IsMapTypeWithPass(pass *analysis.Pass, expr ast.Expr) bool

IsMapTypeWithPass checks if an expression is a map type using TypesInfo.

Params:

  • pass: analysis context with TypesInfo
  • expr: expression to check

Returns:

  • bool: true if it is a map type

func IsMixedCaps

func IsMixedCaps(name string) bool

IsMixedCaps checks if a name follows the MixedCaps/mixedCaps convention.

Params:

  • name: the name to validate

Returns:

  • bool: true if valid (no snake_case, no ALL_CAPS except initialisms)

func IsReferenceType

func IsReferenceType(expr ast.Expr) bool

IsReferenceType checks if a type is a reference type (slice/map/chan).

Params:

  • expr: the expression representing the type

Returns:

  • bool: true if it's a slice, map or channel

func IsSliceOrMapType

func IsSliceOrMapType(expr ast.Expr) bool

IsSliceOrMapType checks if an expression is a slice or a map via AST.

Params:

  • expr: AST expression to check

Returns:

  • bool: true if it is a slice or a map based on AST

func IsSliceOrMapTypeWithPass

func IsSliceOrMapTypeWithPass(pass *analysis.Pass, expr ast.Expr) bool

IsSliceOrMapTypeWithPass checks if an expression is a slice or a map.

Params:

  • pass: analysis context with TypesInfo
  • expr: expression to check

Returns:

  • bool: true if it is a slice or a map

func IsSliceType

func IsSliceType(expr ast.Expr) bool

IsSliceType checks if a type is a slice.

Params:

  • expr: the expression representing the type

Returns:

  • bool: true if it's a slice

func IsSliceTypeWithPass

func IsSliceTypeWithPass(pass *analysis.Pass, expr ast.Expr) bool

IsSliceTypeWithPass checks if an expression is a slice type using TypesInfo.

Params:

  • pass: analysis context with TypesInfo
  • expr: expression to check

Returns:

  • bool: true if it is a slice type

func IsSmallConstantSize

func IsSmallConstantSize(pass *analysis.Pass, expr ast.Expr) bool

IsSmallConstantSize checks if an expression is a constant <= 1024. Used to avoid VAR-004/VAR-005 triggering when VAR-016 applies.

Params:

  • pass: analysis context
  • expr: size expression to check

Returns:

  • bool: true if it is a positive constant <= 1024

func IsStructType

func IsStructType(expr ast.Expr) bool

IsStructType checks if a type is a struct.

Params:

  • expr: the expression representing the type

Returns:

  • bool: true if it's a struct

func IsTestFile

func IsTestFile(filename string) bool

IsTestFile checks if a file is a test file (*_test.go).

Params:

  • filename: the file name to check

Returns:

  • bool: true if the file is a test file

func IsValidInitialism

func IsValidInitialism(name string) bool

IsValidInitialism checks if the name is composed only of valid initialisms.

Params:

  • name: the name to check

Returns:

  • bool: true if composed only of valid initialisms

func IsZeroLiteral

func IsZeroLiteral(expr ast.Expr) bool

IsZeroLiteral checks if an expression is the zero literal (0).

Params:

  • expr: the expression to check

Returns:

  • bool: true if it's the literal 0

func ShouldSkipFile

func ShouldSkipFile(pass *analysis.Pass, file *ast.File) bool

ShouldSkipFile checks if a file should be skipped for analysis. Skips test files and generated files.

Params:

  • pass: analysis pass
  • file: the AST file to check

Returns:

  • bool: true if the file should be skipped

func ShouldSkipGeneratedFile

func ShouldSkipGeneratedFile(file *ast.File) bool

ShouldSkipGeneratedFile checks if a generated file should be skipped. Only skips generated files, not test files.

Params:

  • file: the AST file to check

Returns:

  • bool: true if the file is generated

func ShouldSkipTestFile

func ShouldSkipTestFile(pass *analysis.Pass, file *ast.File) bool

ShouldSkipTestFile checks if a test file should be skipped. Only skips test files, not generated files.

Params:

  • pass: analysis pass
  • file: the AST file to check

Returns:

  • bool: true if the file is a test file

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL