lintr icon indicating copy to clipboard operation
lintr copied to clipboard

find variables which reuse names from functions or variables in base or recommended packages

Open jackwasey opened this issue 6 years ago • 3 comments

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)

jackwasey avatar Mar 02 '19 14:03 jackwasey

This is a good suggestion, thanks

jimhester avatar Mar 10 '19 18:03 jimhester

I wrote something similar to this, the issue is there are several thousand names to check which would make the linter relatively heavy

MichaelChirico avatar Jan 31 '21 08:01 MichaelChirico

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.

AshesITR avatar Jan 31 '21 11:01 AshesITR

See #2307. It's not as slow as you might expect (though noticeably slower than the fastest+simplest linters)

MichaelChirico avatar Nov 18 '23 05:11 MichaelChirico

Marking this as closed by #2307. please file any residual FR as a follow-up

MichaelChirico avatar Nov 19 '23 20:11 MichaelChirico