Skip to content
Open
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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

* Six linters fully deprecated in the previous release are now removed: `consecutive_stopifnot_linter()`, `extraction_operator_linter()`, `no_tab_linter()`, `single_quotes_linter()`, `unnecessary_nested_if_linter()`, and `unneeded_concatenation_linter()`.

## New and improved features

### Linter improvements

* `sort_linter()` recommends usage of `!is.unsorted(x)` over `identical(x, sort(x))` (#2921, @Bisaloo).

## Notes

* {lintr} now requires R 4.1.0
Expand Down
24 changes: 22 additions & 2 deletions R/sort_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ sort_linter <- function() {
and expr/expr = expr
and not(expr/EQ_SUB)
]"

sorted_identical_xpath <- "
parent::expr[not(SYMBOL_SUB)]
/parent::expr[
expr/SYMBOL_FUNCTION_CALL[text() = 'identical']
and expr/expr = expr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need more baroque handling here, a la #2901 and strip_comments_from_subtree(). Try test cases like:

identical(x, sort( # comment
  x
))
identical(foo(x), sort(foo( # comment
  x
)))

]
"

arguments_xpath <-
".//SYMBOL_SUB[text() = 'method' or text() = 'decreasing' or text() = 'na.last']"
Expand Down Expand Up @@ -143,8 +149,22 @@ sort_linter <- function() {
"Use is.unsorted(x) to test the unsortedness of a vector."
)

sorted_identical_expr <- xml_find_all(xml_calls, sorted_identical_xpath)
is_negated <- !is.na(
xml_find_first(sorted_identical_expr, "preceding-sibling::*[not(self::COMMENT)][1][self::OP-EXCLAMATION]")
)

lint_message <- c(
lint_message,
ifelse(
is_negated,
"Use is.unsorted(x) to test the unsortedness of a vector.",
"Use !is.unsorted(x) to test the sortedness of a vector."
)
)

sorted_lints <- xml_nodes_to_lints(
sorted_expr,
combine_nodesets(sorted_expr, sorted_identical_expr),
source_expression = source_expression,
lint_message = lint_message,
type = "warning"
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-sort_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ test_that("sort_linter skips allowed usages", {
expect_no_lint("x[order(x, y)]", linter)
# pretty sure this never makes sense, but test anyway
expect_no_lint("x[order(y, na.last = x)]", linter)

# is.unsorted false positives
expect_no_lint("identical(sort(x), y)", linter)
expect_no_lint("identical(sort(foo(x)), x)", linter)
})


Expand Down Expand Up @@ -134,15 +138,21 @@ test_that("sort_linter blocks simple disallowed usages for is.sorted cases", {
sorted_msg <- rex::rex("Use !is.unsorted(x) to test the sortedness of a vector.")

expect_lint("sort(x) == x", sorted_msg, linter)
expect_lint("identical(x, sort(x))", sorted_msg, linter)

# argument order doesn't matter
expect_lint("x == sort(x)", sorted_msg, linter)
expect_lint("identical(sort(x), x)", sorted_msg, linter)

# inverted version
expect_lint("sort(x) != x", unsorted_msg, linter)
expect_lint("!identical(x, sort(x))", unsorted_msg, linter)

# expression matching
expect_lint("sort(foo(x)) == foo(x)", sorted_msg, linter)
expect_lint("identical(foo(x), sort(foo(x)))", sorted_msg, linter)
expect_lint("identical(sort(foo(x)), foo(x))", sorted_msg, linter)

expect_lint(
trim_some("
sort(foo(x # comment
Expand Down
Loading