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

Also lint nrow(filter(.)) in nrow_subset_linter() #2457

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* `consecutive_mutate_linter()` for encouraging consecutive calls to `dplyr::mutate()` to be combined (part of #884, @MichaelChirico).
* `if_switch_linter()` for encouraging `switch()` over repeated `if`/`else` tests (part of #884, @MichaelChirico).
* `nested_pipe_linter()` for discouraging pipes within pipes, e.g. `df1 %>% inner_join(df2 %>% select(a, b))` (part of #884, @MichaelChirico).
* `nrow_subset_linter()` for discouraging usage like `nrow(subset(x, conditions))` in favor of something like `with(x, sum(conditions))` which doesn't require a full subset of `x` (#2314 and part of #884, @MichaelChirico).
* `nrow_subset_linter()` for discouraging usage like `nrow(subset(x, conditions))` in favor of something like `with(x, sum(conditions))` which doesn't require a full subset of `x` (#2313, #2314 and part of #884, @MichaelChirico).
* `pipe_return_linter()` for discouraging usage of `return()` inside a {magrittr} pipeline (part of #884, @MichaelChirico).
* `one_call_pipe_linter()` for discouraging one-step pipelines like `x |> as.character()` (#2330 and part of #884, @MichaelChirico).
* `object_overwrite_linter()` for discouraging re-use of upstream package exports as local variables (#2344, #2346 and part of #884, @MichaelChirico and @AshesITR).
Expand Down
12 changes: 11 additions & 1 deletion R/nrow_subset_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
#' linters = nrow_subset_linter()
#' )
#'
#' lint(
#' text = "nrow(filter(x, is_treatment))",
#' linters = nrow_subset_linter()
#' )
#'
#' lint(
#' text = "x %>% filter(x, is_treatment) %>% nrow()",
#' linters = nrow_subset_linter()
#' )
#'
#' # okay
#' lint(
#' text = "with(x, sum(is_treatment, na.rm = TRUE))",
Expand All @@ -25,7 +35,7 @@
#' @include shared_constants.R
#' @export
nrow_subset_linter <- make_linter_from_function_xpath(
function_names = "subset",
function_names = c("subset", "filter"),
xpath = glue("
parent::expr
/parent::expr
Expand Down
10 changes: 10 additions & 0 deletions man/nrow_subset_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions tests/testthat/test-nrow_subset_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ test_that("nrow_subset_linter blocks subset() cases", {
)
})

test_that("nrow_subset_linter blocks filter() cases", {
expect_lint(
"nrow(filter(x, y == z))",
rex::rex("Use arithmetic to count the number of rows satisfying a condition"),
nrow_subset_linter()
)
})

test_that("lints vectorize", {
lint_msg <- rex::rex("Use arithmetic to count the number of rows satisfying a condition")

Expand All @@ -21,10 +29,12 @@ test_that("lints vectorize", {
nrow(subset(x, y == z))
subset(x) %>% transform(m = 2)
nrow(subset(a, b == c))
x %>% filter(y == z) %>% nrow()
}"),
list(
list(lint_msg, line_number = 2L),
list(lint_msg, line_number = 4L)
list(lint_msg, line_number = 4L),
list(lint_msg, line_number = 5L)
),
nrow_subset_linter()
)
Expand All @@ -35,7 +45,7 @@ test_that("linter is pipeline-aware", {
lint_msg <- "Use arithmetic to count the number of rows satisfying a condition"

expect_lint("x %>% subset(y == z) %>% nrow()", lint_msg, linter)
expect_lint("subset(x) %>% nrow()", lint_msg, linter)
expect_lint("filter(x, y == z) %>% nrow()", lint_msg, linter)

skip_if_not_r_version("4.1.0")
expect_lint("x |> subset(y == z) |> nrow()", lint_msg, linter)
Expand Down
Loading