You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I see the following unnecessary sprintf() across the codebase I maintain:
sprintf("hello")
sprintf("%s", "hello")
In other words:
sprintf() does not have any arguments passed to ...
sprintf()fmt is "%s"and the next argument in a STR_CONST.
These two examples could be replaced by a more readable and more efficient "hello".
I believe the existing sprintf_linter() infrastructure and in particular the logic around piped calls could be re-used here. If you believe this would be a good improvement, I can give it a try.
only question is if it maybe belongs under literal_coercion_linter(). I also see paste("...") cases that should lint similarly, so maybe sprintf_linter() is too specific.
There's some subtlety here, it's not quite trivial to implement:
sprintf("%s") # already lints b/c '%s' is not matched
sprintf("abc") # needs to lint because there's no template at all
sprintf("def", "ghi") # already lints b/c 'ghi' doesn't correspond to anything
i.e. it's not as simple as "find sprintf() calls with just one argument", it's got to first subset among those calls that don't already generate a lint.
Not particularly difficult, but requires a bit more care.
Activity
MichaelChirico commentedon Sep 18, 2023
only question is if it maybe belongs under literal_coercion_linter(). I also see paste("...") cases that should lint similarly, so maybe sprintf_linter() is too specific.
AshesITR commentedon Sep 18, 2023
I think
sprintf_linter()is a good place (andpaste_linter()for the other case). There is no coercion going on insprintf("%s", "a").MichaelChirico commentedon Sep 18, 2023
Large number of hits on GitHub:
https://github.com/search?q=%2F%5B%5Ea-z0-9._%5Dsprintf%5B%28%5D%5B%22%27%5D%5B%5E%27%22%5D*%5B%27%22%5D%5B%29%5D%2F+lang%3AR&type=code
Missing arguments passed to sprintf()
sprintf_linter() fixes (#13654)
MichaelChirico commentedon Feb 18, 2025
There's some subtlety here, it's not quite trivial to implement:
i.e. it's not as simple as "find
sprintf()calls with just one argument", it's got to first subset among those calls that don't already generate a lint.Not particularly difficult, but requires a bit more care.
AshesITR commentedon Feb 22, 2025
Another edge case to be aware of is
sprintf("%%")andsprintf("%%", ""). The former could lint (replace by"%"). The latter already lints.contains(text(), '%')is too simple.