Skip to content

Commit

Permalink
move lint to outer expression (#2391)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Dec 5, 2023
1 parent 67754bc commit 8ae6da6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions R/unreachable_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ unreachable_code_linter <- function(allow_comment_regex = getOption("covr.exclud
")

xpath_if_while <- "
(//WHILE | //IF)
/following-sibling::expr[1][NUM_CONST[text() = 'FALSE']]
/following-sibling::expr[1]
(//WHILE | //IF)[following-sibling::expr[1]/NUM_CONST[text() = 'FALSE']]
/parent::expr
"

xpath_else <- "
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test-unreachable_code_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ test_that("unreachable_code_linter identifies unreachable code in conditional lo
}
")

expect_lint(lines, list(line_number = 3L, message = msg), linter)
expect_lint(lines, list(line_number = 2L, message = msg), linter)

lines <- trim_some("
foo <- function(bar) {
Expand All @@ -435,7 +435,7 @@ test_that("unreachable_code_linter identifies unreachable code in conditional lo
}
")

expect_lint(lines, list(line_number = 4L, message = msg), linter)
expect_lint(lines, list(line_number = 2L, message = msg), linter)

lines <- trim_some("
foo <- function(bar) {
Expand All @@ -449,7 +449,7 @@ test_that("unreachable_code_linter identifies unreachable code in conditional lo
}
")

expect_lint(lines, list(line_number = 6L, message = msg), linter)
expect_lint(lines, list(line_number = 4L, message = msg), linter)

lines <- trim_some("
foo <- function(bar) {
Expand All @@ -460,7 +460,7 @@ test_that("unreachable_code_linter identifies unreachable code in conditional lo
}
")

expect_lint(lines, list(line_number = 3L, message = msg), linter)
expect_lint(lines, list(line_number = 2L, message = msg), linter)

lines <- trim_some("
foo <- function(bar) {
Expand All @@ -472,21 +472,21 @@ test_that("unreachable_code_linter identifies unreachable code in conditional lo
}
")

expect_lint(lines, list(line_number = 4L, message = msg), linter)
expect_lint(lines, list(line_number = 2L, message = msg), linter)

lines <- "while (FALSE) x <- 3"

expect_lint(
lines,
list(line_number = 1L, ranges = list(c(15L, 20L)), message = msg),
list(line_number = 1L, ranges = list(c(1L, 20L)), message = msg),
linter
)

lines <- "if (FALSE) x <- 3 # Test comment"

expect_lint(
lines,
list(line_number = 1L, ranges = list(c(12L, 17L)), message = msg),
list(line_number = 1L, ranges = list(c(1L, 17L)), message = msg),
linter
)
})
Expand Down Expand Up @@ -551,8 +551,8 @@ test_that("unreachable_code_linter identifies unreachable code in mixed conditio
}
"),
list(
list(false_msg, line_number = 3L),
list(false_msg, line_number = 6L),
list(false_msg, line_number = 2L),
list(false_msg, line_number = 5L),
list(true_msg, line_number = 10L),
list(rex::rex("Code and comments coming after a return() or stop() should be removed."), line_number = 13L)
),
Expand All @@ -562,7 +562,7 @@ test_that("unreachable_code_linter identifies unreachable code in mixed conditio
expect_lint(
"if (FALSE) x <- 3 else if (TRUE) x + 3 else x + 4",
list(
list(false_msg, line_number = 1L, ranges = list(c(12L, 17L))),
list(false_msg, line_number = 1L, ranges = list(c(1L, 49L))),
list(
rex::rex("Code inside an else block after a deterministically true if condition should be removed."),
line_number = 1L,
Expand Down

0 comments on commit 8ae6da6

Please sign in to comment.