Skip to content

Commit 8f0f55d

Browse files
don't allow ranges[1]==0 (#2551)
1 parent 36f668e commit 8f0f55d

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* `object_name_linter()` no longer errors when user-supplied `regexes=` have capture groups (#2188, @MichaelChirico).
2222
* `.lintr` config validation correctly accepts regular expressions which only compile under `perl = TRUE` (#2375, @MichaelChirico). These have always been valid (since `rex::re_matches()`, which powers the lint exclusion logic, also uses this setting), but the new up-front validation in v3.1.1 incorrectly used `perl = FALSE`.
2323
* `.lintr` configs set by option `lintr.linter_file` or environment variable `R_LINTR_LINTER_FILE` can point to subdirectories (#2512, @MichaelChirico).
24+
* `indentation_linter()` returns `ranges[1L]==1L` when the offending line has 0 spaces (#2550, @MichaelChirico).
2425

2526
## Changes to default linters
2627

R/indentation_linter.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,13 @@ indentation_linter <- function(indent = 2L, hanging_indent_style = c("tidy", "al
276276
)
277277
lint_lines <- unname(as.integer(names(source_expression$file_lines)[bad_lines]))
278278
lint_ranges <- cbind(
279-
pmin(expected_indent_levels[bad_lines] + 1L, indent_levels[bad_lines]),
279+
# when indent_levels==0, need to start ranges at column 1.
280+
pmax(
281+
pmin(expected_indent_levels[bad_lines] + 1L, indent_levels[bad_lines]),
282+
1L
283+
),
280284
# If the expected indent is larger than the current line width, the lint range would become invalid.
281-
# Therefor, limit range end to end of line.
285+
# Therefore, limit range end to end of line.
282286
pmin(
283287
pmax(expected_indent_levels[bad_lines], indent_levels[bad_lines]),
284288
nchar(source_expression$file_lines[bad_lines]) + 1L

tests/testthat/test-indentation_linter.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,15 @@ test_that("function shorthand is handled", {
867867
linter
868868
)
869869
})
870+
871+
test_that("lint metadata works for 0-space case", {
872+
expect_lint(
873+
trim_some("
874+
if (TRUE) {
875+
FALSE
876+
}
877+
"),
878+
list(ranges = list(1L:2L)),
879+
indentation_linter()
880+
)
881+
})

0 commit comments

Comments
 (0)