Skip to content

Commit

Permalink
Enable brace-related linters: brace_linter, paren_body_linter (#143)
Browse files Browse the repository at this point in the history
* Fix '){' --> ') {'

* Rest of paren_body_linter hits

* '}else{' spacing

* more cases like '}else' 'else{' etc

* Cases like '{.' or ')}'

* line-initial '{'

* Some disabling

* brace-wrapping single-expression functions

* More multiline unbraced funs

* Should be the last of them

* Enable brace_linter

* updated test output for match.arg

* arg

* use match.arg in qtile.integer64
  • Loading branch information
MichaelChirico authored Jan 22, 2025
1 parent d61782b commit 36160cb
Show file tree
Hide file tree
Showing 13 changed files with 997 additions and 1,023 deletions.
4 changes: 1 addition & 3 deletions .lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ linters = all_linters(
defaults = default_undesirable_functions,
library = NULL,
options = NULL,
par = NULL,
par = NULL
)),
assignment_linter = NULL,
brace_linter = NULL,
commas_linter = NULL,
commented_code_linter = NULL,
condition_call_linter = NULL,
Expand All @@ -22,7 +21,6 @@ linters = all_linters(
line_length_linter = NULL,
nonportable_path_linter = NULL,
object_name_linter = NULL,
paren_body_linter = NULL,
quotes_linter = NULL,
todo_comment_linter = NULL
)
87 changes: 43 additions & 44 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#' @name cache
NULL

#still.identical <- function(x, y){
#still.identical <- function(x, y) {
# .Call(C_r_ram_truly_identical, x = x, y = y, PACKAGE = "bit64")
#}

