lintr icon indicating copy to clipboard operation
lintr copied to clipboard

sprintf_linter() for 1-argument calls?

Open MichaelChirico opened this issue 4 weeks ago • 3 comments

Is there any reason not to lint this?

# known string
sprintf(paste0(x, y))
# assumed string
sprintf(foo())

We already lint the literal version:

lint(text = "sprintf('a')", linters = sprintf_linter())
# <text>:1:1: warning: [sprintf_linter] sprintf call can be removed when a constant string is provided.
# sprintf('a')
# ^~~~~~~~~~~~

MichaelChirico avatar Nov 28 '25 06:11 MichaelChirico

Not that I can think off. The first I've definitely done by accident whilst refactoring so would be nice to catch.

TimTaylor avatar Nov 28 '25 09:11 TimTaylor

Is there a way to inject NSE there? Something like:

df |> mutate(a = sprintf(!!!args))

Also, beware of bquote() for similar use cases.

AshesITR avatar Nov 29 '25 09:11 AshesITR

Is there a way to inject NSE there?

I suppose that could work, though I don't see anyone doing it:

https://github.com/search?q=lang%3AR+%2Fsprintf%5B%28%5D%5B+%5Cn%5D*%5B%21%5D%5B%21%5D%5B%21%5D%2F&type=code

MichaelChirico avatar Dec 01 '25 22:12 MichaelChirico