From 99201100b9f35dfaa7908c64f0b1debf8aa992cc Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 18 Nov 2023 22:39:23 -0800 Subject: [PATCH] reduce nesting where possible (#2303) --- R/cache.R | 3 +- R/settings_utils.R | 3 +- inst/example/complexity.R | 24 ------------- tests/testthat/test-cyclocomp_linter.R | 35 +++++++++++++++---- .../test-spaces_left_parentheses_linter.R | 34 ++++++++++++++---- 5 files changed, 59 insertions(+), 40 deletions(-) delete mode 100644 inst/example/complexity.R diff --git a/R/cache.R b/R/cache.R index 88ddf0c38..8cc767a56 100644 --- a/R/cache.R +++ b/R/cache.R @@ -127,9 +127,8 @@ retrieve_lint <- function(cache, expr, linter, lines) { ) if (is.na(new_line_number)) { return(NULL) - } else { - lints[[i]]$line_number <- new_line_number } + lints[[i]]$line_number <- new_line_number } cache_lint(cache, expr, linter, lints) lints diff --git a/R/settings_utils.R b/R/settings_utils.R index 544de7d85..a04ef5b55 100644 --- a/R/settings_utils.R +++ b/R/settings_utils.R @@ -108,7 +108,6 @@ find_local_config <- function(path, config_file) { pkg_name <- function(path = find_package()) { if (is.null(path)) { return(NULL) - } else { - read.dcf(file.path(path, "DESCRIPTION"), fields = "Package")[1L] } + read.dcf(file.path(path, "DESCRIPTION"), fields = "Package")[1L] } diff --git a/inst/example/complexity.R b/inst/example/complexity.R deleted file mode 100644 index 4bc8e2c8b..000000000 --- a/inst/example/complexity.R +++ /dev/null @@ -1,24 +0,0 @@ -complexity <- function(x) { - if (x > 0.0) { - if (x > 10.0) { - if (x > 20.0) { - x <- x / 2.0 - } else { - return(x) - } - } else { - return(x) - } - } else { - if (x < -10.0) { - if (x < -20.0) { - x <- x * 2.0 - } else { - return(x) - } - } else { - return(x) - } - } - x -} diff --git a/tests/testthat/test-cyclocomp_linter.R b/tests/testthat/test-cyclocomp_linter.R index 0342c3599..9162c75c7 100644 --- a/tests/testthat/test-cyclocomp_linter.R +++ b/tests/testthat/test-cyclocomp_linter.R @@ -11,14 +11,37 @@ test_that("returns the correct linting", { "unexpected symbol", cc_linter_2 ) - complexity <- readLines( - system.file("example/complexity.R", package = "lintr") - ) - expect_lint(complexity, lint_msg, cc_linter_2) + complex_lines <- trim_some(" + complexity <- function(x) { + if (x > 0.0) { + if (x > 10.0) { + if (x > 20.0) { + x <- x / 2.0 + } else { + return(x) + } + } else { + return(x) + } + } else { + if (x < -10.0) { + if (x < -20.0) { + x <- x * 2.0 + } else { + return(x) + } + } else { + return(x) + } + } + x + } + ") + expect_lint(complex_lines, lint_msg, cc_linter_2) expect_lint( - complexity, + complex_lines, "should have cyclomatic complexity of less than 2, this has 10", cc_linter_2 ) - expect_lint(complexity, NULL, cyclocomp_linter(10L)) + expect_lint(complex_lines, NULL, cyclocomp_linter(10L)) }) diff --git a/tests/testthat/test-spaces_left_parentheses_linter.R b/tests/testthat/test-spaces_left_parentheses_linter.R index 707d42102..b50e48efa 100644 --- a/tests/testthat/test-spaces_left_parentheses_linter.R +++ b/tests/testthat/test-spaces_left_parentheses_linter.R @@ -63,13 +63,35 @@ test_that("spaces_left_parentheses_linter blocks disallowed usages", { }) test_that("doesn't produce a warning", { - # complexity.R contains a function with nested if-statements where the conditional includes a unary minus. + # this contains a function with nested if-statements where the conditional includes a unary minus. # This specific constellation with another if-statement at the same nesting level on the other enclosing if-branch # caused a warning in spaces_left_parentheses_linter (e.g. 84bc3a is broken) + complex_lines <- trim_some(" + complexity <- function(x) { + if (x > 0.0) { + if (x > 10.0) { + if (x > 20.0) { + x <- x / 2.0 + } else { + return(x) + } + } else { + return(x) + } + } else { + if (x < -10.0) { + if (x < -20.0) { + x <- x * 2.0 + } else { + return(x) + } + } else { + return(x) + } + } + x + } + ") - expect_silent( - lint( - system.file("example/complexity.R", package = "lintr") - ) - ) + expect_no_warning(lint(text = complex_lines, linters = spaces_left_parentheses_linter())) })