r-lib / lintr

Static Code Analysis for R
https://lintr.r-lib.org
Other
1.19k stars 184 forks source link

indentation_linter disagrees with styler indentation #2034

Open dfsnow opened 1 year ago

dfsnow commented 1 year ago

Possibly related to: https://github.com/r-lib/lintr/issues/2011#issuecomment-1661957104.

indentation_linter and styler do not agree in some cases using long column names inside a function.

Reprex dplyr

library(dplyr)

mtcars %>%
  rename(
    big_long_prefix_num_cylinders = cyl,
    big_long_prefix_horsepower = hp,
    big_long_prefix_weight = wt,
    big_long_prefix_automatic = am
  ) %>%
  mutate(
    big_long_column_name_that_cant_fit_in_col_limit =
      mpg == (big_long_prefix_num_cylinders * big_long_prefix_horsepower) &
        (big_long_prefix_weight * big_long_prefix_automatic) # styler formats to this
  )

This configuration results in the lintr error:

[indentation_linter] Indentation should be 6 spaces but is 8 spaces.

In other words, lintr wants the format below, which styler will auto-fix:

  mutate(
    big_long_column_name_that_cant_fit_in_col_limit =
      mpg == (big_long_prefix_num_cylinders * big_long_prefix_horsepower) &
      (big_long_prefix_weight * big_long_prefix_automatic) # lintr wants this
  )

Reprex data.table

Here's another example using data.table:

library(data.table)
library(dplyr)

mtcars2 <- mtcars %>%
  rename(
    big_long_prefix_num_cylinders = cyl,
    big_long_prefix_horsepower = hp,
    big_long_prefix_weight = wt,
    big_long_prefix_automatic = am
  )

mtcars2[, big_long_column_name_that_cant_fit_in_col_limit :=
  round(big_long_prefix_num_cylinders - big_long_prefix_horsepower, 2)] # styler wants this

lintr throws an error:

[indentation_linter] Indentation should be 9 spaces but is 2 spaces.

lintr wants this format, which styler will auto-fix:

mtcars[, big_long_column_name_that_cant_fit_in_col_limit :=
    round(big_long_prefix_num_cylinders - big_long_prefix_horsepower, 2)
]
SessionInfo
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] styler_1.10.1     lintr_3.1.0       dplyr_1.1.2       data.table_1.14.8

loaded via a namespace (and not attached):
 [1] xmlparsedata_1.0.5 pillar_1.9.0       compiler_4.2.2     R.methodsS3_1.8.2  R.utils_2.12.2     remotes_2.4.2     
 [7] tools_4.2.2        digest_0.6.33      lifecycle_1.0.3    tibble_3.2.1       R.cache_0.16.0     pkgconfig_2.0.3   
[13] rlang_1.1.1        rex_1.2.1          cli_3.6.1          rstudioapi_0.15.0  cyclocomp_1.1.0    withr_2.5.0       
[19] xml2_1.3.5         generics_0.1.3     desc_1.4.2         vctrs_0.6.3        rprojroot_2.0.3    tidyselect_1.2.0  
[25] glue_1.6.2         R6_2.5.1           processx_3.8.2     fansi_1.0.4        callr_3.7.3        purrr_1.0.1       
[31] magrittr_2.0.3     backports_1.4.1    ps_1.7.5           utf8_1.2.3         lazyeval_0.2.2     crayon_1.5.2      
[37] R.oo_1.25.0   
  
AshesITR commented 1 year ago

FWIW, here is what both {lintr} and {styler} can agree to:

library(dplyr)

mtcars %>%
  rename(
    big_long_prefix_num_cylinders = cyl,
    big_long_prefix_horsepower = hp,
    big_long_prefix_weight = wt,
    big_long_prefix_automatic = am
  ) %>%
  mutate(
    big_long_column_name_that_cant_fit_in_col_limit = {
      mpg == (big_long_prefix_num_cylinders * big_long_prefix_horsepower) &
        (big_long_prefix_weight * big_long_prefix_automatic)
    }
  )
mtcars[
  ,
  big_long_column_name_that_cant_fit_in_col_limit :=
    round(big_long_prefix_num_cylinders - big_long_prefix_horsepower, 2)
]
aramirezreyes commented 1 year ago

Related to #2007

danielinteractive commented 1 year ago

Just wanted to say thanks for looking into these consistency issues, very important work! Just ran into the inconsistencies and for now need to disable the indentation linter to avoid too much work ...