Skip to content

object_usage_linter ignores library functions #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
renkun-ken opened this issue Apr 13, 2020 · 5 comments
Closed

object_usage_linter ignores library functions #482

renkun-ken opened this issue Apr 13, 2020 · 5 comments

Comments

@renkun-ken
Copy link
Collaborator

renkun-ken commented Apr 13, 2020

From https://github.com/REditorSupport/vscode-r-lsp/issues/36.

Suppose the following code is stored in test.R:

library(jsonlite)

read <- function(file, ...) {
  read_json(file, ...)
}

Then object_usage_linter seems to ignore library(jsonlite) and mark read_json as unknown which is annoying.

> lintr::lint("test.R")
/Users/ken/Workspaces/test/vscode-r/test/test.R:4:3: warning: no visible global function definition for 'read_json'
  read_json(file, ...)
  ^~~~~~~~~
@tzakharko
Copy link

I was about to open an identical issue, so instead I am commenting here. In addition, maybe one should reconsider enabling object_usage_linter as default? If I understand correctly, it executes code during checks and is therefore a security hazard.

@assignUser
Copy link

assignUser commented Aug 14, 2020

This also happens if it is just in another .R file, it does not happen if the call is directly preceeded by an if without curly braces.
It also only happens within another function, on the top level of an .R file it does not trigger the linter:
image

@naupaka
Copy link

naupaka commented Oct 8, 2020

Adding my +1 to this. It looks like the environment in which the code from outside the function definition is evaluated is not preserved when evaluating code that makes up the function definition. If the file could be parsed and the package variables added to the environment used for testing here:
https://github.com/jimhester/lintr/blob/b93765553d3c1194e37be6efa047085de6d06d5a/R/object_usage_linter.R#L22

that might be one way to resolve this? I tried fiddling with it, but I'm not comfortable enough with the structure of the package to know precisely where the leverage point would be.

@AshesITR
Copy link
Collaborator

AshesITR commented Oct 9, 2020

Note that a somewhat hacky work-around is to source the file before linting / manually attach all needed packages.
This of course isn't ideal and doesn't work reliably in non-interactive sessions.

It works because the default environment in non-packages is globalenv(), where the manual changes can be seen by lintr.

@AshesITR
Copy link
Collaborator

This has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants