Skip to content

Commit 38f0a3b

Browse files
Get rid of lints with performance implications (#1000)
* Get rid of lints with performance implications Some alternatives are more efficient or avoid unnecessary function calls. And also cover some more integer literals. * pre-commit * minor * Address comment Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent dbf8f29 commit 38f0a3b

21 files changed

+47
-53
lines changed

R/addins.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ style_active_file <- function() {
7171
abort("Can only style .R, .Rmd and .Rnw files.")
7272
}
7373
rstudioapi::modifyRange(
74-
c(1, 1, length(context$contents) + 1, 1),
74+
c(1L, 1L, length(context$contents) + 1L, 1L),
7575
paste0(ensure_last_n_empty(out), collapse = "\n"),
7676
id = context$id
7777
)
@@ -134,7 +134,7 @@ style_selection <- function() {
134134
context$selection[[1]]$range,
135135
paste0(c(
136136
out,
137-
if (context$selection[[1]]$range$end[2] == 1) ""
137+
if (context$selection[[1]]$range$end[2] == 1L) ""
138138
), collapse = "\n"),
139139
id = context$id
140140
)

R/compat-dplyr.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ left_join <- function(x, y, by) {
6565
res <- new_tibble(res, nrow = nrow(res))
6666
# dplyr::left_join set unknown list columns to NULL, merge sets them
6767
# to NA
68-
if (exists("child", res) && any(is.na(res$child))) {
68+
if (exists("child", res) && anyNA(res$child)) {
6969
res$child[is.na(res$child)] <- list(NULL)
7070
}
7171
res

R/detect-alignment.R

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ token_is_on_aligned_line <- function(pd_flat) {
104104
# now, pd only contains arguments separated by values, ideal for iterating
105105
# over columns.
106106
n_cols <- map_int(pd_by_line, ~ sum(.x$token == "','"))
107-
previous_line <- 0
108-
current_col <- 0
109-
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1, 2)
110-
for (column in seq2(1, max(n_cols))) {
107+
previous_line <- current_col <- 0L
108+
start_eval <- ifelse(alignment_col1_all_named(pd_by_line), 1L, 2L)
109+
for (column in seq2(1L, max(n_cols))) {
111110
by_line <- alignment_serialize_column(pd_by_line, column) %>%
112111
compact() %>%
113112
unlist() %>%
@@ -120,7 +119,7 @@ token_is_on_aligned_line <- function(pd_flat) {
120119
current_col <- nchar(by_line) - as.integer(column > 1)
121120
# Problem `by_line` counting from comma before column 3, previous_line
122121
# counting 1 space before ~
123-
if (column > 1) {
122+
if (column > 1L) {
124123
previous_line <- previous_line[
125124
intersect(names(previous_line), names(by_line))
126125
]
@@ -129,28 +128,28 @@ token_is_on_aligned_line <- function(pd_flat) {
129128
}
130129

131130
is_aligned <- length(unique(current_col)) == 1L
132-
if (!is_aligned || length(current_col) < 2) {
131+
if (!is_aligned || length(current_col) < 2L) {
133132
# check 2: left aligned after , (comma to next token)
134133
current_col <- "^(,[\\s\\t]*)[^ ]*.*$" %>%
135134
gsub("\\1", by_line, perl = TRUE) %>%
136135
nchar() %>%
137136
magrittr::subtract(1)
138137

139-
if (column > 1) {
138+
if (column > 1L) {
140139
# must add previous columns, as first column might not align
141140
current_col <- previous_line + current_col
142141
}
143-
if (length(current_col) > 1) {
142+
if (length(current_col) > 1L) {
144143
is_aligned <- length(unique(current_col)) == 1L
145144
} else {
146-
is_aligned <- current_col - max_previous_col == 1
145+
is_aligned <- current_col - max_previous_col == 1L
147146
current_col <- max_previous_col + current_col
148147
}
149148

150149
if (is_aligned) {
151150
# if left aligned after ,
152-
start_eval <- 2
153-
previous_line <- nchar(by_line) - 1 + previous_line # comma to comma
151+
start_eval <- 2L
152+
previous_line <- nchar(by_line) - 1L + previous_line # comma to comma
154153
}
155154
} else {
156155
previous_line <- current_col
@@ -162,22 +161,22 @@ token_is_on_aligned_line <- function(pd_flat) {
162161
# match left aligned after =
163162
start_after_eq <- regexpr("= [^ ]", by_line)
164163
names(start_after_eq) <- names(by_line)
165-
start_after_eq <- start_after_eq[start_after_eq > 0]
164+
start_after_eq <- start_after_eq[start_after_eq > 0L]
166165

167166
if (column >= start_eval) {
168167
if (length(start_after_eq) == 0L) {
169168
return(FALSE)
170169
}
171170
# when match via , unsuccessful, matching by = must yield at least one =
172-
if (column == 1) {
171+
if (column == 1L) {
173172
current_col <- start_after_eq
174173
} else {
175174
current_col <- start_after_eq +
176175
previous_line[intersect(names(previous_line), names(start_after_eq))]
177176
}
178177
is_aligned <- all(
179178
length(unique(current_col)) == 1L,
180-
length(start_after_eq) > 1
179+
length(start_after_eq) > 1L
181180
)
182181
if (!is_aligned) {
183182
return(FALSE)

R/expr-is.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ is_subset_expr <- function(pd) {
111111
#' @keywords internal
112112
is_shebang <- function(pd) {
113113
is_first_comment <- pd$pos_id == 1L
114-
is_first_comment[is_first_comment] <- grepl(
115-
"^#!", pd$text[is_first_comment]
116-
)
114+
is_first_comment[is_first_comment] <- startsWith(pd$text[is_first_comment], "#!")
117115
is_first_comment
118116
}
119117

R/io.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ read_utf8 <- function(path) {
8888
} else if (inherits(out, "warning")) {
8989
list(
9090
text = read_utf8_bare(path, warn = FALSE),
91-
missing_EOF_line_break = grepl("incomplete", out$message)
91+
missing_EOF_line_break = grepl("incomplete", out$message, fixed = TRUE)
9292
)
9393
}
9494
}

R/roxygen-examples.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ style_roxygen_example_snippet <- function(code_snippet,
125125
)
126126
}
127127

128-
code_snippet <- ensure_last_n_empty(
129-
code_snippet,
130-
n = ifelse(append_empty, 1L, 0L)
131-
)
128+
code_snippet <- ensure_last_n_empty(code_snippet, n = as.integer(append_empty))
132129

133130
if (!is_cached && cache_is_active) {
134131
cache_write(

R/rules-line-breaks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ remove_line_breaks_in_fun_dec <- function(pd) {
241241
#' @importFrom rlang seq2
242242
add_line_break_after_pipe <- function(pd) {
243243
is_pipe <- pd$token %in% c("SPECIAL-PIPE", "PIPE")
244-
pd$lag_newlines[lag(is_pipe) & pd$lag_newlines > 1] <- 1L
244+
pd$lag_newlines[lag(is_pipe) & pd$lag_newlines > 1L] <- 1L
245245

246-
if (sum(is_pipe & pd$token_after != "COMMENT") > 1 &&
246+
if (sum(is_pipe & pd$token_after != "COMMENT") > 1L &&
247247
!(next_terminal(pd, vars = "token_before")$token_before %in% c("'('", "EQ_SUB", "','"))) {
248248
pd$lag_newlines[lag(is_pipe) & pd$token != "COMMENT"] <- 1L
249249
}

R/rules-spaces.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ style_space_around_tilde <- function(pd_flat, strict) {
113113
} else if (is_asymmetric_tilde_expr(pd_flat)) {
114114
pd_flat <- style_space_around_token(pd_flat,
115115
strict = TRUE, "'~'", level_before = 1L,
116-
level_after = ifelse(nrow(pd_flat$child[[2]]) > 1L, 1L, 0L)
116+
level_after = as.integer(nrow(pd_flat$child[[2]]) > 1L)
117117
)
118118
}
119119
pd_flat
@@ -229,7 +229,7 @@ set_space_between_levels <- function(pd_flat) {
229229
index <- pd_flat$token == "')'" & pd_flat$newlines == 0L
230230
pd_flat$spaces[index] <- 1L
231231
} else if (pd_flat$token[1] == "FOR") {
232-
index <- pd_flat$token == "forcond" & pd_flat$newlines == 0
232+
index <- pd_flat$token == "forcond" & pd_flat$newlines == 0L
233233
pd_flat$spaces[index] <- 1L
234234
}
235235
pd_flat

R/rules-tokens.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2) {
7979
if (length(key_token) > 0L) {
8080
pd <- pd %>%
8181
wrap_multiline_curly(indent_by,
82-
space_after = ifelse(contains_else_expr(pd), 1, 0),
83-
key_token = key_token
82+
key_token = key_token,
83+
space_after = as.integer(contains_else_expr(pd))
8484
)
8585
}
8686
if (is_cond_expr(pd)) {
8787
pd <- pd %>%
88-
wrap_else_multiline_curly(indent_by, space_after = 0)
88+
wrap_else_multiline_curly(indent_by, space_after = 0L)
8989
}
9090
pd
9191
}
@@ -98,13 +98,13 @@ wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2) {
9898
#' the expression to be wrapped (ignoring comments). For if and while loops,
9999
#' this is the closing "')'", for a for-loop it's "forcond".
100100
#' @keywords internal
101-
wrap_multiline_curly <- function(pd, indent_by, space_after = 1L, key_token) {
101+
wrap_multiline_curly <- function(pd, indent_by, key_token, space_after = 1L) {
102102
to_be_wrapped_expr_with_child <- next_non_comment(
103103
pd, which(pd$token == key_token)[1]
104104
)
105105
next_terminal <- next_terminal(pd[to_be_wrapped_expr_with_child, ])$text
106106
requires_braces <- if_for_while_part_requires_braces(pd, key_token) && !any(pd$stylerignore)
107-
if (requires_braces | next_terminal == "return") {
107+
if (requires_braces || next_terminal == "return") {
108108
closing_brace_ind <- which(pd$token == key_token)[1]
109109
pd$spaces[closing_brace_ind] <- 1L
110110

@@ -116,7 +116,7 @@ wrap_multiline_curly <- function(pd, indent_by, space_after = 1L, key_token) {
116116
pd, all_to_be_wrapped_ind, indent_by, space_after
117117
)
118118

119-
if (nrow(pd) > 5) pd$lag_newlines[6] <- 0L
119+
if (nrow(pd) > 5L) pd$lag_newlines[6] <- 0L
120120
}
121121
pd
122122
}
@@ -163,7 +163,7 @@ wrap_subexpr_in_curly <- function(pd,
163163
stretch_out = c(!to_be_wrapped_starts_with_comment, TRUE),
164164
space_after = space_after
165165
)
166-
new_expr$indent <- max(pd$indent[last(ind_to_be_wrapped)] - indent_by, 0)
166+
new_expr$indent <- max(pd$indent[last(ind_to_be_wrapped)] - indent_by, 0L)
167167
new_expr_in_expr <- new_expr %>%
168168
wrap_expr_in_expr() %>%
169169
remove_attributes(c("token_before", "token_after"))

R/set-assert-args.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @keywords internal
88
set_arg_write_tree <- function(write_tree) {
99
if (is.na(write_tree)) {
10-
write_tree <- ifelse(is_installed("data.tree"), TRUE, FALSE)
10+
write_tree <- is_installed("data.tree")
1111
} else if (write_tree) {
1212
assert_data.tree_installation()
1313
}

R/style-guides.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ tidyverse_style <- function(scope = "tokens",
157157
except_token_after = "COMMENT",
158158
# don't modify line break here
159159
except_text_before = c("ifelse", "if_else"),
160-
force_text_before = c("switch") # force line break after first token
160+
force_text_before = "switch" # force line break after first token
161161
)
162162
},
163163
remove_line_break_in_fun_call = purrr::partial(

R/stylerignore.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ env_add_stylerignore <- function(pd_flat) {
2929
map(~ rep(.x[1], length(.x))) %>%
3030
unlist(use.names = FALSE)
3131
pd_flat_temp$lag_newlines <- pd_flat_temp$lag_newlines
32-
pd_flat_temp$lag_spaces <- lag(pd_flat_temp$spaces, default = 0)
32+
pd_flat_temp$lag_spaces <- lag(pd_flat_temp$spaces, default = 0L)
3333
is_terminal_to_ignore <- pd_flat_temp$terminal & pd_flat_temp$stylerignore
3434
env_current$stylerignore <- pd_flat_temp[is_terminal_to_ignore, ]
3535
}

R/testing.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ style_empty <- function(text, base_indention = 0) {
183183

184184
#' @describeIn test_transformer Transformations for indention based on operators
185185
#' @keywords internal
186-
style_op <- function(text, base_indention = 0) {
186+
style_op <- function(text, base_indention = 0L) {
187187
transformers <- list(
188188
# transformer functions
189189
initialize = default_style_guide_attributes,
190190
line_break = NULL,
191-
space = partial(indent_op, indent_by = 2),
191+
space = partial(indent_op, indent_by = 2L),
192192
token = NULL,
193193
# transformer options
194194
use_raw_indention = FALSE,

R/token-create.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ validate_new_pos_ids <- function(new_ids, after) {
145145
#' @keywords internal
146146
wrap_expr_in_curly <- function(pd,
147147
stretch_out = c(FALSE, FALSE),
148-
space_after = 1) {
148+
space_after = 1L) {
149149
if (is_curly_expr(pd)) {
150150
return(pd)
151151
}
@@ -154,8 +154,8 @@ wrap_expr_in_curly <- function(pd,
154154
}
155155

156156
opening <- create_tokens("'{'", "{",
157-
pos_ids = create_pos_ids(pd, 1, after = FALSE),
158-
spaces = 1 - as.integer(stretch_out[1]),
157+
pos_ids = create_pos_ids(pd, 1L, after = FALSE),
158+
spaces = 1L - as.integer(stretch_out[1]),
159159
stylerignore = pd$stylerignore[1],
160160
indents = pd$indent[1]
161161
)

R/ui-styling.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ prettify_any <- function(transformers,
303303
recursive = FALSE
304304
)
305305
} else {
306-
files_other <- c()
306+
files_other <- NULL
307307
}
308308
transform_files(
309309
setdiff(c(files_root, files_other), exclude_files),

R/utils-navigate-nest.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ previous_non_comment <- function(pd, pos) {
5858
next_terminal <- function(pd,
5959
stack = FALSE,
6060
vars = c("pos_id", "token", "text"),
61-
tokens_exclude = c()) {
61+
tokens_exclude = NULL) {
6262
pd$position <- seq2(1, nrow(pd))
6363
pd <- pd[!(pd$token %in% tokens_exclude), ]
6464
if (pd$terminal[1]) {

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ calls_sys <- function(sys_call, ...) {
7878
#' option was not set.
7979
#' @keywords internal
8080
option_read <- function(x, default = NULL, error_if_not_found = TRUE) {
81-
if (x %in% names(options()) | !error_if_not_found) {
81+
if (x %in% names(options()) || !error_if_not_found) {
8282
getOption(x, default)
8383
} else {
8484
rlang::abort(paste("R option", x, "must be set."))

man/next_terminal.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/test_transformer.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/wrap_expr_in_curly.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/wrap_multiline_curly.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)