-
Notifications
You must be signed in to change notification settings - Fork 195
Don't lint expression(paste(., sep = "")) in paste_linter #2955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
f87bb69
c592d53
3639105
bd791e7
f4c9d88
66bfb7f
e041861
095a671
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| #' The following issues are linted by default by this linter | ||
| #' (see arguments for which can be de-activated optionally): | ||
| #' | ||
| #' 1. Block usage of [base::paste()] with `sep = ""`. [base::paste0()] is a faster, more concise alternative. | ||
| #' 1. Block usage of [base::paste()] with `sep = ""`. [base::paste0()] is a faster, more concise alternative; | ||
| #' this is valid unless `paste` occurs inside [base::expression], which according to [grDevices::plotmath] | ||
| #' does not support the `sep` argument. | ||
| #' 2. Block usage of `paste()` or `paste0()` with `collapse = ", "`. [toString()] is a direct | ||
| #' wrapper for this, and alternatives like [glue::glue_collapse()] might give better messages for humans. | ||
| #' 3. Block usage of `paste0()` that supplies `sep=` -- this is not a formal argument to `paste0`, and | ||
|
|
@@ -75,6 +77,11 @@ | |
| #' ) | ||
| #' | ||
| #' lint( | ||
| #' text = 'expression(paste("a", "b", sep = ""))', | ||
| #' linters = paste_linter() | ||
| #' ) | ||
| #' | ||
| #' lint( | ||
| #' text = 'toString(c("a", "b"))', | ||
| #' linters = paste_linter() | ||
| #' ) | ||
|
|
@@ -121,7 +128,14 @@ paste_linter <- function(allow_empty_sep = FALSE, | |
| following-sibling::SYMBOL_SUB[text() = 'sep' and following-sibling::expr[1][STR_CONST]] | ||
| /parent::expr | ||
| " | ||
|
|
||
| expression_paste_sep_xpath <- " | ||
| following-sibling::SYMBOL_SUB[ | ||
| text() = 'sep' | ||
| and following-sibling::expr[1][STR_CONST] | ||
| and parent::expr/preceding-sibling::expr/SYMBOL_FUNCTION_CALL[text() = 'expression'] | ||
| ] | ||
| /parent::expr | ||
| " | ||
| to_string_xpath <- " | ||
| parent::expr[ | ||
| count(expr) = 3 | ||
|
|
@@ -184,7 +198,16 @@ paste_linter <- function(allow_empty_sep = FALSE, | |
| paste_sep_value <- get_r_string(paste_sep_expr, xpath = "./SYMBOL_SUB[text() = 'sep']/following-sibling::expr[1]") | ||
| } | ||
|
|
||
| if (!allow_empty_sep) { | ||
| ## check if we are inside an expression() | ||
| expression_paste_sep_expr <- xml_find_all(paste_calls, expression_paste_sep_xpath) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks off -- what happens for something like this? c(
expression(paste(x, y, sep="")),
paste(x, y, sep=""),
expression(paste(a, b, sep="")),
paste(a, b, sep=""),
expression(c(paste(d, e, sep=""), paste(f, g), paste(h, i, sep="")))
)(generally |
||
| if (length(expression_paste_sep_expr) > 0L) { | ||
| optional_lints <- c(optional_lints, xml_nodes_to_lints( | ||
| expression_paste_sep_expr[1L], | ||
| source_expression = source_expression, | ||
| lint_message = "inside expression(...), paste does not accept a 'sep' argument.", | ||
| type = "warning" | ||
| )) | ||
| } else if (!allow_empty_sep) { | ||
| optional_lints <- c(optional_lints, xml_nodes_to_lints( | ||
| paste_sep_expr[!nzchar(paste_sep_value)], | ||
| source_expression = source_expression, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be in the "will produce lints" section; our approach generally is to "pair" linting expressions with their "clean" equivalents across the "top" and bottom (under "okay") parts of the documentation.
(it's a bit messy for this file in particular because
paste_linter()does a lot)