Skip to content

Infix operator lint metadata is not helpful for object_usage_linter #1497

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
MichaelChirico opened this issue Aug 9, 2022 · 2 comments · Fixed by #1525
Closed

Infix operator lint metadata is not helpful for object_usage_linter #1497

MichaelChirico opened this issue Aug 9, 2022 · 2 comments · Fixed by #1525
Labels
bug an unexpected problem or unintended behavior object linters 🏀

Comments

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Aug 9, 2022

lintr::lint("a <- function() {\n  1:10 %>% sum()\n}", lintr::object_usage_linter())
# <text>:1:6: warning: [object_usage_linter] no visible global function definition for ‘%>%’. Disable this with a comment: `# nolint` (one line) or `# lintr: disable` (multiple lines). See go/tidy-style#linting for more.
# a <- function() {
#      ^~~~~~~~~~~~

The linter anchors to the function definition, not to the usage of %>% itself. Similar goes for other infixes, e.g.

lintr::lint("a <- function() {\n  1:10 %>% sum()\n}", lintr::object_usage_linter())
# <text>:1:6: warning: [object_usage_linter] no visible global function definition for ‘:=’. Disable this with a comment: `# nolint` (one line) or `# lintr: disable` (multiple lines). See go/tidy-style#linting for more.
# a <- function(x) {
#      ^~~~~~~~~~~~~

I guess this is codetools' fault, but is there something we can do to patch it up? Or maybe we have a bad XPath?

@MichaelChirico MichaelChirico added feature a feature request or enhancement object linters 🏀 bug an unexpected problem or unintended behavior and removed feature a feature request or enhancement labels Aug 9, 2022
@MichaelChirico
Copy link
Collaborator Author

Native pipe also affected by this:

lintr::lint("foo <- function(x) {
  x |>
    dplyr::mutate(day = as.POSIXct(round(timestamp, units = 'days')))
}", lintr::object_usage_linter())
# <text>:2:3: warning: [object_usage_linter] unused argument (units = "days").
#   x |>
#   ^~~~

@MichaelChirico
Copy link
Collaborator Author

Ah, nevermind, I think this is not the same issue.

lintr::lint("foo <- function(x) {
  x |>
    sum(y)
}", lintr::object_usage_linter())
# <text>:3:9: warning: [object_usage_linter] no visible binding for global variable 'y'.
#     sum(y)
#         ^

correct answer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior object linters 🏀
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant