-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: cmd/vet: unusedresult
should accept a list of functions whose results _may be unused_ (inverse sense to -funcs flag)
#65984
Comments
unused
should have an option to work with a "black list" of functions to check that results are usedunused
should have an option to work with a "blacklist" of functions to check that results are used
unused
should have an option to work with a "blacklist" of functions to check that results are usedunusedresult
should have an option to work with a "blacklist" of functions to check that results are used
IIUC this proposal is to add a flag that instructs the checker to treat all functions as It would be better to have some data to work with than to speculate though. Have you collected any data about the user experience of such an option? What does this list look like on large projects? This may be a good candidate for trying such a checker out in the wild without/before being integrated in vet itself. |
Exactly. Btw what is the Now, I would argue that if a function return a result, in most cases it is meant to be used -- otherwise why return a value. The Cost benefit analysis of the proposed features (not counting the cost of implementing it ... hopefully it's simple?):
Data analysis: a bias dataset, but easy for me to generate and hopefully it's a data-point useful enough for this proposal. I took one of my larger current project, grepped for all standard library references ( The results for my project follow below. Basically, outside of Labels:
ps.: Script to generate the data, just so one can reproduce on other projects / or verify for correctness: go install github.com/itchyny/gojq/cmd/gojq@latest
go install github.com/ezekg/xo@latest
function enumerateImports() { go list -json ./... | gojq ".Imports" | egrep '^ ' | sed 's/^\s*//; s/,$//; s/"//g;' ; }
function stdlibImports() { egrep -v '(\.org|\.com|\.io)' | sort | uniq ; }
function createExtractionRule() { pkgs="$(xo '/(\w+)\n/$1/i' | sort | uniq | paste -sd '|')" ; printf "/((%s)\.\w+)/\$1/" "$pkgs" ; }
extractRule="$(enumerateImports | stdlibImports | createExtractionRule)" ; echo $extractRule
function enumerateAndCountStdlib() { find . -name '*.go' | while read fileName ; do cat $fileName | xo "$extractRule" ; done | sort | uniq -c | xo '/(\d+)\s([\w.]+)\n/| $2 | $1 | |/'; }
enumerateAndCountStdlib |
Thank you for taking the time to collect this data. This is exactly the kind of information we need to consider to make such a decision about vet. I think what you have given is mostly a claim that the set of functions in the standard library that should be warned on by default for unusedresult is much larger than the current set. I am not sure you will achieve agreement agreement on the Labels you have assigned, for example atomic.AddInt64. But I think there is a case to be made that vet should warn on many of these like The data that is really needed for this proposal is what such a flag would actually end up reporting. This helps us know is both the concrete benefit of adding such a flag (whats reported), and where the potential objections might come from (what needed to be suppressed). This also involves going outside the standard library and including user code. Here is a diff that will be good enough for experimentation:
I attempted to apply this using:
This had problems due to missing headers. I believe this is as expected given the Installation instructions for the project. For the packages that do succeed, these are mostly
Like
It was a typo of |
Thanks for the follow up @timothy-king ! I interpret the data I collected in a slightly different fashion: in a (typical??) project, most of the standard library" functions used would benefit from an "unused return values" checks, weighted by number of uses. I'm hoping this factors in the calculation of the cost/benefit, and how often such flag would be beneficial.
I fear this data wouldn't be telling in committed projects: I already fixed my problem, it took me time. It's possible that most projects eventually fixed their unused returns caused bugs, it just took developers time for them fix it. A better metric would be how much time such a lint option would save developers, by finding bugs quicker. Alas ... I don't have a crystal ball, nor can I think of a proxy metric that would approximate it. But maybe we can borrow the wisdom of other programming languages (in g++ I'm proposing it to be disabled by default. I wonder if it's possible to instrument it to collect usage statistics (??), such that one can observe over time how often it becomes used/enabled by developers ? That could actually be a good proxy metric to saving developers time... If it turns out to be popular, and after a few iterations to minimize false positives, one can decide to make it default one day. Also the statistics collected with this feature could help better inform #20803, without committing to a language change.
Thank you!!! I'll give it a try and report back tomorrow (it's Sundar's day ...), I have the headers here. I can also run on another larger project here. But again, my point is that likely I won't find anything ... what I'm hoping is that this warning would help me fix future bugs quicker. If it's so simple I feel hopeful :) ps.: Indeed |
I am not sure rule 3 in #20148 applies equally well to types that are not errors. A string/int/bool result being ignored in the next statement that reads a string/int/bool variable does not quite seem like a bug without more context. FWIW the |
Agreed in part. Even with respect to errors there are some cases one may want to ignore -- e.g.: closing a file opened for reading. I agree that an My point is only about giving the option for the user to decide what they want to be warned about, and what not. This power is already available as a "whitelist" flag. And this proposal is about improving on that control by providing an alternative "blacklist" flag instead. My use case (and assume others) is that the "whitelist" control doesn't help me at all with the cases that matter, but a "blacklist" will cost some (I would have to list some functions that I don't care about the results), but will be very valuable for catching those functions I hadn't thought about. Btw, with regard to #20148 my point is that the "blacklist" control here could be used to catch those same cases of unhandled errors, along others. But it is not the same. |
unusedresult
should have an option to work with a "blacklist" of functions to check that results are usedunusedresult
should have an option to work with a "blacklist" of functions to check that results are used
unusedresult
should have an option to work with a "blacklist" of functions to check that results are usedunusedresult
should accept a list of functions whose results must be used
unusedresult
should accept a list of functions whose results must be usedunusedresult
should accept a list of functions whose results _may be unused_ (inverse sense to -funcs flag)
Proposal Details
Similar in spirit to proposal #20803 (for the go compiler), and related to #63689, #14972, and #20148.
I recently lost time on a subtle bug where I forgot to use the result of a function. I tried to simplify the issue to a minimal example:
Currently,
go vet
will only catch this if I list-unusedfuncs=math.Sqrt
. But I would only addmath.Sqrt
if I already knew about the bug, which defies thego vet
purpose. I would like to pass all my projects functions/methods and its dependencies (including standard library) to-unusedfuncs
.Instead, can we have an alternative flag
-unusedignorefuncs
(or something else) where one can pass a "blacklist", and every other function whose results are unused are reported back ? (maybe with some defaults, likefmt.Printf
) ?I understand a similar point is being made for the compiler (and extra syntax?) in proposal #20803.
Having a "blacklist" option to
go vet
could also be an intermediary solution for that proposal -- for those usinggo vet
systematically.The text was updated successfully, but these errors were encountered: