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

False positives when appends are conditional #30

Open
mholt opened this issue Aug 29, 2024 · 2 comments
Open

False positives when appends are conditional #30

mholt opened this issue Aug 29, 2024 · 2 comments

Comments

@mholt
Copy link

mholt commented Aug 29, 2024

Even with simple=true, the linter reports about preallocating slices. Here is one such example:

var ents []*Entity // <--- ***false positive here***

if !m.fromMe() {
	e := entityWithID(m.normalizedCallerID)
	ents = append(ents, &e) // <-- conditional append only
}

for _, p := range m.participants {
	pID := p.normalizedID()
	if pID == senderID {
		continue
	}
	e := p.entity()
	ents = append(ents, &e) // <-- also conditional due to continue
}

As you can see, there is no way to know how many elements will be added to the slice - could be 0 or more, with no way to tell beforehand.

Loops that use continue, or appends which are conditional, should be omitted from this linter.

@kevgo
Copy link

kevgo commented Jan 9, 2025

Suggestion for a fix: Simply ignore loops that contain a continue statement. Avoiding false positives feels more important than having a few false negatives. ChatGPT can prototype the code to identify continue statements in the loop body's AST if needed.

@alexkohler
Copy link
Owner

Hi @kevgo, I unfortunately don't have the bandwidth to work on this issue, but that seems like a reasonable heuristic to me.

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

No branches or pull requests

3 participants