Expand All @@ -79,31 +79,30 @@ newcache <- function(x) {
#' @describeIn cache forces `x` to have a cache
#' @export
jamcache <- function(x) {
cache <- attr(x, "cache")
if (is.null(cache)){
cache <- newcache(x)
setattr(x, "cache", cache)
}else
if (!bit::still.identical(x, get("x", envir=cache, inherits=FALSE))){
cache <- newcache(x)
setattr(x, "cache", cache)
warning("replaced outdated cache with empty cache")
}
cache
cache <- attr(x, "cache")
if (is.null(cache)) {
cache <- newcache(x)
setattr(x, "cache", cache)
} else if (!bit::still.identical(x, get("x", envir=cache, inherits=FALSE))) {
cache <- newcache(x)
setattr(x, "cache", cache)
warning("replaced outdated cache with empty cache")
}
cache
}

#' @describeIn cache returns the cache attached to `x` if it is not
#' found to be outdated
#' @export
cache <- function(x) {
cache <- attr(x, "cache")
if (is.null(cache) || bit::still.identical(x, get("x", envir=cache, inherits=FALSE)))
cache
else{
remcache(x)
warning("removed outdated cache")
NULL
}
cache <- attr(x, "cache")
if (is.null(cache) || bit::still.identical(x, get("x", envir=cache, inherits=FALSE))) {
cache
} else {
remcache(x)
warning("removed outdated cache")
NULL
}
}

#' @describeIn cache assigns a value into the cache of `x`
Expand All @@ -118,16 +117,16 @@ setcache <- function(x, which, value) {

#' @describeIn cache gets cache value 'which' from `x`
#' @export
getcache <- function(x, which){
getcache <- function(x, which) {
cache <- attr(x, "cache")
if (is.null(cache))
return(NULL)
if (bit::still.identical(x, get("x", envir=cache, inherits=FALSE))){
if (bit::still.identical(x, get("x", envir=cache, inherits=FALSE))) {
if (exists(which, envir=cache, inherits=FALSE))
get(which, envir=cache, inherits=FALSE)
else
NULL
}else{
} else {
remcache(x)
warning("removed outdated cache")
NULL
Expand All @@ -145,7 +144,7 @@ remcache <- function(x) {
#' @param all.names,pattern passed to [ls()] when listing the cache content
#' @param ... ignored
#' @export
print.cache<- function(x, all.names=FALSE, pattern, ...){
print.cache<- function(x, all.names=FALSE, pattern, ...) {
l <- ls(x, all.names, pattern=pattern)
cat(class(x)[1L], ": ", paste(l, collapse=" - "), "\n", sep="")
invisible(l)
Expand Down Expand Up @@ -185,7 +184,7 @@ print.cache<- function(x, all.names=FALSE, pattern, ...){
#'
#' @keywords environment
#' @export
hashcache <-function(x, nunique=NULL, ...){
hashcache <-function(x, nunique=NULL, ...) {
env <- jamcache(x)
if (is.null(nunique))
nunique <- env$nunique
Expand All @@ -202,8 +201,8 @@ hashcache <-function(x, nunique=NULL, ...){
#' `NA`s. If we know we don't have `NA`s, this may speed-up. _Note_ that you
#' risk a crash if there are unexpected `NA`s with `has.na=FALSE`.
#' @export
sortcache <- function(x, has.na = NULL){
if (is.null(has.na)){
sortcache <- function(x, has.na = NULL) {
if (is.null(has.na)) {
na.count <- getcache(x, "na.count")
if (is.null(na.count))
has.na <- TRUE
Expand All @@ -224,15 +223,15 @@ sortcache <- function(x, has.na = NULL){
#' @param stable boolean scalar defining whether stable sorting is needed. Allowing
#' non-stable may speed-up.
#' @export
sortordercache <- function(x, has.na = NULL, stable = NULL){
if (is.null(has.na)){
sortordercache <- function(x, has.na = NULL, stable = NULL) {
if (is.null(has.na)) {
na.count <- getcache(x, "na.count")
if (is.null(na.count))
has.na <- TRUE
else
has.na <- na.count > 0L
}
if (is.null(stable)){
if (is.null(stable)) {
nunique <- getcache(x, "nunique")
if (is.null(nunique))
stable <- TRUE
Expand All @@ -255,15 +254,15 @@ sortordercache <- function(x, has.na = NULL, stable = NULL){
#' @param optimize by default ramsort optimizes for 'time' which requires more RAM,
#' set to 'memory' to minimize RAM requirements and sacrifice speed.
#' @export
ordercache <- function(x, has.na = NULL, stable = NULL, optimize = "time"){
if (is.null(has.na)){
ordercache <- function(x, has.na = NULL, stable = NULL, optimize = "time") {
if (is.null(has.na)) {
na.count <- getcache(x, "na.count")
if (is.null(na.count))
has.na <- TRUE
else
has.na <- na.count > 0L
}
if (is.null(stable)){
if (is.null(stable)) {
nunique <- getcache(x, "nunique")
if (is.null(nunique))
stable <- TRUE
Expand Down Expand Up @@ -320,7 +319,7 @@ NULL

#' @describeIn is.sorted.integer64 returns the number of `NA`s
#' @export
na.count.integer64 <- function(x, ...){
na.count.integer64 <- function(x, ...) {
env <- cache(x)
if (is.null(env))
return(.Call(C_r_ram_integer64_nacount, x = x, PACKAGE = "bit64"))
Expand All @@ -334,13 +333,13 @@ na.count.integer64 <- function(x, ...){
#' @describeIn is.sorted.integer64 returns the number of valid data points,
#' usually [length()] minus `na.count`.
#' @export
nvalid.integer64 <- function(x, ...){
nvalid.integer64 <- function(x, ...) {
length(x) - na.count(x)
}

#' @describeIn is.sorted.integer64 checks for sortedness of `x` (NAs sorted first)
#' @export
is.sorted.integer64 <- function(x, ...){
is.sorted.integer64 <- function(x, ...) {
env <- cache(x)
if (is.null(env))
return(.Call(C_r_ram_integer64_issorted_asc, x = x, PACKAGE = "bit64"))
Expand All @@ -353,25 +352,25 @@ is.sorted.integer64 <- function(x, ...){

#' @describeIn is.sorted.integer64 returns the number of unique values
#' @export
nunique.integer64 <- function(x, ...){
nunique.integer64 <- function(x, ...) {
env <- cache(x)
if (is.null(env))
has.cache <- FALSE
else if (exists("nunique", envir=env, inherits=FALSE))
return(get("nunique", envir=env, inherits=FALSE))
else # nolint: unreachable_code_linter. TODO(r-lib/lintr#2710): Re-enable.
has.cache <- TRUE
if (is.sorted(x)){
if (is.sorted(x)) {
ret <- .Call(C_r_ram_integer64_sortnut
, x = x
, PACKAGE = "bit64"
)
if (has.cache){
if (has.cache) {
assign("nunique", ret[1L], envir=env)
assign("nties", ret[2L], envir=env)
}
ret[1L]
}else{
} else {
h <- hashmap(x)
if (has.cache)
assign("nunique", h$nunique, envir=env)
Expand All @@ -381,15 +380,15 @@ nunique.integer64 <- function(x, ...){

#' @describeIn is.sorted.integer64 returns the number of tied values.
#' @export
nties.integer64 <- function(x, ...){
nties.integer64 <- function(x, ...) {
cv <- getcache(x, "nties")
if (is.null(cv)){
if (is.sorted(x)){
if (is.null(cv)) {
if (is.sorted(x)) {
cv <- .Call(C_r_ram_integer64_sortnut
, x = x
, PACKAGE = "bit64"
)[2L]
}else{
} else {
s <- clone(x)
# nolint next: object_usage_linter. Keep the output of in-place ramsort for debugging.
na.count <-
Expand Down
4 changes: 2 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
#' oldpar <- par(no.readonly = TRUE)
#' par(mfrow=c(2,1))
#' par(cex=0.7)
#' for (i in 1:nrow(optimizer64.data)){
#' for (j in 1:2){
#' for (i in 1:nrow(optimizer64.data)) {
#' for (j in 1:2) {
#' tim <- optimizer64.data[[i,j]]
#' barplot(t(tim))
#' if (rownames(optimizer64.data)[i]=="match")
Expand Down
Loading

0 comments on commit 36160cb

Please sign in to comment.