r-lib / lintr

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

lintr does not like for loop bodies with a single line and no braces #2564

Open rlaboiss opened 2 months ago

rlaboiss commented 2 months ago

R accepts single-line bodies, without curly braces, for theif and the for statements.

Is there any reason why lintr accepts that for if but not for for? For instance:

> lint("if (TRUE)\n  x")
> lint("for (i in 0)\n  x")
<text>:2:2: style: [indentation_linter] Indentation should be 0 spaces but is 2 spaces.
IndrajeetPatil commented 2 months ago

There is definitely an inconsistency here. Thanks for pointing out.

cat("if (TRUE)\n  x")
#> if (TRUE)
#>   x
cat("for (i in 0)\n  x")
#> for (i in 0)
#>   x
cat("while (TRUE)\n  x")
#> while (TRUE)
#>   x

lintr::lint(text= "if (TRUE)\n  x", linters = lintr::indentation_linter())
lintr::lint(text= "for (i in 0)\n  x", linters = lintr::indentation_linter())
#> <text>:2:2: style: [indentation_linter] Indentation should be 0 spaces but is 2 spaces.
#>   x
#> ~^
lintr::lint(text= "while (TRUE)\n  x", linters = lintr::indentation_linter())

Created on 2024-04-23 with reprex v2.1.0