From 131a3892f80fccb5e4a1a1217a1dcab699f3724d Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Tue, 8 Nov 2022 14:41:19 +0100 Subject: [PATCH] Use integer literals where needed A few I missed out on in previous PRs. --- R/indent.R | 2 +- R/io.R | 2 +- R/nest.R | 2 +- R/relevel.R | 8 ++++---- R/rules-tokens.R | 6 +++--- R/transform-block.R | 4 ++-- R/transform-code.R | 6 +++--- R/utils-navigate-nest.R | 8 ++++---- R/utils.R | 10 +++++----- man/ensure_last_n_empty.Rd | 2 +- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/R/indent.R b/R/indent.R index 475291b9c..bcf7a0c73 100644 --- a/R/indent.R +++ b/R/indent.R @@ -106,7 +106,7 @@ compute_indent_indices <- function(pd, ) trigger <- potential_triggers[needs_indention][1L] if (is.na(trigger)) { - return(numeric(0)) + return(numeric(0L)) } start <- trigger + 1 if (is.null(token_closing)) { diff --git a/R/io.R b/R/io.R index 4c0e6334d..e79364979 100644 --- a/R/io.R +++ b/R/io.R @@ -29,7 +29,7 @@ transform_utf8_one <- function(path, fun, dry) { # only write back when changed OR when there was a missing newline new <- unclass(fun(file_with_info$text)) if (identical(new, "")) { - new <- character(0) + new <- character(0L) } identical_content <- identical(file_with_info$text, new) identical <- identical_content && !file_with_info$missing_EOF_line_break diff --git a/R/nest.R b/R/nest.R index 0b1f60a60..32b035956 100644 --- a/R/nest.R +++ b/R/nest.R @@ -65,7 +65,7 @@ add_cache_block <- function(pd_nested) { if (cache_is_activated()) { pd_nested$block <- cache_find_block(pd_nested) } else { - pd_nested$block <- rep(1, length(pd_nested$block)) + pd_nested$block <- rep(1L, length(pd_nested$block)) } pd_nested } diff --git a/R/relevel.R b/R/relevel.R index 1b7208ee8..eb376d82b 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -197,10 +197,10 @@ relocate_eq_assign_nest <- function(pd) { #' @keywords internal find_block_id <- function(pd) { idx_eq_assign <- which(pd$token == "EQ_ASSIGN") - eq_belongs_to_block <- c(0, diff(idx_eq_assign) > 2) + eq_belongs_to_block <- c(0L, diff(idx_eq_assign) > 2L) - empty_seq <- rep(0, nrow(pd)) - empty_seq[idx_eq_assign - 1] <- eq_belongs_to_block + empty_seq <- rep(0L, nrow(pd)) + empty_seq[idx_eq_assign - 1L] <- eq_belongs_to_block block_id <- cumsum(empty_seq) block_id } @@ -216,7 +216,7 @@ relocate_eq_assign_one <- function(pd) { idx_eq_assign <- which(pd$token == "EQ_ASSIGN") eq_ind <- seq2(idx_eq_assign[1L] - 1L, last(idx_eq_assign) + 1L) # initialize because wrap_expr_in_expr -> create_tokens -> requires it - pd$indent <- 0 + pd$indent <- 0L eq_expr <- pd[eq_ind, ] %>% wrap_expr_in_expr() %>% add_line_col_to_wrapped_expr() %>% diff --git a/R/rules-tokens.R b/R/rules-tokens.R index e6b33780f..8f77897f0 100644 --- a/R/rules-tokens.R +++ b/R/rules-tokens.R @@ -31,12 +31,12 @@ add_brackets_in_pipe_child <- function(pd) { add_brackets_in_pipe_one <- function(pd, pos) { next_non_comment <- next_non_comment(pd, pos) rh_child <- pd$child[[next_non_comment]] - if (nrow(rh_child) < 2 && rh_child$token == "SYMBOL") { + if (nrow(rh_child) < 2L && rh_child$token == "SYMBOL") { child <- pd$child[[next_non_comment]] - new_pos_ids <- create_pos_ids(child, 1, after = TRUE, n = 2L) + new_pos_ids <- create_pos_ids(child, 1L, after = TRUE, n = 2L) new_pd <- create_tokens( texts = c("(", ")"), - lag_newlines = rep(0L, 2), + lag_newlines = rep(0L, 2L), spaces = 0L, pos_ids = new_pos_ids, token_before = c(child$token[1L], "'('"), diff --git a/R/transform-block.R b/R/transform-block.R index 798008cdc..5b533fb60 100644 --- a/R/transform-block.R +++ b/R/transform-block.R @@ -45,13 +45,13 @@ parse_transform_serialize_r_block <- function(pd_nested, ) } else { serialized_transformed_text <- map2( - c(0, find_blank_lines_to_next_expr(pd_nested)[-1L] - 1L), + c(0L, find_blank_lines_to_next_expr(pd_nested)[-1L] - 1L), paste0(rep_char(" ", base_indention), pd_nested$text), ~ c(rep("", .x), .y) ) %>% unlist(use.names = FALSE) } - c(rep("", start_line - 1), serialized_transformed_text) + c(rep("", start_line - 1L), serialized_transformed_text) } #' Find the groups of expressions that should be processed together diff --git a/R/transform-code.R b/R/transform-code.R index 4e3269965..5224e9ac4 100644 --- a/R/transform-code.R +++ b/R/transform-code.R @@ -41,7 +41,7 @@ transform_mixed <- function(lines, transformer_fun, filetype) { chunks$r_chunks <- map(chunks$r_chunks, transform_mixed_non_empty, transformer_fun = transformer_fun ) - map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0))), c) %>% + map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0L))), c) %>% flatten_chr() } @@ -70,11 +70,11 @@ separate_chunks <- function(lines, filetype) { r_raw_chunks <- identify_raw_chunks(lines, filetype = filetype) r_chunks <- map2( - r_raw_chunks$starts, r_raw_chunks$ends, ~ lines[seq2(.x + 1, .y - 1)] + r_raw_chunks$starts, r_raw_chunks$ends, ~ lines[seq2(.x + 1L, .y - 1L)] ) text_chunks <- map2( - c(1, r_raw_chunks$ends), c(r_raw_chunks$starts, length(lines)), + c(1L, r_raw_chunks$ends), c(r_raw_chunks$starts, length(lines)), ~ lines[seq2(.x, .y)] ) list(r_chunks = r_chunks, text_chunks = text_chunks) diff --git a/R/utils-navigate-nest.R b/R/utils-navigate-nest.R index 0d2051467..f518b8c69 100644 --- a/R/utils-navigate-nest.R +++ b/R/utils-navigate-nest.R @@ -13,11 +13,11 @@ #' @export next_non_comment <- function(pd, pos) { if (length(pos) < 1 || is.na(pos) || pos >= nrow(pd)) { - return(integer(0)) + return(integer(0L)) } candidates <- seq2(pos + 1L, nrow(pd)) if (all(candidates %in% which(pd$token == "COMMENT"))) { - return(integer(0)) + return(integer(0L)) } setdiff(candidates, which(pd$token == "COMMENT"))[1L] } @@ -26,11 +26,11 @@ next_non_comment <- function(pd, pos) { #' @rdname next_non_comment previous_non_comment <- function(pd, pos) { if (length(pos) < 1 || is.na(pos) || pos > nrow(pd)) { - return(integer(0)) + return(integer(0L)) } candidates <- seq2(1L, pos - 1L) if (all(candidates %in% which(pd$token == "COMMENT"))) { - return(integer(0)) + return(integer(0L)) } last(setdiff(candidates, which(pd$token == "COMMENT"))) } diff --git a/R/utils.R b/R/utils.R index ff3d38659..d5150e89a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -23,12 +23,12 @@ new_styler_df <- function(x) { #' styler:::ensure_last_n_empty(letters) #' styler:::ensure_last_n_empty(c(letters, "", "", "")) #' @keywords internal -ensure_last_n_empty <- function(x, n = 1) { +ensure_last_n_empty <- function(x, n = 1L) { if (all(x == "")) { return("") } x <- c(x, "", "") - x <- x[seq(1, length(x) - which(rev(x) != "")[1L] + 1L)] + x <- x[seq(1L, length(x) - which(rev(x) != "")[1L] + 1L)] c(x, rep("", n)) } @@ -43,14 +43,14 @@ re_match <- function(text, pattern) { length <- attr(match, "match.length") end <- start + length - 1L matchstr <- substring(text, start, end) - matchstr[start == -1] <- NA_character_ + matchstr[start == -1L] <- NA_character_ res <- data.frame(stringsAsFactors = FALSE, .text = text, .match = matchstr) gstart <- attr(match, "capture.start") glength <- attr(match, "capture.length") gend <- gstart + glength - 1L groupstr <- substring(text, gstart, gend) - groupstr[gstart == -1] <- NA_character_ + groupstr[gstart == -1L] <- NA_character_ dim(groupstr) <- dim(gstart) res <- cbind(groupstr, res, stringsAsFactors = FALSE) @@ -70,7 +70,7 @@ re_match <- function(text, pattern) { #' @keywords internal convert_newlines_to_linebreaks <- function(text) { split <- strsplit(text, "\n", fixed = TRUE) - map(split, ~ if (identical(.x, character(0))) { + map(split, ~ if (identical(.x, character(0L))) { "" } else { .x diff --git a/man/ensure_last_n_empty.Rd b/man/ensure_last_n_empty.Rd index 489a67f2f..37c82f804 100644 --- a/man/ensure_last_n_empty.Rd +++ b/man/ensure_last_n_empty.Rd @@ -4,7 +4,7 @@ \alias{ensure_last_n_empty} \title{Ensure there is one (and only one) blank line at the end of a vector} \usage{ -ensure_last_n_empty(x, n = 1) +ensure_last_n_empty(x, n = 1L) } \description{ Ensure there is one (and only one) blank line at the end of a vector