target

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CandidateManager

type CandidateManager struct {
	// contains filtered or unexported fields
}

CandidateManager manages the set of declaration move candidates.

func (CandidateManager) BlockMovesLosingTypeInfo

func (cm CandidateManager) BlockMovesLosingTypeInfo(allUsages iter.Seq2[*types.Var, []usage.NodeUsage]) map[astutil.NodeIndex][]*types.Var

BlockMovesLosingTypeInfo prevents moves that would lose necessary type information.

Scenario: A variable is declared with an explicit or inferred type, then later reassigned with a different type inference. If we move the first declaration, subsequent uses would have a different type.

Example:

var x any           // First declaration (unused)
x, y := "hello", 0  // Reassignment with different type

Moving the first declaration would change x's type from any to string.

func (CandidateManager) BlockMovesWithTypeChanges

func (cm CandidateManager) BlockMovesWithTypeChanges(allUsages iter.Seq2[*types.Var, []usage.NodeUsage], conservative bool)

BlockMovesWithTypeChanges marks candidates as blocked when moving would change the inferred type of a variable that is actually used.

Type changes are blocked in two cases:

  • Conservative mode: Any type change for a used variable
  • Type change to untyped nil (would cause compile errors)

func (CandidateManager) BlockSideEffects

func (cm CandidateManager) BlockSideEffects(info *types.Info, body inspector.Cursor)

BlockSideEffects marks candidates as blocked if there are intervening statements with possible side effects.

func (CandidateManager) OrphanedDeclarations

func (cm CandidateManager) OrphanedDeclarations(allUsages iter.Seq2[*types.Var, []usage.NodeUsage]) map[astutil.NodeIndex][]*types.Var

OrphanedDeclarations identifies declarations that would become entirely unused after other declarations are moved. These can have all their variables replaced with '_'.

This handles the case where a variable is reassigned multiple times, and moving the first declaration leaves subsequent assignments with no remaining reads.

func (CandidateManager) ResolveInitFieldConflicts

func (cm CandidateManager) ResolveInitFieldConflicts(in *inspector.Inspector, combine bool)

ResolveInitFieldConflicts handles multiple declarations targeting the same init field.

If conservative mode is on, all conflicts are blocked. If not conservative, it attempts to combine compatible simple assignments (x:=1, y:=2 -> x,y:=1,2).

func (CandidateManager) SortedMoveTargets

func (cm CandidateManager) SortedMoveTargets(unused, orphanedDeclarations map[astutil.NodeIndex][]*types.Var) []MoveTarget

SortedMoveTargets converts the intermediate candidate map to a sorted slice of MoveTarget.

Combines:

  • Regular move candidates (with or without unused variables)
  • Orphaned declarations (no target node, all variables unused)

Returns results sorted by source position for deterministic output.

type MovableDecl

type MovableDecl struct {
	Decl   astutil.NodeIndex // Inspector index of the declaration statement to move
	Unused []string          // Unused identifiers in this declaration
}

MovableDecl represents a declaration that can be moved to another scope in the code analysis process.

type MoveCandidate

type MoveCandidate struct {
	// contains filtered or unexported fields
}

MoveCandidate is an intermediate representation of a potential move operation.

Differences from MoveTarget:

  • Does not include the declaration index (stored as a map key)
  • Mutable status field (updated during conflict resolution)

type MoveStatus

type MoveStatus interface {
	Movable() bool
	String() string
}

MoveStatus indicates if a move is safe or why it isn't. Implementations report specific reasons that prevent moving a declaration (e.g., variable shadowing, scope conflicts).

type MoveTarget

type MoveTarget struct {
	MovableDecl                 // The declaration to move
	TargetNode    ast.Node      // The node with the target scope (e.g., *[ast.IfStmt], *[ast.BlockStmt])
	AbsorbedDecls []MovableDecl // Additional declarations merged into this one
	Status        MoveStatus    // Status indicating if the move is safe or why it isn't
}

MoveTarget represents a declaration that can be moved to a tighter scope.

type Stage

type Stage struct {
	// The current [*analysis.Pass]
	*analysis.Pass

	// TargetScope provides context for scope adjustments and safety checks.
	scope.TargetScope

	// MaxLines specifies the maximum number of lines a declaration can span to be considered for moving
	// into control flow initializers.
	MaxLines int

	// Conservative specifies to only permit moves that don't cross code with potential side effects.
	Conservative bool

	// Combine determines whether to attempt combining initialization statements during scope tightening.
	Combine bool
}

Stage contains configurable options for analyzing variable scope tightening.

func (Stage) CollectMoveCandidates

func (ts Stage) CollectMoveCandidates(body inspector.Cursor, cf astutil.CurrentFile, scopeRanges iter.Seq2[astutil.NodeIndex, usage.ScopeRange]) CandidateManager

CollectMoveCandidates iterates through all usage scopes and determines valid target nodes for declarations that can be moved to tighter scopes.

func (Stage) SelectTargets

func (ts Stage) SelectTargets(ctx context.Context, cf astutil.CurrentFile, body inspector.Cursor, usageData usage.Result) []MoveTarget

SelectTargets determines which declarations can be moved to tighter scopes and where they should go.

Returns a sorted list of move targets.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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