Skip to content

New any_is_na_linter #975

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

Merged
merged 4 commits into from
Mar 22, 2022
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Collate:
'aaa.R'
'actions.R'
'addins.R'
'any_is_na_linter.R'
'assignment_linter.R'
'backport_linter.R'
'cache.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(T_and_F_symbol_linter)
export(absolute_path_linter)
export(all_undesirable_functions)
export(all_undesirable_operators)
export(any_is_na_linter)
export(assignment_linter)
export(available_linters)
export(backport_linter)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function calls. (#850, #851, @renkun-ken)
* `expect_length_linter()` Require usage of `expect_length(x, n)` over `expect_equal(length(x), n)` and similar
* `expect_identical_linter()` Require usage of `expect_identical()` by default, and `expect_equal()` only by exception
* `expect_comparison_linter()` Require usage of `expect_gt(x, y)` over `expect_true(x > y)` and similar
* `any_is_na_linter()` Require usage of `anyNA(x)` over `any(is.na(x))`
* `assignment_linter()` now lints right assignment (`->` and `->>`) and gains two arguments. `allow_cascading_assign` (`TRUE` by default) toggles whether to lint `<<-` and `->>`; `allow_right_assign` toggles whether to lint `->` and `->>` (#915, @michaelchirico)
* `infix_spaces_linter()` gains argument `exclude_operators` to disable lints on selected infix operators. By default, all "low-precedence" operators throw lints; see `?infix_spaces_linter` for an enumeration of these. (#914 @michaelchirico)
* `infix_spaces_linter()` now throws a lint on `a~b` and `function(a=1) {}` (#930, @michaelchirico)
Expand Down
37 changes: 37 additions & 0 deletions R/any_is_na_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Require usage of anyNA over any(is.na(.))
#'
#' [anyNA()] exists as a replacement for `any(is.na(.))` which is more efficient
#' for simple objects, and in the worst case is the same efficiency. Therefore
#' it should be used in all situations instead of the latter.
#'
#' @evalRd rd_tags("any_is_na_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
any_is_na_linter <- function() {
Linter(function(source_file) {
if (length(source_file$parsed_content) == 0L) {
return(list())
}

xml <- source_file$xml_parsed_content

xpath <- "//expr[
expr[SYMBOL_FUNCTION_CALL[text() = 'any']]
and expr[expr[SYMBOL_FUNCTION_CALL[text() = 'is.na']]]
and (
count(expr) = 2
or (count(expr) = 3 and SYMBOL_SUB[text() = 'na.rm'])
)
]"

bad_expr <- xml2::xml_find_all(xml, xpath)

return(lapply(
bad_expr,
xml_nodes_to_lint,
source_file = source_file,
lint_message = "anyNA(x) is better than any(is.na(x)).",
type = "warning"
))
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linter,tags
absolute_path_linter,robustness best_practices configurable
any_is_na_linter,efficiency best_practices
assignment_linter,style consistency default
backport_linter,robustness configurable package_development
closed_curly_linter,style readability default configurable
Expand Down
19 changes: 19 additions & 0 deletions man/any_is_na_linter.Rd

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

1 change: 1 addition & 0 deletions man/best_practices_linters.Rd

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

1 change: 1 addition & 0 deletions man/efficiency_linters.Rd

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

5 changes: 3 additions & 2 deletions man/linters.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-any_is_na_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test_that("any_is_na_linter skips allowed usages", {
expect_lint("x <- any(y)", NULL, any_is_na_linter())

expect_lint("y <- is.na(z)", NULL, any_is_na_linter())

# extended usage of ... arguments to any is not covered
expect_lint("any(is.na(y), b)", NULL, any_is_na_linter())
expect_lint("any(b, is.na(y))", NULL, any_is_na_linter())
})

test_that("any_is_na_linter blocks simple disallowed usages", {
lint_message <- rex::rex("anyNA(x) is better than any(is.na(x)).")
expect_lint("any(is.na(x))", lint_message, any_is_na_linter())

expect_lint("any(is.na(foo(x)))", lint_message, any_is_na_linter())

# na.rm doesn't really matter for this since is.na can't return NA
expect_lint("any(is.na(x), na.rm = TRUE)", lint_message, any_is_na_linter())

# also catch nested usage
expect_lint("foo(any(is.na(x)))", lint_message, any_is_na_linter())
})