Skip to content

Commit

Permalink
reduce nesting where possible (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Nov 19, 2023
1 parent 2460465 commit 9920110
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
3 changes: 1 addition & 2 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions R/settings_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
24 changes: 0 additions & 24 deletions inst/example/complexity.R

This file was deleted.

35 changes: 29 additions & 6 deletions tests/testthat/test-cyclocomp_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
34 changes: 28 additions & 6 deletions tests/testthat/test-spaces_left_parentheses_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})

0 comments on commit 9920110

Please sign in to comment.