Skip to content

Commit f02120a

Browse files
authored
Prevent Variable Shadowing and using fixed length slice (#422)
Update code to save shadow variable name and also prevent using append when we already know the size of the slice.
1 parent ac0fa7e commit f02120a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

param.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ func newParamList(ctype reflect.Type, c containerStore) (paramList, error) {
123123

124124
pl := paramList{
125125
ctype: ctype,
126-
Params: make([]param, 0, numArgs),
126+
Params: make([]param, numArgs),
127127
}
128128

129129
for i := 0; i < numArgs; i++ {
130130
p, err := newParam(ctype.In(i), c)
131131
if err != nil {
132132
return pl, newErrInvalidInput(fmt.Sprintf("bad argument %d", i+1), err)
133133
}
134-
pl.Params = append(pl.Params, p)
134+
pl.Params[i] = p
135135
}
136136

137137
return pl, nil

provide.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,17 @@ func (s *Scope) provide(ctor interface{}, opts provideOptions) (err error) {
449449
// we start making changes to it as we may need to
450450
// undo them upon encountering errors.
451451
allScopes := s.appendSubscopes(nil)
452-
for _, s := range allScopes {
453-
s := s
454-
s.gh.Snapshot()
455-
defer func() {
456-
if err != nil {
457-
s.gh.Rollback()
452+
453+
defer func(allSc []*Scope) {
454+
if err != nil {
455+
for _, sc := range allSc {
456+
sc.gh.Rollback()
458457
}
459-
}()
458+
}
459+
}(allScopes)
460+
461+
for _, sc := range allScopes {
462+
sc.gh.Snapshot()
460463
}
461464

462465
n, err := newConstructorNode(

0 commit comments

Comments
 (0)