Skip to content

Commit

Permalink
all: run gopls's modernize -fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed Feb 22, 2025
1 parent 632b908 commit e8f1ddb
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 42 deletions.
4 changes: 2 additions & 2 deletions bundled_typeparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func typeparams_computeTermSetInternal(t types.Type, seen map[types.Type]*typepa
// The term set of an interface is the intersection of the term sets of its
// embedded types.
tset.terms = typeparams_allTermlist
for i := 0; i < u.NumEmbeddeds(); i++ {
for i := range u.NumEmbeddeds() {
embedded := u.EmbeddedType(i)
if _, ok := embedded.Underlying().(*types.TypeParam); ok {
return nil, fmt.Errorf("invalid embedded type %T", embedded)
Expand All @@ -99,7 +99,7 @@ func typeparams_computeTermSetInternal(t types.Type, seen map[types.Type]*typepa
case *types.Union:
// The term set of a union is the union of term sets of its terms.
tset.terms = nil
for i := 0; i < u.Len(); i++ {
for i := range u.Len() {
t := u.Term(i)
var terms typeparams_termlist
switch t.Type().Underlying().(type) {
Expand Down
4 changes: 2 additions & 2 deletions bundled_typeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type typeutil_hasher struct{ inGenericSig bool }
// hashString computes the Fowler–Noll–Vo hash of s.
func typeutil_hashString(s string) uint32 {
var h uint32
for i := 0; i < len(s); i++ {
for i := range len(s) {
h ^= uint32(s[i])
h *= 16777619
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (h typeutil_hasher) hash(t types.Type) uint32 {
case *types.Named:
hash := h.hashTypeName(t.Obj())
targs := t.TypeArgs()
for i := 0; i < targs.Len(); i++ {
for i := range targs.Len() {
targ := targs.At(i)
hash += 2 * h.hash(targ)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ctrlflow/ctrlflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func Obfuscate(fset *token.FileSet, ssaPkg *ssa.Package, files []*ast.File, obfR
if trashBlockCount > 0 {
addTrashBlockMarkers(ssaFunc, trashBlockCount, obfRand)
}
for i := 0; i < split; i++ {
for range split {
if !applySplitting(ssaFunc, obfRand) {
break // no more candidates for splitting
}
Expand All @@ -209,7 +209,7 @@ func Obfuscate(fset *token.FileSet, ssaPkg *ssa.Package, files []*ast.File, obfR
addJunkBlocks(ssaFunc, junkCount, obfRand)
}
var dispatchers []dispatcherInfo
for i := 0; i < passes; i++ {
for range passes {
if info := applyFlattening(ssaFunc, obfRand); info != nil {
dispatchers = append(dispatchers, info)
}
Expand Down
7 changes: 2 additions & 5 deletions internal/ctrlflow/hardening.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,10 @@ type delegateTableHardening struct{}

func (delegateTableHardening) Apply(dispatcher []cfgInfo, ssaRemap map[ssa.Value]ast.Expr, rnd *mathrand.Rand) (ast.Decl, ast.Stmt) {
keySize := literals.MinSize + mathrand.Intn(literals.MinSize)
delegateCount := keySize

// Reusing multiple times one decryption function is fine,
// but it doesn't make sense to generate more functions than keys.
if delegateCount > len(dispatcher) {
delegateCount = len(dispatcher)
}
delegateCount := min(keySize, len(dispatcher))

delegateKeyIdxs := rnd.Perm(keySize)[:delegateCount]
delegateLocalKeys := generateKeys(delegateCount, nil, rnd)
Expand Down Expand Up @@ -184,7 +181,7 @@ func (delegateTableHardening) Apply(dispatcher []cfgInfo, ssaRemap map[ssa.Value
}

delegatesAst := make([]ast.Expr, delegateCount)
for i := 0; i < delegateCount; i++ {
for i := range delegateCount {
// Code for single decryption delegate:
/*
func(i int) int {
Expand Down
4 changes: 2 additions & 2 deletions internal/ctrlflow/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func addJunkBlocks(ssaFunc *ssa.Function, count int, obfRand *mathrand.Rand) {
return
}

for i := 0; i < count; i++ {
for i := range count {
targetBlock := candidates[obfRand.Intn(len(candidates))]
succsIdx := obfRand.Intn(len(targetBlock.Succs))
succs := targetBlock.Succs[succsIdx]
Expand Down Expand Up @@ -254,7 +254,7 @@ func addTrashBlockMarkers(ssaFunc *ssa.Function, count int, obfRand *mathrand.Ra
return
}

for i := 0; i < count; i++ {
for range count {
targetBlock := candidates[obfRand.Intn(len(candidates))]
succsIdx := obfRand.Intn(len(targetBlock.Succs))
succs := targetBlock.Succs[succsIdx]
Expand Down
17 changes: 7 additions & 10 deletions internal/ctrlflow/trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func isSupportedSig(m *types.Func) bool {
if isGenericType(sig) {
return false
}
for i := 0; i < sig.Params().Len(); i++ {
for i := range sig.Params().Len() {
if !isSupportedType(sig.Params().At(i).Type()) {
return false
}
Expand Down Expand Up @@ -363,7 +363,7 @@ func (t *trashGenerator) cacheMethods(vars map[string]*definedVar) {
var methods []*types.Func
switch typ := typ.(type) {
case methodSet:
for i := 0; i < typ.NumMethods(); i++ {
for i := range typ.NumMethods() {
if m := typ.Method(i); token.IsExported(m.Name()) && isSupportedSig(m) {
methods = append(methods, m)
if len(methods) > limitFunctionCount {
Expand Down Expand Up @@ -432,15 +432,15 @@ func (t *trashGenerator) generateCall(vars map[string]*definedVar) ast.Stmt {

targetSig := targetFunc.Signature()
params := targetSig.Params()
for i := 0; i < params.Len(); i++ {
for i := range params.Len() {
param := params.At(i)
if !targetSig.Variadic() || i != params.Len()-1 {
args = append(args, t.generateRandomValue(param.Type(), vars))
continue
}

variadicCount := t.rand.Intn(maxVariadicParams)
for i := 0; i < variadicCount; i++ {
for range variadicCount {
sliceTyp, ok := param.Type().(*types.Slice)
if !ok {
panic(fmt.Errorf("unsupported variadic type: %v", param.Type()))
Expand Down Expand Up @@ -472,7 +472,7 @@ func (t *trashGenerator) generateCall(vars map[string]*definedVar) ast.Stmt {
Rhs: []ast.Expr{callExpr},
}

for i := 0; i < results.Len(); i++ {
for i := range results.Len() {
ident := ast.NewIdent(getRandomName(t.rand))
vars[ident.Name] = &definedVar{
Type: results.At(i).Type(),
Expand Down Expand Up @@ -501,10 +501,7 @@ func (t *trashGenerator) generateAssign(vars map[string]*definedVar) ast.Stmt {
varNames[i], varNames[j] = varNames[j], varNames[i]
})

varCount := 1 + t.rand.Intn(maxAssignVars)
if varCount > len(varNames) {
varCount = len(varNames)
}
varCount := min(1+t.rand.Intn(maxAssignVars), len(varNames))

assignStmt := &ast.AssignStmt{
Tok: token.ASSIGN,
Expand Down Expand Up @@ -537,7 +534,7 @@ func (t *trashGenerator) Generate(statementCount int, externalVars map[string]ty
}

var stmts []ast.Stmt
for i := 0; i < statementCount; i++ {
for range statementCount {
var stmt ast.Stmt
if len(vars) >= minVarsForAssign && t.rand.Float32() < assignVarProb {
stmt = t.generateAssign(vars)
Expand Down
2 changes: 1 addition & 1 deletion internal/literals/obfuscators.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (

func genRandIntSlice(obfRand *mathrand.Rand, max, count int) []int {
indexes := make([]int, count)
for i := 0; i < count; i++ {
for i := range count {
indexes[i] = obfRand.Intn(max)
}
return indexes
Expand Down
5 changes: 1 addition & 4 deletions internal/literals/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ func splitIntoRandomChunks(obfRand *mathrand.Rand, data []byte) [][]byte {

var chunks [][]byte
for len(data) > 0 {
chunkSize := 1 + obfRand.Intn(maxChunkSize)
if chunkSize > len(data) {
chunkSize = len(data)
}
chunkSize := min(1+obfRand.Intn(maxChunkSize), len(data))

chunks = append(chunks, data[:chunkSize])
data = data[chunkSize:]
Expand Down
7 changes: 3 additions & 4 deletions internal/ssa2ast/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/ast"
"go/token"
"go/types"
"maps"
"slices"
"sort"
"strconv"
Expand Down Expand Up @@ -459,7 +460,7 @@ func (fc *funcConverter) convertBlock(astFunc *AstFunc, ssaBlock *ssa.BasicBlock
localTuple := true
tmpVars := make(map[string]types.Type)

for i := 0; i < tuple.Len(); i++ {
for i := range tuple.Len() {
name, typ, hasRefs := fc.tupleVarNameAndType(r, i)
tmpVars[name] = typ
if hasRefs {
Expand All @@ -469,9 +470,7 @@ func (fc *funcConverter) convertBlock(astFunc *AstFunc, ssaBlock *ssa.BasicBlock
}

if !localTuple {
for n, t := range tmpVars {
astFunc.Vars[n] = t
}
maps.Copy(astFunc.Vars, tmpVars)
}

return assignStmt
Expand Down
18 changes: 9 additions & 9 deletions internal/ssa2ast/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
case *types.Interface:
methods := &ast.FieldList{}
hasComparable := false
for i := 0; i < typ.NumEmbeddeds(); i++ {
for i := range typ.NumEmbeddeds() {
embeddedType := typ.EmbeddedType(i)
if namedType, ok := embeddedType.(*types.Named); ok && namedType.String() == "comparable" {
hasComparable = true
Expand All @@ -74,7 +74,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
if !hasComparable && typ.IsComparable() {
methods.List = append(methods.List, &ast.Field{Type: ast.NewIdent("comparable")})
}
for i := 0; i < typ.NumExplicitMethods(); i++ {
for i := range typ.NumExplicitMethods() {
method := typ.ExplicitMethod(i)
methodSig, err := tc.Convert(method.Type())
if err != nil {
Expand Down Expand Up @@ -112,7 +112,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
// reference to unexported named emulated through new interface with explicit declarated methods
if !token.IsExported(obj.Name()) {
var methods []*types.Func
for i := 0; i < typ.NumMethods(); i++ {
for i := range typ.NumMethods() {
method := typ.Method(i)
if token.IsExported(method.Name()) {
methods = append(methods, method)
Expand All @@ -139,7 +139,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
return &ast.IndexExpr{X: namedExpr, Index: typeParamExpr}, nil
}
genericExpr := &ast.IndexListExpr{X: namedExpr}
for i := 0; i < typeParams.Len(); i++ {
for i := range typeParams.Len() {
typeArgs := typeParams.At(i)
typeParamExpr, err := tc.Convert(typeArgs)
if err != nil {
Expand All @@ -157,7 +157,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
case *types.Signature:
funcSigExpr := &ast.FuncType{Params: &ast.FieldList{}}
if sigParams := typ.Params(); sigParams != nil {
for i := 0; i < sigParams.Len(); i++ {
for i := range sigParams.Len() {
param := sigParams.At(i)

var paramType ast.Expr
Expand Down Expand Up @@ -185,7 +185,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
}
if sigResults := typ.Results(); sigResults != nil {
funcSigExpr.Results = &ast.FieldList{}
for i := 0; i < sigResults.Len(); i++ {
for i := range sigResults.Len() {
result := sigResults.At(i)
resultExpr, err := tc.Convert(result.Type())
if err != nil {
Expand All @@ -201,7 +201,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
}
if typeParams := typ.TypeParams(); typeParams != nil {
funcSigExpr.TypeParams = &ast.FieldList{}
for i := 0; i < typeParams.Len(); i++ {
for i := range typeParams.Len() {
typeParam := typeParams.At(i)
resultExpr, err := tc.Convert(typeParam.Constraint().Underlying())
if err != nil {
Expand All @@ -220,7 +220,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
return &ast.ArrayType{Elt: eltExpr}, nil
case *types.Struct:
fieldList := &ast.FieldList{}
for i := 0; i < typ.NumFields(); i++ {
for i := range typ.NumFields() {
f := typ.Field(i)
fieldExpr, err := tc.Convert(f.Type())
if err != nil {
Expand All @@ -240,7 +240,7 @@ func (tc *TypeConverter) Convert(typ types.Type) (ast.Expr, error) {
return ast.NewIdent(typ.Obj().Name()), nil
case *types.Union:
var unionExpr ast.Expr
for i := 0; i < typ.Len(); i++ {
for i := range typ.Len() {
term := typ.Term(i)
expr, err := tc.Convert(term.Type())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion reflect_abi_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func _makeGenericReplacer(oldnew []string) *_genericReplacer {
// Find each byte used, then assign them each an index.
for i := 0; i < len(oldnew); i += 2 {
key := oldnew[i]
for j := 0; j < len(key); j++ {
for j := range len(key) {
r.mapping[key[j]] = 1
}
}
Expand Down

0 comments on commit e8f1ddb

Please sign in to comment.