lintr
lintr copied to clipboard
find variables which reuse names from functions or variables in base or recommended packages
Although the code in R itself doesn't follow its own recommendations, it would be nice to lint for times when a variable is defined which overrides the name of something in a base or recommended package. I imagine it would be easy to string match against the contents of those namespaces.
E.g., I would love a lints for:
version <- "version is a 'simple.list' in the base package"
names <- c("alice", "bob") # oh no, names is a function in base
This can lead to problems, e.g. when editing the variable declaration, but not updating subsequent uses, since the symbol will still be found, but will often be for a closure, giving opaque errors.
This would apply to both variables defined within functions, and function arguments, so the following would also be good to lint:
my_fun <- function(data.frame = cars) head(data.frame)
This is a good suggestion, thanks
I wrote something similar to this, the issue is there are several thousand names to check which would make the linter relatively heavy
Maybe reusing logic from the object_usage_linter and checking for collisions of all of the found symbols within the base & recommended packages would be a lightweight way to implement this.
That way we wouldn't need a list of all bad object names, but instead dynamically check the (presumably fewer) used symbols against a bunch of loaded namespaces.
See #2307. It's not as slow as you might expect (though noticeably slower than the fastest+simplest linters)
Marking this as closed by #2307. please file any residual FR as a follow-up