From 8ae6da6924d2c46f90684376f0a2afb23e97eab7 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Tue, 5 Dec 2023 13:34:17 +0800 Subject: [PATCH] move lint to outer expression (#2391) --- R/unreachable_code_linter.R | 5 ++--- tests/testthat/test-unreachable_code_linter.R | 20 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/R/unreachable_code_linter.R b/R/unreachable_code_linter.R index f84aab614..ccd2140ca 100644 --- a/R/unreachable_code_linter.R +++ b/R/unreachable_code_linter.R @@ -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 <- " diff --git a/tests/testthat/test-unreachable_code_linter.R b/tests/testthat/test-unreachable_code_linter.R index 411ca146e..346a80513 100644 --- a/tests/testthat/test-unreachable_code_linter.R +++ b/tests/testthat/test-unreachable_code_linter.R @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -472,13 +472,13 @@ 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 ) @@ -486,7 +486,7 @@ test_that("unreachable_code_linter identifies unreachable code in conditional lo 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 ) }) @@ -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) ), @@ -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,