Almost surely this should be written with on.exit(close(c2), add = TRUE). Better yet in this case is to use {withr}, but this applies to on.exit() generally.
If the author does intend to overwrite on.exit(), the linter encourages doing so explicitly: on.exit(close(c2), add = FALSE).
The difficulty here will be identifying "multiple times". It may not be possible in general, e.g.
If the on.exit() calls belong to the same expr, there should be a code path that hits both, right?
In that case, a following-sibling::SYMBOL_FUNCTION_CALL based XPath should work well.
I think it makes sense to require
on.exit()
to be called with explicitadd=
when called multiple times in a function body:Almost surely this should be written with
on.exit(close(c2), add = TRUE)
. Better yet in this case is to use {withr}, but this applies toon.exit()
generally.If the author does intend to overwrite
on.exit()
, the linter encourages doing so explicitly:on.exit(close(c2), add = FALSE)
.The difficulty here will be identifying "multiple times". It may not be possible in general, e.g.
This problem only gets worse as further nesting is allowed. Here's an even trickier example:
That said, I think we can at least come up with some simple rules to start with a high-precision, low-recall linter that's useful.