From 4aba2498464698a2af2aa93d0c02dcd4dc67ba88 Mon Sep 17 00:00:00 2001 From: Dinko Korunic Date: Mon, 22 Jul 2024 23:36:35 +0200 Subject: [PATCH] Ignore structs with anonymous fields (fixes #15) --- betteralign.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/betteralign.go b/betteralign.go index 1bd0585..265108d 100644 --- a/betteralign.go +++ b/betteralign.go @@ -109,10 +109,12 @@ func run(pass *analysis.Pass) (interface{}, error) { nodeFilter := []ast.Node{ (*ast.File)(nil), (*ast.StructType)(nil), + (*ast.GenDecl)(nil), } var aFile *ast.File var dFile *dst.File + var strName string applyFixesFset := make(map[string][]byte) testFset := make(map[string]bool) @@ -142,11 +144,26 @@ func run(pass *analysis.Pass) (interface{}, error) { var ok bool var s *ast.StructType + var g *ast.GenDecl + + if g, ok = node.(*ast.GenDecl); ok { + if g.Tok == token.TYPE { + decl := g.Specs[0].(*ast.TypeSpec) + strName = decl.Name.Name + } + + return + } if s, ok = node.(*ast.StructType); !ok { return } + // ignore structs with anonymous fields + if strName == "" { + return + } + if tv, ok := pass.TypesInfo.Types[s]; ok { betteralign(pass, s, tv.Type.(*types.Struct), dec, dFile, applyFixesFset, fn) }