Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
karimodm committed Oct 31, 2023
1 parent 98a36e7 commit efa5302
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tools/code-analysis/detect_modified_after_captured_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func main() {
for _, expr := range t.Lhs {
if ident, ok := expr.(*ast.Ident); ok {
if pos, captured := capturedVars[ident.Name]; captured {
pos := fset.Position(pos)
fmt.Printf("%s:%d: variable '%s' captured and modified outside closure\n", pos.Filename, pos.Line, ident.Name)
capturedPos := fset.Position(pos)
modifiedPos := fset.Position(ident.Pos())
fmt.Printf("%s:%d: variable '%s' captured by closure and modified at %s:%d\n",
capturedPos.Filename, capturedPos.Line, ident.Name, modifiedPos.Filename, modifiedPos.Line)
}
}
}
Expand Down Expand Up @@ -74,6 +76,10 @@ func main() {
return true
}

if ident.Name == "_" { // skip underscore variables
return true
}

if ident.Obj == nil || ident.Obj.Kind != ast.Var {
return true
}
Expand Down
14 changes: 14 additions & 0 deletions tools/code-analysis/examples/redeclared_in_different_scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

func redaclered() {
x := 42

// Outer closure
func() {
fmt.Println(x)
}()

x = 2
}

0 comments on commit efa5302

Please sign in to comment.