View source: R/unreachable_code_linter.R
unreachable_code_linter | R Documentation |
Code after e.g. a return()
or stop()
or in deterministically false conditional loops like if (FALSE)
can't be reached;
typically this is vestigial code left after refactoring or sandboxing code, which
is fine for exploration, but shouldn't ultimately be checked in. Comments
meant for posterity should be placed before the final return()
.
unreachable_code_linter(
allow_comment_regex = getOption("covr.exclude_end", "# nocov end")
)
allow_comment_regex |
Character vector of regular expressions which identify
comments to exclude when finding unreachable terminal comments. By default, this
includes the default "skip region" end marker for |
best_practices, configurable, readability
linters for a complete list of linters available in lintr.
# will produce lints
code_lines <- "f <- function() {\n return(1 + 1)\n 2 + 2\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
code_lines <- "if (FALSE) {\n 2 + 2\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
code_lines <- "while (FALSE) {\n 2 + 2\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
code_lines <- "f <- function() {\n return(1)\n # end skip\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
# okay
code_lines <- "f <- function() {\n return(1 + 1)\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
code_lines <- "if (foo) {\n 2 + 2\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
code_lines <- "while (foo) {\n 2 + 2\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter()
)
code_lines <- "f <- function() {\n return(1)\n # end skip\n}"
writeLines(code_lines)
lint(
text = code_lines,
linters = unreachable_code_linter(allow_comment_regex = "# end skip")
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.