-
Notifications
You must be signed in to change notification settings - Fork 18k
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: fmt: add a function to detect formatting directives in string #64657
Comments
IIUC all that needs to happen is to for IIalsoUC this is not strictly a correctness criteria for @jba Any thoughts on classifying formatting directives as a bug in @dominikh staticcheck candidate?
Could you maybe flesh this out a bit more? Right now it seems like this is to code analysis tools. And there is one additional analysis that has been identified as maybe benefiting, |
Would this case be so different from the "possible formatting directive" that gets flagged for the fmt.Println case? Both can have false positives in theory and don't seem to in practice. |
The slog pass is run during |
I think the predicate you are looking for is simply
All of these rules compose under concatenation. |
@adonovan nice to hear from you and thanks for the response :) That's definitely a clever way (and missed on my part) to be able to tell if the string has a formatting directive. Question: should the printf analysis implementation be updated to do this as an example? https://github.com/golang/tools/blob/06407013ff725a89b552c86d78d310b93f80aa57/go/analysis/passes/printf/printf.go#L1052 Thanks! |
I think the answer has to be no, because the comment next to the regexp says that it intentionally excludes the "% " case so that (for example) "x % y" doesn't trigger a diagnostic. That function is using a heuristic to answer the question "does this look like it was intended for printf?", not "might this be a valid format string?" See https://go.dev/play/p/dpWRELHLES0 for an example that defeats the heuristic.
Sorry for the delay. We should definitely grab that (much belated) beer we discussed at GothamGo. |
Since we have an answer - |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
There are some tools out there that need/should check if a string has fmt.Printf formatting directives.
Therefore, it would be great if we had a function like the following
Motivation:
The new log/slog function signature is exactly the same as fmt's Printf function signature:
func (string, ...any)
but the expected arguments are not the same.Therefore, it's easy to make a mistake for users migrating from
log.Printf
toslog
by accidentally passing formatting directives.logger.Info("username %s has %d errors", "John", 5)
Where the output ends up being
{"message": "username %s has %d erros", "John": 5}
instead of{"message": "username John has 5 errors"}
There's already a slog analysis package but it does not catch the above case.
While there's a printf analysis package that catches exactly this case when you pass formatting directives to non-formatting functions like
fmt.Println("Hello %s", "John") // fmt.Println has possible Printf formatting directive
. The analysis code for catching this error is hereInstead of copying the internal logic over, it might be nice to just create a std lib function (or under x/tools) that catches this case as I imagine there are other use cases out there where this function is needed.
Otherwise, this proposal can just be to update the slog Analysis package to catch the above case :)
The text was updated successfully, but these errors were encountered: