Skip to content
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

Improve object_usage_linter() #1715

Merged
merged 5 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* `assignment_linter()` no longer lints assignments in braces that include comments when `allow_trailing = FALSE` (#1701, @ashbaldry)

* `object_usage_linter()` no longer silently ignores usage warnings that don't contain a quoted name (#1714, @AshesITR)

## Changes to defaults

* Set the default for the `except` argument in `duplicate_argument_linter()` to `c("mutate", "transmute")`.
Expand Down
39 changes: 21 additions & 18 deletions R/object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) {
},
integer(1L)
)

nodes <- unclass(lintable_symbols)[matched_symbol]
nodes[is.na(matched_symbol)] <- list(fun_assignment)

# fallback to line based matching if no symbol is found
nodes[is.na(matched_symbol)] <- lapply(which(is.na(matched_symbol)), function(i) {
line_based_match <- xml2::xml_find_first(
fun_assignment,
glue::glue("descendant::expr[@line1 = {res$line1[i]} and @line2 = {res$line2[i]}]")
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
)
if (is.na(line_based_match)) fun_assignment else line_based_match
})

xml_nodes_to_lints(nodes, source_expression = source_expression, lint_message = res$message, type = "warning")
})
Expand Down Expand Up @@ -268,30 +275,26 @@ parse_check_usage <- function(expression,
function_name,
capture(
name = "message",
anything,
one_of(quote, "\u2018"),
capture(name = "name", anything),
one_of(quote, "\u2019"),
anything
zero_or_more(any, type = "lazy"),
maybe(
one_of(quote, "\u2018"),
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
capture(name = "name", anything),
one_of(quote, "\u2019"),
anything
)
),
line_info
)
)

# nocov start
missing <- is.na(res$message)
if (any(missing)) {
res[missing, ] <- re_matches(
vals[missing],
rex(
function_name,
capture(
name = "message",
"possible error in ", capture(name = "name", anything), ": ", anything
),
line_info
)
)
warning("Possible bug in lintr: Couldn't parse usage message ", sQuote(vals[missing][[1L]]), ". ",
"Ignoring ", sum(missing), " usage warnings.")
res <- res[!missing, ]
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
}
# nocov end

res <- res[!is.na(res$message), ]

Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ test_that("fallback works", {
"),
list(
message = rex::rex("no visible global function definition for ", anything, "non_existing_assign<-"),
column_number = 6L
line_number = 2L,
column_number = 3L
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
),
object_usage_linter()
)
Expand Down Expand Up @@ -606,3 +607,20 @@ test_that("missing libraries don't cause issue", {
object_usage_linter()
)
})

test_that("messages without a quoted name are caught", {
# regression test for #1714
expect_lint(
trim_some("
foo <- function() {
a <- ...
a
}
"),
list(
message = "... may be used in an incorrect context",
line_number = 2L
),
object_usage_linter()
)
})