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

sprintf linter #544

Closed
renkun-ken opened this issue Oct 27, 2020 · 3 comments · Fixed by #578
Closed

sprintf linter #544

renkun-ken opened this issue Oct 27, 2020 · 3 comments · Fixed by #578

Comments

@renkun-ken
Copy link
Collaborator

I'm thinking of a sprintf linter that extracts all sprintf(...) calls and checks if the number of supplied arguments is consistent with the number of format placeholders in the format string when the format string is a string literal (rather than a symbol or call).

If the supplied argument is an atomic value, it could also check if its type is consistent with the format at the corresponding position.

@AshesITR
Copy link
Collaborator

AshesITR commented Nov 23, 2020

Just a memo if this will be implemented, sprintf() supports the %1$d notation for position selection (and repeated replacements).

> sprintf("%1$s %s", "a", 42L)
[1] "a a"

@renkun-ken
Copy link
Collaborator Author

Thanks for pointing it out!

I think we could transform calls like sprintf(<string>, <call>, <symbol>, 20L, "a") into sprintf(<string>, 0, 0, 20L, "a"), i.e. transform arguments that are not atomic scalar value into something like 0 that is compatible with all formats (?) and evaluate it and capture the error if any. Looks like it should work to identify an incorrect number of arguments and incompatible types that directly appear in the arguments?

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Nov 23, 2020

Just confirming in fact 0 works (whereas TRUE) doesn't:

# every format mentioned in ?sprintf
fmts = c(
  "%d", "%i", "%o", "%x", "%X", "%f", "%e", "%E", "%g", "%G", "%a", "%A", "%s",
  "%02d", "%2d", "%1.2x", "1.2f", "1.2g", "%05s"
)
for (fmt in fmts) sprintf(fmt, 0)
# (no error)

@renkun-ken renkun-ken mentioned this issue Nov 23, 2020
2 tasks
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

Successfully merging a pull request may close this issue.

3 participants