From 6283afc8592b64800cf258e9dca574ffd658bc92 Mon Sep 17 00:00:00 2001 From: mpadge Date: Wed, 25 Sep 2024 14:31:08 +0200 Subject: [PATCH 1/2] rm 'dangerous_fn_linter' for #173 --- R/my_linters.R | 63 -------------------------------------------------- R/prep_lintr.R | 18 +++++++++++---- 2 files changed, 14 insertions(+), 67 deletions(-) diff --git a/R/my_linters.R b/R/my_linters.R index 75e39fa..a82c379 100644 --- a/R/my_linters.R +++ b/R/my_linters.R @@ -105,66 +105,3 @@ seq_linter <- function() lintr::Linter(function(source_file) { } ) }) - -#' @importFrom lintr Lint - -dangerous_functions_linter <- function(source_file, funcs, type, - msg, linter) lintr::Linter(function(source_file) { - - bad <- which( - source_file$parsed_content$token == "SYMBOL_FUNCTION_CALL" & - source_file$parsed_content$text %in% funcs - ) - - lapply( - bad, - function(line) { - parsed <- source_file$parsed_content[line, ] - Lint( - filename = source_file$filename, - line_number = parsed$line1, - column_number = parsed$col1, - type = type, - message = msg, - line = source_file$lines[as.character(parsed$line1)], - ranges = list(c(parsed$col1, parsed$col2)) - ) - } - ) -}) - -attach_detach_linter <- function(source_file) { - dangerous_functions_linter( - source_file, - funcs = c("attach", "detach"), - type = "warning", - msg = "Avoid attach/detach, it is easy to create errors with it" - ) -} - -setwd_linter <- function(source_file) { - dangerous_functions_linter( - source_file, - funcs = "setwd", - type = "warning", - msg = "Avoid changing the working directory, or restore it in on.exit" - ) -} - -sapply_linter <- function(source_file) { - dangerous_functions_linter( - source_file, - funcs = "sapply", - type = "warning", - msg = "Avoid using sapply, consider vapply instead, that's type safe" - ) -} - -library_require_linter <- function(source_file) { - dangerous_functions_linter( - source_file, - funcs = c("library", "require"), - type = "warning", - msg = "Avoid library() and require() calls in packages" - ) -} diff --git a/R/prep_lintr.R b/R/prep_lintr.R index 3a9ded6..407bda3 100644 --- a/R/prep_lintr.R +++ b/R/prep_lintr.R @@ -5,10 +5,20 @@ linters_to_lint <- list( assignment_linter = lintr::assignment_linter(), line_length_linter = lintr::line_length_linter(80), trailing_semicolon_linter = trailing_semicolon_linter(), - attach_detach_linter = attach_detach_linter(), - setwd_linter = setwd_linter(), - sapply_linter = sapply_linter(), - library_require_linter = library_require_linter(), + attach_detach_linter = lintr::undesirable_function_linter(fun = c( + "attach" = "Avoid attach, it is easy to create errors with it", + "detach" = "Avoid detach, it is easy to create errors with it" + )), + setwd_linter = lintr::undesirable_function_linter(fun = c( + "setwd" = "Avoid changing the working directory, or restore it in on.exit" + )), + sapply_linter = lintr::undesirable_function_linter(fun = c( + "sapply" = "Avoid using sapply, consider vapply instead, that's type safe" + )), + library_require_linter = lintr::undesirable_function_linter(fun = c( + "library" = "Avoid library() calls in packages", + "require" = "Avoid require() calls in packages" + )), seq_linter = seq_linter() ) From 9083c9f8657d1f318e2db18d137ccbcee0c696d8 Mon Sep 17 00:00:00 2001 From: mpadge Date: Thu, 26 Sep 2024 12:07:58 +0200 Subject: [PATCH 2/2] use lintr default undesirable_function defs for #173 --- R/prep_lintr.R | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/R/prep_lintr.R b/R/prep_lintr.R index 407bda3..c9c2c05 100644 --- a/R/prep_lintr.R +++ b/R/prep_lintr.R @@ -5,20 +5,18 @@ linters_to_lint <- list( assignment_linter = lintr::assignment_linter(), line_length_linter = lintr::line_length_linter(80), trailing_semicolon_linter = trailing_semicolon_linter(), - attach_detach_linter = lintr::undesirable_function_linter(fun = c( - "attach" = "Avoid attach, it is easy to create errors with it", - "detach" = "Avoid detach, it is easy to create errors with it" - )), - setwd_linter = lintr::undesirable_function_linter(fun = c( - "setwd" = "Avoid changing the working directory, or restore it in on.exit" - )), - sapply_linter = lintr::undesirable_function_linter(fun = c( - "sapply" = "Avoid using sapply, consider vapply instead, that's type safe" - )), - library_require_linter = lintr::undesirable_function_linter(fun = c( - "library" = "Avoid library() calls in packages", - "require" = "Avoid require() calls in packages" - )), + attach_detach_linter = lintr::undesirable_function_linter( + fun = lintr::default_undesirable_functions[c("attach", "detach")] + ), + setwd_linter = lintr::undesirable_function_linter( + fun = lintr::default_undesirable_functions["setwd"] + ), + sapply_linter = lintr::undesirable_function_linter( + fun = lintr::default_undesirable_functions["sapply"] + ), + library_require_linter = lintr::undesirable_function_linter( + fun = lintr::default_undesirable_functions[c("library", "require")] + ), seq_linter = seq_linter() )