Skip to content

Commit

Permalink
Add examples to documentation: Part-9 (cleanup) (#1688)
Browse files Browse the repository at this point in the history
* Examples for `fixed_regex_linter()`

* cleanup

* more cleanup

* review comments

* another example for fixed regex
  • Loading branch information
IndrajeetPatil authored Oct 12, 2022
1 parent 2160d87 commit 5ea4ca8
Show file tree
Hide file tree
Showing 34 changed files with 228 additions and 105 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions R/assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand All @@ -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)
#' )
#'
Expand Down
8 changes: 4 additions & 4 deletions R/class_equals_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
10 changes: 5 additions & 5 deletions R/condition_message_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
2 changes: 1 addition & 1 deletion R/conjunct_test_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#' )
#'
Expand Down
8 changes: 4 additions & 4 deletions R/expect_named_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
8 changes: 4 additions & 4 deletions R/expect_s3_class_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
4 changes: 2 additions & 2 deletions R/expect_s4_class_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
6 changes: 3 additions & 3 deletions R/expect_type_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
6 changes: 3 additions & 3 deletions R/extraction_operator_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
49 changes: 49 additions & 0 deletions R/fixed_regex_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions R/is_numeric_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
8 changes: 4 additions & 4 deletions R/missing_argument_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#' )
#'
Expand Down
6 changes: 3 additions & 3 deletions R/nested_ifelse_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
#' )
#'
Expand Down
2 changes: 1 addition & 1 deletion R/package_hooks_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#' )
#'
#' lint(
#' text = ".onAttach <- function(lib, pkg) { loadNamespace('foo') }",
#' text = '.onAttach <- function(lib, pkg) { loadNamespace("foo") }',
#' linters = package_hooks_linter()
#' )
#'
Expand Down
Loading

0 comments on commit 5ea4ca8

Please sign in to comment.