diff --git a/protectedby/analyzer.go b/protectedby/analyzer.go index 892bf63..b8aea31 100644 --- a/protectedby/analyzer.go +++ b/protectedby/analyzer.go @@ -430,18 +430,15 @@ func getLock(pass *analysis.Pass, spec *ast.TypeSpec, c *ast.Comment) (*ast.Fiel } func getStructFieldByName(name string, st *ast.StructType) *ast.Field { - var lockField *ast.Field - for _, field := range st.Fields.List { for _, n := range field.Names { if name == n.Name { - lockField = field - break + return field } } } - return lockField + return nil } // getLockName returns the first word in the comment after "protected by" statement or error if the statement is not diff --git a/protectedby/analyzer_test.go b/protectedby/analyzer_test.go index e8a2dad..db20531 100644 --- a/protectedby/analyzer_test.go +++ b/protectedby/analyzer_test.go @@ -83,7 +83,7 @@ func Test_getLockName(t *testing.T) { } if name != tc.expectedLockName { - t.Fatalf("expected lock name %q, got %q", tc.expectedError, err) + t.Fatalf("expected lock name %q, got %q", tc.expectedLockName, name) } }) }