Skip to content
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

Use integer literals and avoid coercions where needed #994

Merged
merged 6 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import(tibble)
importFrom(magrittr,"%>%")
importFrom(magrittr,or)
importFrom(magrittr,set_names)
importFrom(purrr,as_mapper)
importFrom(purrr,compact)
importFrom(purrr,flatten)
importFrom(purrr,flatten_chr)
Expand Down
4 changes: 2 additions & 2 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ style_active_file <- function() {
context <- get_rstudio_context()
transformer <- make_transformer(get_addins_style_transformer(),
include_roxygen_examples = TRUE,
base_indention = 0,
base_indention = 0L,
warn_empty = is_plain_r_file(context$path)
)
is_r_file <- any(
Expand Down Expand Up @@ -124,7 +124,7 @@ style_selection <- function() {
communicate_addins_style_transformers()
context <- get_rstudio_context()
text <- context$selection[[1]]$text
if (all(nchar(text) == 0)) abort("No code selected")
if (all(nchar(text) == 0L)) abort("No code selected")
out <- style_text(
text,
transformers = get_addins_style_transformer(),
Expand Down
7 changes: 0 additions & 7 deletions R/compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,3 @@ last <- function(x) {
slice <- function(.data, ...) {
.data[c(...), , drop = FALSE]
}

#' @importFrom purrr as_mapper map
map_dfr <- function(.x, .f, ..., .id = NULL) {
.f <- as_mapper(.f, ...)
res <- map(.x, .f, ...)
bind_rows(res, .id = .id)
}
6 changes: 3 additions & 3 deletions R/detect-alignment-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ alignment_ensure_no_closing_brace <- function(pd_by_line,
alignment_drop_comments <- function(pd_by_line) {
map(pd_by_line, function(x) {
out <- x[x$token != "COMMENT", ]
if (nrow(out) < 1) {
if (nrow(out) < 1L) {
return(NULL)
} else {
out
Expand Down Expand Up @@ -184,7 +184,7 @@ alignment_serialize <- function(pd_sub) {
#' @keywords internal
alignment_has_correct_spacing_around_comma <- function(pd_sub) {
comma_tokens <- which(pd_sub$token == "','")
if (length(comma_tokens) == 0) {
if (length(comma_tokens) == 0L) {
return(TRUE)
}
relevant_comma_token <- comma_tokens[seq2(1, length(comma_tokens) - 1L)]
Expand All @@ -201,7 +201,7 @@ alignment_has_correct_spacing_around_comma <- function(pd_sub) {
#' @importFrom rlang seq2
alignment_has_correct_spacing_around_eq_sub <- function(pd_sub) {
relevant_eq_sub_token <- which(pd_sub$token == "EQ_SUB")
if (length(relevant_eq_sub_token) == 0) {
if (length(relevant_eq_sub_token) == 0L) {
return(TRUE)
}

Expand Down
10 changes: 5 additions & 5 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' transformers <- tidyverse_style()
#' pd_nested <- styler:::compute_parse_data_nested(c(
#' "call(",
#' " ab = 1,",
#' " ab = 1L,",
#' " a = 2",
#' ")"
#' )) %>%
Expand All @@ -50,7 +50,7 @@ token_is_on_aligned_line <- function(pd_flat) {
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
pd_by_line <- split(pd_flat, line_idx)
pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL
if (length(pd_by_line) < 1) {
if (length(pd_by_line) < 1L) {
return(TRUE)
}
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1
Expand Down Expand Up @@ -93,7 +93,7 @@ token_is_on_aligned_line <- function(pd_flat) {
}

pd_by_line <- alignment_drop_comments(pd_by_line)
if (length(pd_by_line) < 1) {
if (length(pd_by_line) < 1L) {
return(TRUE)
}
pd_by_line <- alignment_drop_last_expr(pd_by_line) %>%
Expand Down Expand Up @@ -165,7 +165,7 @@ token_is_on_aligned_line <- function(pd_flat) {
start_after_eq <- start_after_eq[start_after_eq > 0]

if (column >= start_eval) {
if (length(start_after_eq) == 0) {
if (length(start_after_eq) == 0L) {
return(FALSE)
}
# when match via , unsuccessful, matching by = must yield at least one =
Expand All @@ -176,7 +176,7 @@ token_is_on_aligned_line <- function(pd_flat) {
previous_line[intersect(names(previous_line), names(start_after_eq))]
}
is_aligned <- all(
length(unique(current_col)) == 1,
length(unique(current_col)) == 1L,
length(start_after_eq) > 1
)
if (!is_aligned) {
Expand Down
2 changes: 1 addition & 1 deletion R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ contains_else_expr <- function(pd) {
#' @keywords internal
contains_else_expr_that_needs_braces <- function(pd) {
else_idx <- which(pd$token == "ELSE")
if (length(else_idx) > 0) {
if (length(else_idx) > 0L) {
non_comment_after_else <- next_non_comment(pd, else_idx)
sub_expr <- pd$child[[non_comment_after_else]]
# needs braces if NOT if_condition, NOT curly expr
Expand Down
10 changes: 5 additions & 5 deletions R/indent.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ indent_without_paren_for_while_fun <- function(pd, indent_by) {
return(pd)
}

if (pd$newlines[length(pd$newlines) - 1] == 0) {
if (pd$newlines[length(pd$newlines) - 1] == 0L) {
return(pd)
}
pd$indent[nrow] <- indent_by
Expand Down Expand Up @@ -50,7 +50,7 @@ indent_without_paren_if_else <- function(pd, indent_by) {


else_idx <- which(pd$token == "ELSE")
if (length(else_idx) == 0) {
if (length(else_idx) == 0L) {
return(pd)
}
expr_after_else_idx <- next_non_comment(pd, else_idx)
Expand Down Expand Up @@ -167,7 +167,7 @@ needs_indention <- function(pd,
needs_indention_one <- function(pd,
potential_trigger_pos,
other_trigger_tokens) {
before_first_break <- which(pd$lag_newlines > 0)[1] - 1L
before_first_break <- which(pd$lag_newlines > 0L)[1] - 1L
if (is.na(before_first_break)) {
return(FALSE)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ pd_multi_line <- function(pd) {
#' @seealso choose_indention
#' @keywords internal
update_newlines <- function(pd) {
seq_pd <- seq_len(nrow(pd) - 1)
pd$newlines[seq_pd] <- pd$lag_newlines[seq_pd + 1]
seq_pd <- seq_len(nrow(pd) - 1L)
pd$newlines[seq_pd] <- pd$lag_newlines[seq_pd + 1L]
pd
}
2 changes: 1 addition & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ read_utf8_bare <- function(con, warn = TRUE) {
x <- readLines(con, encoding = "UTF-8", warn = warn)
i <- invalid_utf8(x)
n <- length(i)
if (n > 0) {
if (n > 0L) {
stop(
c(
"The file ", con, " is not encoded in UTF-8. ",
Expand Down
12 changes: 6 additions & 6 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ drop_cached_children <- function(pd) {
order <- order(pd$line1, pd$col1, -pd$line2, -pd$col2, as.integer(pd$terminal))
pd_parent_first <- pd[order, ]
pos_ids_to_keep <- pd_parent_first %>%
split(cumsum(pd_parent_first$parent == 0)) %>%
split(cumsum(pd_parent_first$parent == 0L)) %>%
map(find_pos_id_to_keep) %>%
unlist() %>%
unname()
Expand All @@ -125,11 +125,11 @@ drop_cached_children <- function(pd) {
#' Find the pos ids to keep
#'
#' To make a parse table shallow, we must know which ids to keep.
#' `split(cumsum(pd_parent_first$parent == 0))` above puts comments with
#' `split(cumsum(pd_parent_first$parent == 0L))` above puts comments with
#' negative parents in the same block as proceeding expressions (but also with
#' positive).
#' `find_pos_id_to_keep()` must hence always keep negative comments. We did not
#' use `split(cumsum(pd_parent_first$parent < 1))` because then every top-level
#' use `split(cumsum(pd_parent_first$parent < 1L))` because then every top-level
#' comment is an expression on its own and processing takes much longer for
#' typical roxygen annotated code.
#' @param pd A temporary top level nest where the first expression is always a
Expand Down Expand Up @@ -341,10 +341,10 @@ set_spaces <- function(spaces_after_prefix, force_one) {
#' @importFrom purrr map2
#' @keywords internal
nest_parse_data <- function(pd_flat) {
if (all(pd_flat$parent <= 0)) {
if (all(pd_flat$parent <= 0L)) {
return(pd_flat)
}
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0))
pd_flat$internal <- with(pd_flat, (id %in% parent) | (parent <= 0L))
split_data <- split(pd_flat, pd_flat$internal)

child <- split_data$`FALSE`
Expand Down Expand Up @@ -382,7 +382,7 @@ nest_parse_data <- function(pd_flat) {
#' @keywords internal
combine_children <- function(child, internal_child) {
bound <- bind_rows(child, internal_child)
if (nrow(bound) == 0) {
if (nrow(bound) == 0L) {
return(NULL)
}
bound[order(bound$pos_id), ]
Expand Down
6 changes: 3 additions & 3 deletions R/reindent.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ find_tokens_to_update <- function(flattened_pd, target_token) {
#' @keywords internal
set_regex_indention <- function(flattened_pd,
pattern,
target_indention = 0,
target_indention = 0L,
comments_only = TRUE) {
if (comments_only) {
cond <- which(
(flattened_pd$token == "COMMENT") & (flattened_pd$lag_newlines > 0)
(flattened_pd$token == "COMMENT") & (flattened_pd$lag_newlines > 0L)
)
if (length(cond) < 1) {
if (length(cond) < 1L) {
return(flattened_pd)
}
to_check <- flattened_pd[cond, ]
Expand Down
8 changes: 4 additions & 4 deletions R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ flatten_operators_one <- function(pd_nested) {
#' @keywords internal
flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) {
token_pos_candidates <- which(pd_nested$token[-1] %in% token) + 1
if (length(token_pos_candidates) == 0) {
if (length(token_pos_candidates) == 0L) {
return(pd_nested)
}
token_pos <- token_pos_candidates[
Expand All @@ -64,7 +64,7 @@ flatten_pd <- function(pd_nested, token, child_token = token, left = TRUE) {
} else {
pos <- next_non_comment(pd_nested, token_pos)
}
if (pos < 1) {
if (pos < 1L) {
return(pd_nested)
}
if (!any(pd_nested$child[[pos]]$token[-1] %in% child_token)) {
Expand Down Expand Up @@ -173,10 +173,10 @@ relocate_eq_assign <- function(pd) {
#' @keywords internal
relocate_eq_assign_nest <- function(pd) {
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
if (length(idx_eq_assign) > 0) {
if (length(idx_eq_assign) > 0L) {
block_id <- find_block_id(pd)
blocks <- split(pd, block_id)
pd <- map_dfr(blocks, relocate_eq_assign_one)
pd <- purrr::map_dfr(blocks, relocate_eq_assign_one)
}
pd
}
Expand Down
4 changes: 2 additions & 2 deletions R/roxygen-examples-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ roxygen_remove_extra_brace <- function(parsed) {
while (worth_trying_to_remove_brace) {
# remove brace
brace <- which(parsed == "}")
if (length(brace) > 0) {
if (length(brace) > 0L) {
parsed <- parsed[-last(brace)]
}
linebreak <- which(parsed == "\n")
if (length(linebreak) > 0) {
if (length(linebreak) > 0L) {
parsed <- parsed[-last(linebreak)]
}
# try if can be parsed (need remve dontrun)
Expand Down
18 changes: 9 additions & 9 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ set_line_break_before_curly_opening <- function(pd) {
)

line_break_to_set_idx <- setdiff(line_break_to_set_idx, nrow(pd))
if (length(line_break_to_set_idx) > 0) {
if (length(line_break_to_set_idx) > 0L) {
is_not_curly_curly <- map_chr(
line_break_to_set_idx + 1L,
~ next_terminal(pd[.x, ], vars = "token_after")$token_after
Expand All @@ -74,7 +74,7 @@ set_line_break_before_curly_opening <- function(pd) {
linebreak_before_curly <- ifelse(is_function_call(pd),
# if in function call and has pipe, it is not recognized as function call
# and goes to else case
any(pd$lag_newlines[seq2(1, line_break_to_set_idx[1])] > 0),
any(pd$lag_newlines[seq2(1, line_break_to_set_idx[1])] > 0L),
# if not a function call, only break line if it is a pipe followed by {}
pd$token[line_break_to_set_idx] %in% c("SPECIAL-PIPE", "PIPE")
)
Expand Down Expand Up @@ -107,7 +107,7 @@ set_line_break_before_curly_opening <- function(pd) {
pd$lag_newlines[should_not_be_on_same_line_idx] <- 1L

# non-curly expressions after curly expressions must have line breaks
if (length(should_not_be_on_same_line_idx) > 0) {
if (length(should_not_be_on_same_line_idx) > 0L) {
comma_exprs_idx <- which(pd$token == "','")
comma_exprs_idx <- setdiff(comma_exprs_idx, 1 + is_not_curly_curly_idx)
non_comment_after_comma <- map_int(comma_exprs_idx,
Expand All @@ -128,7 +128,7 @@ set_line_break_around_comma_and_or <- function(pd, strict) {
ops <- c("','", "AND", "OR", "AND2", "OR2")
comma_with_line_break_that_can_be_removed_before <-
(pd$token %in% ops) &
(pd$lag_newlines > 0) &
(pd$lag_newlines > 0L) &
(pd$token_before != "COMMENT") &
(lag(pd$token) != "'['")

Expand All @@ -137,7 +137,7 @@ set_line_break_around_comma_and_or <- function(pd, strict) {

comma_with_line_break_that_can_be_moved_two_tokens_left <- which(
(pd$token == "EQ_SUB") &
(pd$lag_newlines > 0) &
(pd$lag_newlines > 0L) &
(pd$token_before != "COMMENT") &
(lag(pd$token) != "'['")
)
Expand Down Expand Up @@ -307,13 +307,13 @@ set_line_break_after_opening_if_call_is_multi_line <- function(pd,
nrow(pd) # always break before last because this is multi-line
)
} else {
if (!any(pd$lag_newlines[seq2(3L, nrow(pd))] > 0)) {
if (!any(pd$lag_newlines[seq2(3L, nrow(pd))] > 0L)) {
return(pd)
}
break_pos <- find_line_break_position_in_multiline_call(pd)
idx_nested <- next_non_comment(pd, 2)
nested_call <- is_function_call(pd$child[[idx_nested]])
if (pd_is_multi_line(pd$child[[idx_nested]]) && sum(pd$lag_newlines) > 0) {
if (pd_is_multi_line(pd$child[[idx_nested]]) && sum(pd$lag_newlines) > 0L) {
break_pos <- c(break_pos, idx_nested)
}
}
Expand Down Expand Up @@ -354,8 +354,8 @@ set_line_break_before_closing_call <- function(pd, except_token_before) {
return(pd)
}
npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3L, npd - 1L)] > 0)
if (is_multi_line == 0) {
is_multi_line <- any(pd$lag_newlines[seq2(3L, npd - 1L)] > 0L)
if (is_multi_line == 0L) {
exception <- which(pd$token_before %in% except_token_before)
pd$lag_newlines[setdiff(npd, exception)] <- 0L
return(pd)
Expand Down
12 changes: 7 additions & 5 deletions R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ set_space_around_op <- function(pd_flat, strict) {
if (!any(op_after)) {
return(pd_flat)
}

sum_lag_newlines <- sum(pd_flat$lag_newlines)
if (
!getOption("styler.ignore_alignment", FALSE) &&
(
(is_function_call(pd_flat) && sum(pd_flat$lag_newlines) > 2) ||
(is_function_dec(pd_flat) && sum(pd_flat$lag_newlines) > 1)
(is_function_call(pd_flat) && sum_lag_newlines > 2L) ||
(is_function_dec(pd_flat) && sum_lag_newlines > 1L)
) &&
any(pd_flat$token %in% c("EQ_SUB", "','", "EQ_FORMALS"))
) {
Expand Down Expand Up @@ -106,12 +108,12 @@ style_space_around_tilde <- function(pd_flat, strict) {
if (is_symmetric_tilde_expr(pd_flat)) {
pd_flat <- style_space_around_token(pd_flat,
strict, "'~'",
level_before = 1, level_after = 1
level_before = 1L, level_after = 1L
)
} else if (is_asymmetric_tilde_expr(pd_flat)) {
pd_flat <- style_space_around_token(pd_flat,
strict = TRUE, "'~'", level_before = 1,
level_after = ifelse(nrow(pd_flat$child[[2]]) > 1, 1, 0)
strict = TRUE, "'~'", level_before = 1L,
level_after = ifelse(nrow(pd_flat$child[[2]]) > 1L, 1L, 0L)
)
}
pd_flat
Expand Down
Loading