Skip to content

length_test_linter() for length(x == 0) #2124

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 8 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -116,6 +116,7 @@ Collate:
'is_numeric_linter.R'
'keyword_quote_linter.R'
'length_levels_linter.R'
'length_test_linter.R'
'lengths_linter.R'
'library_call_linter.R'
'line_length_linter.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export(is_lint_level)
export(is_numeric_linter)
export(keyword_quote_linter)
export(length_levels_linter)
export(length_test_linter)
export(lengths_linter)
export(library_call_linter)
export(line_length_linter)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* `scalar_in_linter()` for discouraging `%in%` when the right-hand side is a scalar, e.g. `x %in% 1` (part of #884, @MichaelChirico).
* `if_not_else_linter()` for encouraging `if` statements to be structured as `if (A) x else y` instead of `if (!A) y else x` (part of #884, @MichaelChirico).
* `repeat_linter()` for encouraging `repeat` for infinite loops instead of `while (TRUE)` (#2106, @MEO265).
* `length_test_linter()` detects the common mistake `length(x == 0)` which is meant to be `length(x) == 0` (#1991, @MichaelChirico).

## Changes to defaults

Expand Down
51 changes: 51 additions & 0 deletions R/length_test_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Check for a common mistake where length is applied in the wrong place
#'
#' Usage like `length(x == 0)` is a mistake. If you intended to check `x` is empty,
#' use `length(x) == 0`. Other mistakes are possible, but running `length()` on the
#' outcome of a logical comparison is never the best choice.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "length(x == 0)",
#' linters = length_test_linter()
#' )
#'
#' # okay
#' lint(
#' text = "length(x) > 0",
#' linters = length_test_linter()
#' )
#' @evalRd rd_tags("class_equals_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
length_test_linter <- function() {
xpath <- glue::glue("
//SYMBOL_FUNCTION_CALL[text() = 'length']
/parent::expr
/following-sibling::expr[{ xp_or(infix_metadata$xml_tag[infix_metadata$comparator]) }]
/parent::expr
")

Linter(function(source_expression) {
if (!is_lint_level(source_expression, "expression")) {
return(list())
}

xml <- source_expression$xml_parsed_content

bad_expr <- xml_find_all(xml, xpath)
expr_parts <- vapply(lapply(bad_expr, xml_find_all, "expr[2]/*"), xml_text, character(3L))
lint_message <- sprintf(
"Checking the length of a logical vector is likely a mistake. Did you mean `length(%s) %s %s`?",
expr_parts[1L, ], expr_parts[2L, ], expr_parts[3L, ]
)

xml_nodes_to_lints(
bad_expr,
source_expression = source_expression,
lint_message = lint_message,
type = "warning"
)
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ inner_combine_linter,efficiency consistency readability
is_numeric_linter,readability best_practices consistency
keyword_quote_linter,readability consistency style
length_levels_linter,readability best_practices consistency
length_test_linter,common_mistakes efficiency
lengths_linter,efficiency readability best_practices
library_call_linter,style best_practices readability
line_length_linter,style readability default configurable
Expand Down
1 change: 1 addition & 0 deletions man/common_mistakes_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.

32 changes: 32 additions & 0 deletions man/length_test_linter.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.

32 changes: 32 additions & 0 deletions tests/testthat/test-length_test_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("skips allowed usages", {
linter <- length_test_linter()

expect_lint("length(x) > 0", NULL, linter)
expect_lint("length(DF[key == val, cols])", NULL, linter)
})

test_that("blocks simple disallowed usages", {
linter <- length_test_linter()
lint_msg_stub <- rex::rex("Checking the length of a logical vector is likely a mistake. Did you mean ")

expect_lint("length(x == 0)", rex::rex(lint_msg_stub, "`length(x) == 0`?"), linter)
expect_lint("length(x == y)", rex::rex(lint_msg_stub, "`length(x) == y`?"), linter)
expect_lint("length(x + y == 2)", rex::rex(lint_msg_stub, "`length(x+y) == 2`?"), linter)
})

local({
ops <- c(lt = "<", lte = "<=", gt = ">", gte = ">=", eq = "==", neq = "!=")
linter <- length_test_linter()
lint_msg_stub <- rex::rex("Checking the length of a logical vector is likely a mistake. Did you mean ")

patrick::with_parameters_test_that(
"all logical operators detected",
expect_lint(
paste("length(x", op, "y)"),
rex::rex("`length(x) ", op, " y`?"),
linter
),
op = ops,
.test_name = names(ops)
)
})