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

Mention the specific operator in lint message for vector_logic_linter() #2398

Merged
merged 3 commits into from
Dec 6, 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
3 changes: 2 additions & 1 deletion R/vector_logic_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ vector_logic_linter <- function() {
if (is.null(xml)) return(list())
bad_expr <- xml_find_all(xml, xpath)

op <- xml_text(bad_expr)
xml_nodes_to_lints(
bad_expr,
source_expression = source_expression,
lint_message = "Conditional expressions require scalar logical operators (&& and ||)",
lint_message = sprintf("Use `%s` in conditional expressions.", strrep(op, 2L)),
type = "warning"
)
})
Expand Down
29 changes: 18 additions & 11 deletions tests/testthat/test-vector_logic_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,37 @@ test_that("vector_logic_linter skips allowed usages", {

test_that("vector_logic_linter blocks simple disallowed usages", {
linter <- vector_logic_linter()
lint_msg <- rex::rex("Conditional expressions require scalar logical operators")

expect_lint("if (TRUE & FALSE) 1", lint_msg, linter)
expect_lint("while (TRUE | TRUE) 2", lint_msg, linter)
expect_lint("if (TRUE & FALSE) 1", rex::rex("Use `&&` in conditional expressions."), linter)
expect_lint("while (TRUE | TRUE) 2", rex::rex("Use `||` in conditional expressions."), linter)
})

test_that("vector_logic_linter detects nested conditions", {
linter <- vector_logic_linter()
lint_msg <- rex::rex("Conditional expressions require scalar logical operators")

expect_lint("if (TRUE & TRUE || FALSE) 4", lint_msg, linter)
expect_lint("if (TRUE && (TRUE | FALSE)) 4", lint_msg, linter)
expect_lint(
"if (TRUE & TRUE || FALSE) 4",
list(rex::rex("Use `&&` in conditional expressions."), column_number = 10L),
linter
)
expect_lint(
"if (TRUE && (TRUE | FALSE)) 4",
list(rex::rex("Use `||` in conditional expressions."), column_number = 19L),
linter
)
})

test_that("vector_logic_linter catches usages in expect_true()/expect_false()", {
linter <- vector_logic_linter()
lint_msg <- rex::rex("Conditional expressions require scalar logical operators")
and_msg <- rex::rex("Use `&&` in conditional expressions.")
or_msg <- rex::rex("Use `||` in conditional expressions.")

expect_lint("expect_true(TRUE & FALSE)", lint_msg, linter)
expect_lint("expect_false(TRUE | TRUE)", lint_msg, linter)
expect_lint("expect_true(TRUE & FALSE)", and_msg, linter)
expect_lint("expect_false(TRUE | TRUE)", or_msg, linter)

# ditto with namespace qualification
expect_lint("testthat::expect_true(TRUE & FALSE)", lint_msg, linter)
expect_lint("testthat::expect_false(TRUE | TRUE)", lint_msg, linter)
expect_lint("testthat::expect_true(TRUE & FALSE)", and_msg, linter)
expect_lint("testthat::expect_false(TRUE | TRUE)", or_msg, linter)
})

test_that("vector_logic_linter doesn't get mixed up from complex usage", {
Expand Down
Loading