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

cmd/vet: know what builtin functions have no side effects #22851

Open
mvdan opened this issue Nov 22, 2017 · 3 comments
Open

cmd/vet: know what builtin functions have no side effects #22851

mvdan opened this issue Nov 22, 2017 · 3 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Nov 22, 2017

Reminder issue to continue the work in https://go-review.googlesource.com/c/go/+/79536.

For example, len and cap never have any side effects, and it would be useful to know that i == len(x) || i == len(x) is a suspicious expression.

More broadly, this could be extended to automatically detect what functions are free of side effects. But this would require having access to the full source with full type information, and may be complex and costly, so I'm not entirely sure it's a right fit for vet.

A simpler version of the above would be to also add standard library functions, such as strings.Contains or path.Join. I don't know if it is OK for vet to treat standard library packages differently, though.

@mvdan mvdan self-assigned this Nov 22, 2017
@gopherbot
Copy link

Change https://golang.org/cl/97775 mentions this issue: cmd/vet: teach vet that len and cap are pure funcs

@andybons andybons added the NeedsFix The path to resolution is known, but the work has not been done. label Mar 13, 2018
@andybons andybons added this to the Unplanned milestone Mar 13, 2018
@gopherbot
Copy link

Change https://golang.org/cl/112177 mentions this issue: cmd/vet: assume that no builtin funcs are pure

gopherbot pushed a commit that referenced this issue May 9, 2018
That was the intention with the existing code, but it was buggy; builtin
functions aren't treated as values by types.TypeAndVal. Thus, we should
use the IsBuiltin method instead of IsValue.

Teaching vet what builtin funcs are pure is already being tracked as a
separate issue, #22851.

While at it, also add a test with methods, just to be sure that the
current logic doesn't break with that edge case either.

Fixes #25303.

Change-Id: Ic18402b22cceeabf76641c02f575b194b9a536cc
Reviewed-on: https://go-review.googlesource.com/112177
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
@mvdan mvdan removed their assignment Nov 15, 2018
@mvdan
Copy link
Member Author

mvdan commented Nov 15, 2018

I've abandoned the CL I had sent for this, since it required more work and it had gotten considerable merge conflicts. Perhaps someone else will pick up this issue in the future.

@adonovan adonovan added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants