From 5ea4ca85bda69a35cc056c4c8f9fc3c19b4d1449 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Wed, 12 Oct 2022 21:16:04 +0200 Subject: [PATCH] Add examples to documentation: Part-9 (cleanup) (#1688) * Examples for `fixed_regex_linter()` * cleanup * more cleanup * review comments * another example for fixed regex --- NEWS.md | 2 ++ R/assignment_linter.R | 12 ++++++-- R/class_equals_linter.R | 8 ++--- R/condition_message_linter.R | 10 +++---- R/conjunct_test_linter.R | 2 +- R/expect_named_linter.R | 8 ++--- R/expect_s3_class_linter.R | 8 ++--- R/expect_s4_class_linter.R | 4 +-- R/expect_type_linter.R | 6 ++-- R/extraction_operator_linter.R | 6 ++-- R/fixed_regex_linter.R | 49 ++++++++++++++++++++++++++++++ R/is_numeric_linter.R | 10 +++---- R/missing_argument_linter.R | 8 ++--- R/nested_ifelse_linter.R | 6 ++-- R/package_hooks_linter.R | 2 +- R/path_linters.R | 30 ++++++++++++------- R/sprintf_linter.R | 6 ++-- man/absolute_path_linter.Rd | 7 +++-- man/assignment_linter.Rd | 12 ++++++-- man/class_equals_linter.Rd | 8 ++--- man/condition_message_linter.Rd | 10 +++---- man/conjunct_test_linter.Rd | 2 +- man/expect_named_linter.Rd | 8 ++--- man/expect_s3_class_linter.Rd | 8 ++--- man/expect_s4_class_linter.Rd | 4 +-- man/expect_type_linter.Rd | 6 ++-- man/extraction_operator_linter.Rd | 6 ++-- man/fixed_regex_linter.Rd | 50 +++++++++++++++++++++++++++++++ man/is_numeric_linter.Rd | 10 +++---- man/missing_argument_linter.Rd | 8 ++--- man/nested_ifelse_linter.Rd | 6 ++-- man/nonportable_path_linter.Rd | 3 +- man/package_hooks_linter.Rd | 2 +- man/sprintf_linter.Rd | 6 ++-- 34 files changed, 228 insertions(+), 105 deletions(-) diff --git a/NEWS.md b/NEWS.md index 71233ff16..142384425 100644 --- a/NEWS.md +++ b/NEWS.md @@ -48,6 +48,8 @@ * `sprintf_linter()` also applies to `gettextf()` (#1677, @MichaelChirico) +* Documentation for all linters contains examples of code that does and does not produce lints (#1492, @IndrajeetPatil). + ### New linters * `unnecessary_lambda_linter()`: detect unnecessary lambdas (anonymous functions), e.g. diff --git a/R/assignment_linter.R b/R/assignment_linter.R index d5eabbcd8..eee461aa4 100644 --- a/R/assignment_linter.R +++ b/R/assignment_linter.R @@ -14,8 +14,10 @@ #' linters = assignment_linter() #' ) #' +#' code_lines <- "1 -> x\n2 ->> y" +#' writeLines(code_lines) #' lint( -#' text = "1 -> x; 2 ->> y", +#' text = code_lines, #' linters = assignment_linter() #' ) #' @@ -25,14 +27,18 @@ #' linters = assignment_linter() #' ) #' +#' code_lines <- "x <- 1\ny <<- 2" +#' writeLines(code_lines) #' lint( -#' text = "x <- 1; y <<- 2", +#' text = code_lines, #' linters = assignment_linter() #' ) #' #' # customizing using arguments +#' code_lines <- "1 -> x\n2 ->> y" +#' writeLines(code_lines) #' lint( -#' text = "1 -> x; 2 ->> y", +#' text = code_lines, #' linters = assignment_linter(allow_right_assign = TRUE) #' ) #' diff --git a/R/class_equals_linter.R b/R/class_equals_linter.R index 52eea9377..33a4d4960 100644 --- a/R/class_equals_linter.R +++ b/R/class_equals_linter.R @@ -10,23 +10,23 @@ #' @examples #' # will produce lints #' lint( -#' text = "is_lm <- class(x) == 'lm'", +#' text = 'is_lm <- class(x) == "lm"', #' linters = class_equals_linter() #' ) #' #' lint( -#' text = "if ('lm' %in% class(x)) is_lm <- TRUE", +#' text = 'if ("lm" %in% class(x)) is_lm <- TRUE', #' linters = class_equals_linter() #' ) #' #' # okay #' lint( -#' text = "is_lm <- inherits(x, 'lm')", +#' text = 'is_lm <- inherits(x, "lm")', #' linters = class_equals_linter() #' ) #' #' lint( -#' text = "if (inherits(x, 'lm')) is_lm <- TRUE", +#' text = 'if (inherits(x, "lm")) is_lm <- TRUE', #' linters = class_equals_linter() #' ) #' diff --git a/R/condition_message_linter.R b/R/condition_message_linter.R index 4e7de7b34..694a29908 100644 --- a/R/condition_message_linter.R +++ b/R/condition_message_linter.R @@ -12,28 +12,28 @@ #' @examples #' # will produce lints #' lint( -#' text = "stop(paste('a string', 'another'))", +#' text = 'stop(paste("a string", "another")"', #' linters = condition_message_linter() #' ) #' #' lint( -#' text = "warning(paste0('a string', ' another'))", +#' text = 'warning(paste0("a string", " another")', #' linters = condition_message_linter() #' ) #' #' # okay #' lint( -#' text = "stop('a string', ' another')", +#' text = 'stop("a string", " another")', #' linters = condition_message_linter() #' ) #' #' lint( -#' text = "warning('a string', ' another')", +#' text = 'warning("a string", " another")', #' linters = condition_message_linter() #' ) #' #' lint( -#' text = "warning(paste('a string', 'another', sep = '-'))", +#' text = 'warning(paste("a string", "another", sep = "-"))', #' linters = condition_message_linter() #' ) #' diff --git a/R/conjunct_test_linter.R b/R/conjunct_test_linter.R index fa5bdcd4d..6c81f1ade 100644 --- a/R/conjunct_test_linter.R +++ b/R/conjunct_test_linter.R @@ -34,7 +34,7 @@ #' ) #' #' lint( -#' text = "stopifnot('x must be a logical scalar' = length(x) == 1 && is.logical(x) && !is.na(x))", +#' text = 'stopifnot("x must be a logical scalar" = length(x) == 1 && is.logical(x) && !is.na(x))', #' linters = conjunct_test_linter(allow_named_stopifnot = TRUE) #' ) #' diff --git a/R/expect_named_linter.R b/R/expect_named_linter.R index e7c804d27..9bb82e254 100644 --- a/R/expect_named_linter.R +++ b/R/expect_named_linter.R @@ -7,23 +7,23 @@ #' @examples #' # will produce lints #' lint( -#' text = "expect_equal(names(x), 'a')", +#' text = 'expect_equal(names(x), "a")', #' linters = expect_named_linter() #' ) #' #' # okay #' lint( -#' text = "expect_named(x, 'a')", +#' text = 'expect_named(x, "a")', #' linters = expect_named_linter() #' ) #' #' lint( -#' text = "expect_equal(colnames(x), 'a')", +#' text = 'expect_equal(colnames(x), "a")', #' linters = expect_named_linter() #' ) #' #' lint( -#' text = "expect_equal(dimnames(x), 'a')", +#' text = 'expect_equal(dimnames(x), "a")', #' linters = expect_named_linter() #' ) #' diff --git a/R/expect_s3_class_linter.R b/R/expect_s3_class_linter.R index 4d9733f8c..245a644de 100644 --- a/R/expect_s3_class_linter.R +++ b/R/expect_s3_class_linter.R @@ -8,23 +8,23 @@ #' @examples #' # will produce lints #' lint( -#' text = "expect_equal(class(x), 'data.frame')", +#' text = 'expect_equal(class(x), "data.frame")', #' linters = expect_s3_class_linter() #' ) #' #' lint( -#' text = "expect_equal(class(x), 'numeric')", +#' text = 'expect_equal(class(x), "numeric")', #' linters = expect_s3_class_linter() #' ) #' #' # okay #' lint( -#' text = "expect_s3_class(x, 'data.frame')", +#' text = 'expect_s3_class(x, "data.frame")', #' linters = expect_s3_class_linter() #' ) #' #' lint( -#' text = "expect_type(x, 'double')", +#' text = 'expect_type(x, "double")', #' linters = expect_s3_class_linter() #' ) #' diff --git a/R/expect_s4_class_linter.R b/R/expect_s4_class_linter.R index c82df6456..95f10d1aa 100644 --- a/R/expect_s4_class_linter.R +++ b/R/expect_s4_class_linter.R @@ -7,13 +7,13 @@ #' @examples #' # will produce lints #' lint( -#' text = "expect_true(is(x, 'Matrix'))", +#' text = 'expect_true(is(x, "Matrix"))', #' linters = expect_s4_class_linter() #' ) #' #' # okay #' lint( -#' text = "expect_s4_class(x, 'Matrix')", +#' text = 'expect_s4_class(x, "Matrix")', #' linters = expect_s4_class_linter() #' ) #' diff --git a/R/expect_type_linter.R b/R/expect_type_linter.R index 6aec31fd3..028403b70 100644 --- a/R/expect_type_linter.R +++ b/R/expect_type_linter.R @@ -8,18 +8,18 @@ #' @examples #' # will produce lints #' lint( -#' text = "expect_equal(typeof(x), 'double')", +#' text = 'expect_equal(typeof(x), "double")', #' linters = expect_type_linter() #' ) #' #' lint( -#' text = "expect_identical(typeof(x), 'double')", +#' text = 'expect_identical(typeof(x), "double")', #' linters = expect_type_linter() #' ) #' #' # okay #' lint( -#' text = "expect_type(x, 'double')", +#' text = 'expect_type(x, "double")', #' linters = expect_type_linter() #' ) #' diff --git a/R/extraction_operator_linter.R b/R/extraction_operator_linter.R index be1592472..c39e7880c 100644 --- a/R/extraction_operator_linter.R +++ b/R/extraction_operator_linter.R @@ -29,18 +29,18 @@ #' @examples #' # will produce lints #' lint( -#' text = "iris['Species']", +#' text = 'iris["Species"]', #' linters = extraction_operator_linter() #' ) #' #' lint( -#' text = "iris$Species", +#' text = 'iris$Species', #' linters = extraction_operator_linter() #' ) #' #' # okay #' lint( -#' text = "iris[['Species']]", +#' text = 'iris[["Species"]]', #' linters = extraction_operator_linter() #' ) #' diff --git a/R/fixed_regex_linter.R b/R/fixed_regex_linter.R index f4fd972a7..5f066372c 100644 --- a/R/fixed_regex_linter.R +++ b/R/fixed_regex_linter.R @@ -10,6 +10,55 @@ #' likely cases. It should _never_ report false positives, however; please #' report false positives as an error. #' +#' @examples +#' # will produce lints +#' code_lines <- 'gsub("\\\\.", "", x)' +#' writeLines(code_lines) +#' lint( +#' text = code_lines, +#' linters = fixed_regex_linter() +#' ) +#' +#' lint( +#' text = 'grepl("a[*]b", x)', +#' linters = fixed_regex_linter() +#' ) +#' +#' code_lines <- 'stringr::str_subset(x, "\\\\$")' +#' writeLines(code_lines) +#' lint( +#' text = code_lines, +#' linters = fixed_regex_linter() +#' ) +#' +#' lint( +#' text = 'grepl("Munich", address)', +#' linters = fixed_regex_linter() +#' ) +#' +#' # okay +#' code_lines <- 'gsub("\\\\.", "", x, fixed = TRUE)' +#' writeLines(code_lines) +#' lint( +#' text = code_lines, +#' linters = fixed_regex_linter() +#' ) +#' +#' lint( +#' text = 'grepl("a*b", x, fixed = TRUE)', +#' linters = fixed_regex_linter() +#' ) +#' +#' lint( +#' text = 'stringr::str_subset(x, stringr::fixed("$"))', +#' linters = fixed_regex_linter() +#' ) +#' +#' lint( +#' text = 'grepl("Munich", address, fixed = TRUE)', +#' linters = fixed_regex_linter() +#' ) +#' #' @evalRd rd_tags("fixed_regex_linter") #' @seealso [linters] for a complete list of linters available in lintr. #' @export diff --git a/R/is_numeric_linter.R b/R/is_numeric_linter.R index dd79cce9e..520030fc5 100644 --- a/R/is_numeric_linter.R +++ b/R/is_numeric_linter.R @@ -5,28 +5,28 @@ #' #' NB: This linter plays well with [class_equals_linter()], which can help #' avoid further `is.numeric()` equivalents like -#' any(class(x) == c("numeric", "integer"))`. +#' `any(class(x) == c("numeric", "integer"))`. #' #' @examples #' # will produce lints #' lint( -#' text = "is.numeric(y) || is.integer(y)", +#' text = 'is.numeric(y) || is.integer(y)', #' linters = is_numeric_linter() #' ) #' #' lint( -#' text = "class(z) %in% c('numeric', 'integer')", +#' text = 'class(z) %in% c("numeric", "integer")', #' linters = is_numeric_linter() #' ) #' #' # okay #' lint( -#' text = "is.numeric(y) || is.factor(y)", +#' text = 'is.numeric(y) || is.factor(y)', #' linters = is_numeric_linter() #' ) #' #' lint( -#' text = "class(z) %in% c('numeric', 'integer', 'factor')", +#' text = 'class(z) %in% c("numeric", "integer", "factor")', #' linters = is_numeric_linter() #' ) #' diff --git a/R/missing_argument_linter.R b/R/missing_argument_linter.R index 693d9e4c6..40bbb480d 100644 --- a/R/missing_argument_linter.R +++ b/R/missing_argument_linter.R @@ -8,23 +8,23 @@ #' @examples #' # will produce lints #' lint( -#' text = "tibble(x = 'a', )", +#' text = 'tibble(x = "a", )', #' linters = missing_argument_linter() #' ) #' #' # okay #' lint( -#' text = "tibble(x = 'a')", +#' text = 'tibble(x = "a")', #' linters = missing_argument_linter() #' ) #' #' lint( -#' text = "tibble(x = 'a', )", +#' text = 'tibble(x = "a", )', #' linters = missing_argument_linter(except = "tibble") #' ) #' #' lint( -#' text = "tibble(x = 'a', )", +#' text = 'tibble(x = "a", )', #' linters = missing_argument_linter(allow_trailing = TRUE) #' ) #' diff --git a/R/nested_ifelse_linter.R b/R/nested_ifelse_linter.R index 094544733..7f6c0c02a 100644 --- a/R/nested_ifelse_linter.R +++ b/R/nested_ifelse_linter.R @@ -16,18 +16,18 @@ #' @examples #' # will produce lints #' lint( -#' text = "ifelse(x == 'a', 1L, ifelse(x == 'b', 2L, 3L))", +#' text = 'ifelse(x == "a", 1L, ifelse(x == "b", 2L, 3L))', #' linters = nested_ifelse_linter() #' ) #' #' # okay #' lint( -#' text = "dplyr::case_when(x == 'a' ~ 1L, x == 'b' ~ 2L, TRUE ~ 3L)", +#' text = 'dplyr::case_when(x == "a" ~ 1L, x == "b" ~ 2L, TRUE ~ 3L)', #' linters = nested_ifelse_linter() #' ) #' #' lint( -#' text = "data.table::fcase(x == 'a', 1L, x == 'b', 2L, default = 3L)", +#' text = 'data.table::fcase(x == "a", 1L, x == "b", 2L, default = 3L)', #' linters = nested_ifelse_linter() #' ) #' diff --git a/R/package_hooks_linter.R b/R/package_hooks_linter.R index ea7ab6949..e6e4ddecd 100644 --- a/R/package_hooks_linter.R +++ b/R/package_hooks_linter.R @@ -35,7 +35,7 @@ #' ) #' #' lint( -#' text = ".onAttach <- function(lib, pkg) { loadNamespace('foo') }", +#' text = '.onAttach <- function(lib, pkg) { loadNamespace("foo") }', #' linters = package_hooks_linter() #' ) #' diff --git a/R/path_linters.R b/R/path_linters.R index c2be41dbf..4916976ef 100644 --- a/R/path_linters.R +++ b/R/path_linters.R @@ -1,9 +1,13 @@ control_char_regex <- rex(one_of(intToUtf8(seq.int(0L, 31L), multiple = TRUE))) # control chars (non-printing) -win32_bad_punct_regex <- rex(one_of("*", "?", "\"", "<", ">", "|", ":", "/", "\\")) -win32_good_punct_regex <- rex(one_of("!", "#", "$", "%", "&", "'", "(", ")", "+", ",", "-", ".", ";", - "=", "@", "[", "]", "^", "_", "`", "{", "}", "~")) +win32_bad_punct_regex <- rex(one_of( + "*", "?", "\"", "<", ">", "|", ":", "/", "\\" +)) +win32_good_punct_regex <- rex(one_of( + "!", "#", "$", "%", "&", "'", "(", ")", "+", ",", "-", ".", ";", + "=", "@", "[", "]", "^", "_", "`", "{", "}", "~" +)) # win32BadPunct + win32AllowedPunct = [:punct:] unsafe_char_regex <- rex(or(control_char_regex, win32_bad_punct_regex)) @@ -25,6 +29,7 @@ root_regex <- list( root_path_regex <- rex(start, or(root_regex), end) +# styler: off absolute_path_regex <- rex(or( root_regex[["unix"]] %if_next_isnt% one_of(space, "/"), root_regex[["tilde"]] %if_next_isnt% one_of(space, quote), @@ -34,12 +39,13 @@ absolute_path_regex <- rex(or( relative_path_regex <- rex( start %if_next_isnt% or(quote, absolute_path_regex, protocol_regex), - or( # not an absolute path or protocol, and then + or( # not an absolute path or protocol, and then group(one_or_more(safe_char_regex), or("/", "\\")), # (back)slash-separated paths - group(dot, maybe(dot), end) # or single or double dot - ) # (paths without slash or dots left out) + group(dot, maybe(dot), end) # or single or double dot + ) # (paths without slash or dots left out) %if_next_isnt% quote ) +# styler: on path_regex <- rex(or(absolute_path_regex, relative_path_regex)) @@ -70,7 +76,7 @@ is_valid_path <- function(paths, lax = FALSE) { return(TRUE) } if (length(dirs) > 0L && is_root_path(dirs[[1L]])) { - dirs <- tail(dirs, -1L) # remove root element ("/", "C:", or "\\") + dirs <- tail(dirs, -1L) # remove root element ("/", "C:", or "\\") } !any(re_matches(dirs, unsafe_char_regex)) }, @@ -169,18 +175,19 @@ path_linter_factory <- function(path_function, message, linter, name = linter_au #' #' # will produce lints #' lint( -#' text = "R'--[/blah/file.txt]--'", +#' text = 'R"--[/blah/file.txt]--"', #' linters = absolute_path_linter() #' ) #' #' # okay #' lint( -#' text = "R'(./blah)'", +#' text = 'R"(./blah)"', #' linters = absolute_path_linter() #' ) #' #' @evalRd rd_tags("absolute_path_linter") -#' @seealso [linters] for a complete list of linters available in lintr. +#' @seealso [linters] for a complete list of linters available in lintr. \cr +#' [nonportable_path_linter()] #' @export absolute_path_linter <- function(lax = TRUE) { path_linter_factory( @@ -197,7 +204,8 @@ absolute_path_linter <- function(lax = TRUE) { #' #' @inheritParams absolute_path_linter #' @evalRd rd_tags("nonportable_path_linter") -#' @seealso [linters] for a complete list of linters available in lintr. +#' @seealso [linters] for a complete list of linters available in lintr. \cr +#' [absolute_path_linter()] #' @export nonportable_path_linter <- function(lax = TRUE) { path_linter_factory( diff --git a/R/sprintf_linter.R b/R/sprintf_linter.R index 959ce8c5e..551e8fb05 100644 --- a/R/sprintf_linter.R +++ b/R/sprintf_linter.R @@ -8,18 +8,18 @@ #' @examples #' # will produce lints #' lint( -#' text = "sprintf('hello %s %s %d', x, y)", +#' text = 'sprintf("hello %s %s %d", x, y)', #' linters = sprintf_linter() #' ) #' #' # okay #' lint( -#' text = "sprintf('hello %s %s %d', x, y, z)", +#' text = 'sprintf("hello %s %s %d", x, y, z)', #' linters = sprintf_linter() #' ) #' #' lint( -#' text = "sprintf('hello %s %s %d', x, y, ...)", +#' text = 'sprintf("hello %s %s %d", x, y, ...)', #' linters = sprintf_linter() #' ) #' diff --git a/man/absolute_path_linter.Rd b/man/absolute_path_linter.Rd index 5f5815f7d..35ece4aa0 100644 --- a/man/absolute_path_linter.Rd +++ b/man/absolute_path_linter.Rd @@ -23,19 +23,20 @@ Check that no absolute paths are used (e.g. "/var", "C:\\System", "~/docs"). # will produce lints lint( - text = "R'--[/blah/file.txt]--'", + text = 'R"--[/blah/file.txt]--"', linters = absolute_path_linter() ) # okay lint( - text = "R'(./blah)'", + text = 'R"(./blah)"', linters = absolute_path_linter() ) \dontshow{\}) # examplesIf} } \seealso{ -\link{linters} for a complete list of linters available in lintr. +\link{linters} for a complete list of linters available in lintr. \cr +\code{\link[=nonportable_path_linter]{nonportable_path_linter()}} } \section{Tags}{ \link[=best_practices_linters]{best_practices}, \link[=configurable_linters]{configurable}, \link[=robustness_linters]{robustness} diff --git a/man/assignment_linter.Rd b/man/assignment_linter.Rd index 80aa9c1b3..0244ea3d4 100644 --- a/man/assignment_linter.Rd +++ b/man/assignment_linter.Rd @@ -28,8 +28,10 @@ lint( linters = assignment_linter() ) +code_lines <- "1 -> x\n2 ->> y" +writeLines(code_lines) lint( - text = "1 -> x; 2 ->> y", + text = code_lines, linters = assignment_linter() ) @@ -39,14 +41,18 @@ lint( linters = assignment_linter() ) +code_lines <- "x <- 1\ny <<- 2" +writeLines(code_lines) lint( - text = "x <- 1; y <<- 2", + text = code_lines, linters = assignment_linter() ) # customizing using arguments +code_lines <- "1 -> x\n2 ->> y" +writeLines(code_lines) lint( - text = "1 -> x; 2 ->> y", + text = code_lines, linters = assignment_linter(allow_right_assign = TRUE) ) diff --git a/man/class_equals_linter.Rd b/man/class_equals_linter.Rd index 790a4ae41..51dfd5488 100644 --- a/man/class_equals_linter.Rd +++ b/man/class_equals_linter.Rd @@ -18,23 +18,23 @@ Similar reasoning applies for \code{class(x) \%in\% "character"}. \examples{ # will produce lints lint( - text = "is_lm <- class(x) == 'lm'", + text = 'is_lm <- class(x) == "lm"', linters = class_equals_linter() ) lint( - text = "if ('lm' \%in\% class(x)) is_lm <- TRUE", + text = 'if ("lm" \%in\% class(x)) is_lm <- TRUE', linters = class_equals_linter() ) # okay lint( - text = "is_lm <- inherits(x, 'lm')", + text = 'is_lm <- inherits(x, "lm")', linters = class_equals_linter() ) lint( - text = "if (inherits(x, 'lm')) is_lm <- TRUE", + text = 'if (inherits(x, "lm")) is_lm <- TRUE', linters = class_equals_linter() ) diff --git a/man/condition_message_linter.Rd b/man/condition_message_linter.Rd index 1bea69cf4..dfe5fb405 100644 --- a/man/condition_message_linter.Rd +++ b/man/condition_message_linter.Rd @@ -22,28 +22,28 @@ and \code{\link[=packageStartupMessage]{packageStartupMessage()}}. \examples{ # will produce lints lint( - text = "stop(paste('a string', 'another'))", + text = 'stop(paste("a string", "another")"', linters = condition_message_linter() ) lint( - text = "warning(paste0('a string', ' another'))", + text = 'warning(paste0("a string", " another")', linters = condition_message_linter() ) # okay lint( - text = "stop('a string', ' another')", + text = 'stop("a string", " another")', linters = condition_message_linter() ) lint( - text = "warning('a string', ' another')", + text = 'warning("a string", " another")', linters = condition_message_linter() ) lint( - text = "warning(paste('a string', 'another', sep = '-'))", + text = 'warning(paste("a string", "another", sep = "-"))', linters = condition_message_linter() ) diff --git a/man/conjunct_test_linter.Rd b/man/conjunct_test_linter.Rd index 91803f318..5d42de513 100644 --- a/man/conjunct_test_linter.Rd +++ b/man/conjunct_test_linter.Rd @@ -43,7 +43,7 @@ lint( ) lint( - text = "stopifnot('x must be a logical scalar' = length(x) == 1 && is.logical(x) && !is.na(x))", + text = 'stopifnot("x must be a logical scalar" = length(x) == 1 && is.logical(x) && !is.na(x))', linters = conjunct_test_linter(allow_named_stopifnot = TRUE) ) diff --git a/man/expect_named_linter.Rd b/man/expect_named_linter.Rd index 91dc58d05..1305af5d4 100644 --- a/man/expect_named_linter.Rd +++ b/man/expect_named_linter.Rd @@ -14,23 +14,23 @@ but it is better to use the tailored function instead. \examples{ # will produce lints lint( - text = "expect_equal(names(x), 'a')", + text = 'expect_equal(names(x), "a")', linters = expect_named_linter() ) # okay lint( - text = "expect_named(x, 'a')", + text = 'expect_named(x, "a")', linters = expect_named_linter() ) lint( - text = "expect_equal(colnames(x), 'a')", + text = 'expect_equal(colnames(x), "a")', linters = expect_named_linter() ) lint( - text = "expect_equal(dimnames(x), 'a')", + text = 'expect_equal(dimnames(x), "a")', linters = expect_named_linter() ) diff --git a/man/expect_s3_class_linter.Rd b/man/expect_s3_class_linter.Rd index a2a4ef1c1..682825017 100644 --- a/man/expect_s3_class_linter.Rd +++ b/man/expect_s3_class_linter.Rd @@ -15,23 +15,23 @@ but it is better to use the tailored function instead. \examples{ # will produce lints lint( - text = "expect_equal(class(x), 'data.frame')", + text = 'expect_equal(class(x), "data.frame")', linters = expect_s3_class_linter() ) lint( - text = "expect_equal(class(x), 'numeric')", + text = 'expect_equal(class(x), "numeric")', linters = expect_s3_class_linter() ) # okay lint( - text = "expect_s3_class(x, 'data.frame')", + text = 'expect_s3_class(x, "data.frame")', linters = expect_s3_class_linter() ) lint( - text = "expect_type(x, 'double')", + text = 'expect_type(x, "double")', linters = expect_s3_class_linter() ) diff --git a/man/expect_s4_class_linter.Rd b/man/expect_s4_class_linter.Rd index e1c1ae132..0d9fa0789 100644 --- a/man/expect_s4_class_linter.Rd +++ b/man/expect_s4_class_linter.Rd @@ -14,13 +14,13 @@ but it is better to use the tailored function instead. \examples{ # will produce lints lint( - text = "expect_true(is(x, 'Matrix'))", + text = 'expect_true(is(x, "Matrix"))', linters = expect_s4_class_linter() ) # okay lint( - text = "expect_s4_class(x, 'Matrix')", + text = 'expect_s4_class(x, "Matrix")', linters = expect_s4_class_linter() ) diff --git a/man/expect_type_linter.Rd b/man/expect_type_linter.Rd index 7628864ef..63b4ba873 100644 --- a/man/expect_type_linter.Rd +++ b/man/expect_type_linter.Rd @@ -15,18 +15,18 @@ but it is better to use the tailored function instead. \examples{ # will produce lints lint( - text = "expect_equal(typeof(x), 'double')", + text = 'expect_equal(typeof(x), "double")', linters = expect_type_linter() ) lint( - text = "expect_identical(typeof(x), 'double')", + text = 'expect_identical(typeof(x), "double")', linters = expect_type_linter() ) # okay lint( - text = "expect_type(x, 'double')", + text = 'expect_type(x, "double")', linters = expect_type_linter() ) diff --git a/man/extraction_operator_linter.Rd b/man/extraction_operator_linter.Rd index c8853318a..307cd6f72 100644 --- a/man/extraction_operator_linter.Rd +++ b/man/extraction_operator_linter.Rd @@ -33,18 +33,18 @@ For lists, however, the reverse is true. \examples{ # will produce lints lint( - text = "iris['Species']", + text = 'iris["Species"]', linters = extraction_operator_linter() ) lint( - text = "iris$Species", + text = 'iris$Species', linters = extraction_operator_linter() ) # okay lint( - text = "iris[['Species']]", + text = 'iris[["Species"]]', linters = extraction_operator_linter() ) diff --git a/man/fixed_regex_linter.Rd b/man/fixed_regex_linter.Rd index c1394f461..044d9d179 100644 --- a/man/fixed_regex_linter.Rd +++ b/man/fixed_regex_linter.Rd @@ -17,6 +17,56 @@ NB: this linter is likely not able to distinguish every possible case when a fixed regular expression is preferable, rather it seeks to identify likely cases. It should \emph{never} report false positives, however; please report false positives as an error. +} +\examples{ +# will produce lints +code_lines <- 'gsub("\\\\\\\\.", "", x)' +writeLines(code_lines) +lint( + text = code_lines, + linters = fixed_regex_linter() +) + +lint( + text = 'grepl("a[*]b", x)', + linters = fixed_regex_linter() +) + +code_lines <- 'stringr::str_subset(x, "\\\\\\\\$")' +writeLines(code_lines) +lint( + text = code_lines, + linters = fixed_regex_linter() +) + +lint( + text = 'grepl("Munich", address)', + linters = fixed_regex_linter() +) + +# okay +code_lines <- 'gsub("\\\\\\\\.", "", x, fixed = TRUE)' +writeLines(code_lines) +lint( + text = code_lines, + linters = fixed_regex_linter() +) + +lint( + text = 'grepl("a*b", x, fixed = TRUE)', + linters = fixed_regex_linter() +) + +lint( + text = 'stringr::str_subset(x, stringr::fixed("$"))', + linters = fixed_regex_linter() +) + +lint( + text = 'grepl("Munich", address, fixed = TRUE)', + linters = fixed_regex_linter() +) + } \seealso{ \link{linters} for a complete list of linters available in lintr. diff --git a/man/is_numeric_linter.Rd b/man/is_numeric_linter.Rd index 7b8cf7bd5..ffb5cf05c 100644 --- a/man/is_numeric_linter.Rd +++ b/man/is_numeric_linter.Rd @@ -13,28 +13,28 @@ testing \code{is.numeric(x) || is.integer(x)} is thus redundant. \details{ NB: This linter plays well with \code{\link[=class_equals_linter]{class_equals_linter()}}, which can help avoid further \code{is.numeric()} equivalents like -any(class(x) == c("numeric", "integer"))`. +\code{any(class(x) == c("numeric", "integer"))}. } \examples{ # will produce lints lint( - text = "is.numeric(y) || is.integer(y)", + text = 'is.numeric(y) || is.integer(y)', linters = is_numeric_linter() ) lint( - text = "class(z) \%in\% c('numeric', 'integer')", + text = 'class(z) \%in\% c("numeric", "integer")', linters = is_numeric_linter() ) # okay lint( - text = "is.numeric(y) || is.factor(y)", + text = 'is.numeric(y) || is.factor(y)', linters = is_numeric_linter() ) lint( - text = "class(z) \%in\% c('numeric', 'integer', 'factor')", + text = 'class(z) \%in\% c("numeric", "integer", "factor")', linters = is_numeric_linter() ) diff --git a/man/missing_argument_linter.Rd b/man/missing_argument_linter.Rd index 4eab6fbc2..b607abc9f 100644 --- a/man/missing_argument_linter.Rd +++ b/man/missing_argument_linter.Rd @@ -17,23 +17,23 @@ Check for missing arguments in function calls (e.g. \code{stats::median(1:10, )} \examples{ # will produce lints lint( - text = "tibble(x = 'a', )", + text = 'tibble(x = "a", )', linters = missing_argument_linter() ) # okay lint( - text = "tibble(x = 'a')", + text = 'tibble(x = "a")', linters = missing_argument_linter() ) lint( - text = "tibble(x = 'a', )", + text = 'tibble(x = "a", )', linters = missing_argument_linter(except = "tibble") ) lint( - text = "tibble(x = 'a', )", + text = 'tibble(x = "a", )', linters = missing_argument_linter(allow_trailing = TRUE) ) diff --git a/man/nested_ifelse_linter.Rd b/man/nested_ifelse_linter.Rd index ae0301fff..3e5259f11 100644 --- a/man/nested_ifelse_linter.Rd +++ b/man/nested_ifelse_linter.Rd @@ -26,18 +26,18 @@ and outputs and merge this to the input). \examples{ # will produce lints lint( - text = "ifelse(x == 'a', 1L, ifelse(x == 'b', 2L, 3L))", + text = 'ifelse(x == "a", 1L, ifelse(x == "b", 2L, 3L))', linters = nested_ifelse_linter() ) # okay lint( - text = "dplyr::case_when(x == 'a' ~ 1L, x == 'b' ~ 2L, TRUE ~ 3L)", + text = 'dplyr::case_when(x == "a" ~ 1L, x == "b" ~ 2L, TRUE ~ 3L)', linters = nested_ifelse_linter() ) lint( - text = "data.table::fcase(x == 'a', 1L, x == 'b', 2L, default = 3L)", + text = 'data.table::fcase(x == "a", 1L, x == "b", 2L, default = 3L)', linters = nested_ifelse_linter() ) diff --git a/man/nonportable_path_linter.Rd b/man/nonportable_path_linter.Rd index c895fe891..c384fccb7 100644 --- a/man/nonportable_path_linter.Rd +++ b/man/nonportable_path_linter.Rd @@ -18,7 +18,8 @@ If \code{TRUE}, only lint path strings, which Check that \code{\link[=file.path]{file.path()}} is used to construct safe and portable paths. } \seealso{ -\link{linters} for a complete list of linters available in lintr. +\link{linters} for a complete list of linters available in lintr. \cr +\code{\link[=absolute_path_linter]{absolute_path_linter()}} } \section{Tags}{ \link[=best_practices_linters]{best_practices}, \link[=configurable_linters]{configurable}, \link[=robustness_linters]{robustness} diff --git a/man/package_hooks_linter.Rd b/man/package_hooks_linter.Rd index 03eea50b3..a1eafbda8 100644 --- a/man/package_hooks_linter.Rd +++ b/man/package_hooks_linter.Rd @@ -45,7 +45,7 @@ lint( ) lint( - text = ".onAttach <- function(lib, pkg) { loadNamespace('foo') }", + text = '.onAttach <- function(lib, pkg) { loadNamespace("foo") }', linters = package_hooks_linter() ) diff --git a/man/sprintf_linter.Rd b/man/sprintf_linter.Rd index a551c1a8d..97a372073 100644 --- a/man/sprintf_linter.Rd +++ b/man/sprintf_linter.Rd @@ -16,18 +16,18 @@ Check for an inconsistent number of arguments or arguments with incompatible typ \examples{ # will produce lints lint( - text = "sprintf('hello \%s \%s \%d', x, y)", + text = 'sprintf("hello \%s \%s \%d", x, y)', linters = sprintf_linter() ) # okay lint( - text = "sprintf('hello \%s \%s \%d', x, y, z)", + text = 'sprintf("hello \%s \%s \%d", x, y, z)', linters = sprintf_linter() ) lint( - text = "sprintf('hello \%s \%s \%d', x, y, ...)", + text = 'sprintf("hello \%s \%s \%d", x, y, ...)', linters = sprintf_linter() )