Skip to content

Commit 4398297

Browse files
Use integer literals and avoid coercions where needed (#994)
1 parent 042624e commit 4398297

37 files changed

+100
-98
lines changed

API

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ cache_info(cache_name = NULL, format = "both")
99
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention(), style_guide_name = NULL, style_guide_version = NULL, more_specs_style_guide = NULL, transformers_drop = specify_transformers_drop(), indent_character = " ")
1010
default_style_guide_attributes(pd_flat)
1111
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
12-
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
12+
specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE)
1313
specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL)
14-
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
15-
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
16-
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), exclude_files = c("R/RcppExports.R", "R/cpp11.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
14+
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
15+
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
16+
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), exclude_files = c("R/RcppExports.R", "R/cpp11.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
1717
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0)
1818
tidyverse_math_token_spacing()
1919
tidyverse_reindention()

R/addins.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ style_active_file <- function() {
5252
context <- get_rstudio_context()
5353
transformer <- make_transformer(get_addins_style_transformer(),
5454
include_roxygen_examples = TRUE,
55-
base_indention = 0,
55+
base_indention = 0L,
5656
warn_empty = is_plain_r_file(context$path)
5757
)
5858
is_r_file <- any(
@@ -124,7 +124,7 @@ style_selection <- function() {
124124
communicate_addins_style_transformers()
125125
context <- get_rstudio_context()
126126
text <- context$selection[[1]]$text
127-
if (all(nchar(text) == 0)) abort("No code selected")
127+
if (all(nchar(text) == 0L)) abort("No code selected")
128128
out <- style_text(
129129
text,
130130
transformers = get_addins_style_transformer(),

R/compat-dplyr.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ slice <- function(.data, ...) {
8080
.data[c(...), , drop = FALSE]
8181
}
8282

83+
# TODO: Use `purrr::map_dfr()` when it stops implicitly relying on `{dplyr}`
8384
#' @importFrom purrr as_mapper map
8485
map_dfr <- function(.x, .f, ..., .id = NULL) {
8586
.f <- as_mapper(.f, ...)

R/detect-alignment-utils.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
3232
alignment_drop_comments <- function(pd_by_line) {
3333
map(pd_by_line, function(x) {
3434
out <- x[x$token != "COMMENT", ]
35-
if (nrow(out) < 1) {
35+
if (nrow(out) < 1L) {
3636
return(NULL)
3737
} else {
3838
out
@@ -184,7 +184,7 @@ alignment_serialize <- function(pd_sub) {
184184
#' @keywords internal
185185
alignment_has_correct_spacing_around_comma <- function(pd_sub) {
186186
comma_tokens <- which(pd_sub$token == "','")
187-
if (length(comma_tokens) == 0) {
187+
if (length(comma_tokens) == 0L) {
188188
return(TRUE)
189189
}
190190
relevant_comma_token <- comma_tokens[seq2(1, length(comma_tokens) - 1L)]
@@ -201,7 +201,7 @@ alignment_has_correct_spacing_around_comma <- function(pd_sub) {
201201
#' @importFrom rlang seq2
202202
alignment_has_correct_spacing_around_eq_sub <- function(pd_sub) {
203203
relevant_eq_sub_token <- which(pd_sub$token == "EQ_SUB")
204-
if (length(relevant_eq_sub_token) == 0) {
204+
if (length(relevant_eq_sub_token) == 0L) {
205205
return(TRUE)
206206
}
207207

R/detect-alignment.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#' transformers <- tidyverse_style()
3434
#' pd_nested <- styler:::compute_parse_data_nested(c(
3535
#' "call(",
36-
#' " ab = 1,",
36+
#' " ab = 1L,",
3737
#' " a = 2",
3838
#' ")"
3939
#' )) %>%
@@ -50,7 +50,7 @@ token_is_on_aligned_line <- function(pd_flat) {
5050
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
5151
pd_by_line <- split(pd_flat, line_idx)
5252
pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL
53-
if (length(pd_by_line) < 1) {
53+
if (length(pd_by_line) < 1L) {
5454
return(TRUE)
5555
}
5656
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1
@@ -93,7 +93,7 @@ token_is_on_aligned_line <- function(pd_flat) {
9393
}
9494

9595
pd_by_line <- alignment_drop_comments(pd_by_line)
96-
if (length(pd_by_line) < 1) {
96+
if (length(pd_by_line) < 1L) {
9797
return(TRUE)
9898
}
9999
pd_by_line <- alignment_drop_last_expr(pd_by_line) %>%
@@ -165,7 +165,7 @@ token_is_on_aligned_line <- function(pd_flat) {
165165
start_after_eq <- start_after_eq[start_after_eq > 0]
166166

167167
if (column >= start_eval) {
168-
if (length(start_after_eq) == 0) {
168+
if (length(start_after_eq) == 0L) {
169169
return(FALSE)
170170
}
171171
# when match via , unsuccessful, matching by = must yield at least one =
@@ -176,7 +176,7 @@ token_is_on_aligned_line <- function(pd_flat) {
176176
previous_line[intersect(names(previous_line), names(start_after_eq))]
177177
}
178178
is_aligned <- all(
179-
length(unique(current_col)) == 1,
179+
length(unique(current_col)) == 1L,
180180
length(start_after_eq) > 1
181181
)
182182
if (!is_aligned) {

R/expr-is.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ contains_else_expr <- function(pd) {
150150
#' @keywords internal
151151
contains_else_expr_that_needs_braces <- function(pd) {
152152
else_idx <- which(pd$token == "ELSE")
153-
if (length(else_idx) > 0) {
153+
if (length(else_idx) > 0L) {
154154
non_comment_after_else <- next_non_comment(pd, else_idx)
155155
sub_expr <- pd$child[[non_comment_after_else]]
156156
# needs braces if NOT if_condition, NOT curly expr

R/indent.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ indent_without_paren_for_while_fun <- function(pd, indent_by) {
2121
return(pd)
2222
}
2323

24-
if (pd$newlines[length(pd$newlines) - 1] == 0) {
24+
if (pd$newlines[length(pd$newlines) - 1] == 0L) {
2525
return(pd)
2626
}
2727
pd$indent[nrow] <- indent_by
@@ -50,7 +50,7 @@ indent_without_paren_if_else <- function(pd, indent_by) {
5050

5151

5252
else_idx <- which(pd$token == "ELSE")
53-
if (length(else_idx) == 0) {
53+
if (length(else_idx) == 0L) {
5454
return(pd)
5555
}
5656
expr_after_else_idx <- next_non_comment(pd, else_idx)
@@ -167,7 +167,7 @@ needs_indention <- function(pd,
167167
needs_indention_one <- function(pd,
168168
potential_trigger_pos,
169169
other_trigger_tokens) {
170-
before_first_break <- which(pd$lag_newlines > 0)[1] - 1L
170+
before_first_break <- which(pd$lag_newlines > 0L)[1] - 1L
171171
if (is.na(before_first_break)) {
172172
return(FALSE)
173173
}
@@ -240,7 +240,7 @@ pd_multi_line <- function(pd) {
240240
#' @seealso choose_indention
241241
#' @keywords internal
242242
update_newlines <- function(pd) {
243-
seq_pd <- seq_len(nrow(pd) - 1)
244-
pd$newlines[seq_pd] <- pd$lag_newlines[seq_pd + 1]
243+
seq_pd <- seq_len(nrow(pd) - 1L)
244+
pd$newlines[seq_pd] <- pd$lag_newlines[seq_pd + 1L]
245245
pd
246246
}

R/initialize.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ NULL
3737
#' @describeIn initialize_attributes Initializes `newlines` and `lag_newlines`.
3838
#' @keywords internal
3939
initialize_newlines <- function(pd_flat) {
40-
pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1))
40+
pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1L))
4141
pd_flat$newlines <- pd_flat$line3 - pd_flat$line2
4242
pd_flat$lag_newlines <- lag(pd_flat$newlines, default = 0L)
4343
pd_flat$line3 <- NULL
@@ -47,7 +47,7 @@ initialize_newlines <- function(pd_flat) {
4747
#' @describeIn initialize_attributes Initializes `spaces`.
4848
#' @keywords internal
4949
initialize_spaces <- function(pd_flat) {
50-
pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1) + 1L)
50+
pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1L) + 1L)
5151
pd_flat$col2_nl <- ifelse(pd_flat$newlines > 0L,
5252
rep(0L, nrow(pd_flat)), pd_flat$col2
5353
)
@@ -83,7 +83,7 @@ initialize_indention_ref_pos_id <- function(pd_flat) {
8383
#' @keywords internal
8484
initialize_indent <- function(pd_flat) {
8585
if (!("indent" %in% names(pd_flat))) {
86-
pd_flat$indent <- 0
86+
pd_flat$indent <- 0L
8787
}
8888
pd_flat
8989
}

R/io.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ read_utf8_bare <- function(con, warn = TRUE) {
100100
x <- readLines(con, encoding = "UTF-8", warn = warn)
101101
i <- invalid_utf8(x)
102102
n <- length(i)
103-
if (n > 0) {
103+
if (n > 0L) {
104104
stop(
105105
c(
106106
"The file ", con, " is not encoded in UTF-8. ",

R/nest.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ drop_cached_children <- function(pd) {
112112
order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal))
113113
pd_parent_first <- pd[order, ]
114114
pos_ids_to_keep <- pd_parent_first %>%
115-
split(cumsum(pd_parent_first$parent == 0)) %>%
115+
split(cumsum(pd_parent_first$parent == 0L)) %>%
116116
map(find_pos_id_to_keep) %>%
117117
unlist(use.names = FALSE)
118118
pd[pd$pos_id %in% pos_ids_to_keep, ]
@@ -124,11 +124,11 @@ drop_cached_children <- function(pd) {
124124
#' Find the pos ids to keep
125125
#'
126126
#' To make a parse table shallow, we must know which ids to keep.
127-
#' `split(cumsum(pd_parent_first$parent == 0))` above puts comments with
127+
#' `split(cumsum(pd_parent_first$parent == 0L))` above puts comments with
128128
#' negative parents in the same block as proceeding expressions (but also with
129129
#' positive).
130130
#' `find_pos_id_to_keep()` must hence always keep negative comments. We did not
131-
#' use `split(cumsum(pd_parent_first$parent < 1))` because then every top-level
131+
#' use `split(cumsum(pd_parent_first$parent < 1L))` because then every top-level
132132
#' comment is an expression on its own and processing takes much longer for
133133
#' typical roxygen annotated code.
134134
#' @param pd A temporary top level nest where the first expression is always a
@@ -340,10 +340,10 @@ set_spaces <- function(spaces_after_prefix, force_one) {
340340
#' @importFrom purrr map2
341341
#' @keywords internal
342342
nest_parse_data <- function(pd_flat) {
343-
if (all(pd_flat$parent <= 0)) {
343+
if (all(pd_flat$parent <= 0L)) {
344344
return(pd_flat)
345345
}
346-
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0))
346+
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
347347
split_data <- split(pd_flat, pd_flat$internal)
348348

349349
child <- split_data$`FALSE`
@@ -381,7 +381,7 @@ nest_parse_data <- function(pd_flat) {
381381
#' @keywords internal
382382
combine_children <- function(child, internal_child) {
383383
bound <- bind_rows(child, internal_child)
384-
if (nrow(bound) == 0) {
384+
if (nrow(bound) == 0L) {
385385
return(NULL)
386386
}
387387
bound[order(bound$pos_id), ]

0 commit comments

Comments
 (0)