View source: R/unnecessary_nesting_linter.R
unnecessary_nesting_linter | R Documentation |
Excessive nesting harms readability. Use helper functions or early returns to reduce nesting wherever possible.
unnecessary_nesting_linter(
allow_assignment = TRUE,
allow_functions = c("switch", "try", "tryCatch", "withCallingHandlers", "quote",
"expression", "bquote", "substitute", "with_parameters_test_that", "reactive",
"observe", "observeEvent", "renderCachedPlot", "renderDataTable", "renderImage",
"renderPlot", "renderPrint", "renderTable", "renderText", "renderUI")
)
allow_assignment |
Logical, default |
allow_functions |
Character vector of functions which always allow
one-child braced expressions. |
best_practices, configurable, consistency, readability
cyclocomp_linter()
for another linter that penalizes overly complex code.
linters for a complete list of linters available in lintr.
# will produce lints
code <- "if (A) {\n stop('A is bad!')\n} else {\n do_good()\n}"
writeLines(code)
lint(
text = code,
linters = unnecessary_nesting_linter()
)
code <- "tryCatch(\n {\n foo()\n },\n error = identity\n)"
writeLines(code)
lint(
text = code,
linters = unnecessary_nesting_linter()
)
code <- "expect_warning(\n {\n x <- foo()\n },\n 'warned'\n)"
writeLines(code)
lint(
text = code,
linters = unnecessary_nesting_linter(allow_assignment = FALSE)
)
writeLines("if (x) { \n if (y) { \n return(1L) \n } \n}")
lint(
text = "if (x) { \n if (y) { \n return(1L) \n } \n}",
linters = unnecessary_nesting_linter()
)
lint(
text = "my_quote({x})",
linters = unnecessary_nesting_linter()
)
# okay
code <- "if (A) {\n stop('A is bad because a.')\n} else {\n stop('!A is bad too.')\n}"
writeLines(code)
lint(
text = code,
linters = unnecessary_nesting_linter()
)
code <- "capture.output({\n foo()\n})"
writeLines(code)
lint(
text = code,
linters = unnecessary_nesting_linter()
)
code <- "expect_warning(\n {\n x <- foo()\n },\n 'warned'\n)"
writeLines(code)
lint(
text = code,
linters = unnecessary_nesting_linter()
)
writeLines("if (x && y) { \n return(1L) \n}")
lint(
text = "if (x && y) { \n return(1L) \n}",
linters = unnecessary_nesting_linter()
)
writeLines("if (x) { \n y <- x + 1L\n if (y) { \n return(1L) \n } \n}")
lint(
text = "if (x) { \n y <- x + 1L\n if (y) { \n return(1L) \n } \n}",
linters = unnecessary_nesting_linter()
)
lint(
text = "my_quote({x})",
linters = unnecessary_nesting_linter(allow_functions = "my_quote")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.