Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line of sight loops trick prealloc #16

Open
carnott-snap opened this issue Aug 7, 2019 · 1 comment
Open

line of sight loops trick prealloc #16

carnott-snap opened this issue Aug 7, 2019 · 1 comment

Comments

@carnott-snap
Copy link

When ranging over a list that contains conditional break/continue directives and a slice = append(slice, element) at the end, prealloc suggests preallocation.

No lint triggered

var a []int
for i := range []struct{}{{}, {}, {}} {
        if i < 1 {
                a = append(a, i)
        } 
}

Lint triggered

var a []int
for i := range []struct{}{{}, {}, {}} {
        if i < 1 {
              continue
        } 
        a = append(a, i)
}
@luisdavim
Copy link

I also get a false positive with code that looks like this:

// ...
var (
         a []string
         u map[string]struct{}
)
// some code to populate u
// where a is used to return in case of errors
a = make([]string,0, len(u))
for s := range u {
        a = append(s)
}
// ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants