Skip to content

Add a fuzzer for injecting comments #2899

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

Draft
wants to merge 136 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
3882350
use maybe_write_content for easier 'mocking'
MichaelChirico Mar 5, 2025
c392c53
initial progress
MichaelChirico Mar 6, 2025
5cef281
getting very close i think...
MichaelChirico Mar 6, 2025
a4e4a66
skip Rmd files
MichaelChirico Mar 6, 2025
0b1eaf5
caught a live one!
MichaelChirico Mar 6, 2025
868ad30
need to match original file extension?
MichaelChirico Mar 6, 2025
0ed5cc0
caught another one!
MichaelChirico Mar 6, 2025
99d00a3
simpler approach, avoid rex() due to bug
MichaelChirico Mar 6, 2025
d3cca7a
also ignore warnings
MichaelChirico Mar 6, 2025
59dc1b0
finally getting somewhere...
MichaelChirico Mar 6, 2025
a25065f
progressively more complicated :(
MichaelChirico Mar 6, 2025
491a340
round of fixes & first working nofuzz
MichaelChirico Mar 6, 2025
92f0628
looks like we got another live one... break time
MichaelChirico Mar 6, 2025
d387a71
another true positive
MichaelChirico Mar 6, 2025
e150ffe
more ignores, need '.' in file extension, restore test
MichaelChirico Mar 6, 2025
3d1fc0e
wrapping up
MichaelChirico Mar 6, 2025
b69b7cd
Write up the GHA config
MichaelChirico Mar 6, 2025
b8a06e3
annotation
MichaelChirico Mar 6, 2025
a3dbf27
comment for future work
MichaelChirico Mar 6, 2025
5a22050
vestigial
MichaelChirico Mar 6, 2025
76b869f
skips on old R
MichaelChirico Mar 6, 2025
afec743
expect_no_lint
MichaelChirico Mar 6, 2025
51593e4
new tests
MichaelChirico Mar 6, 2025
f4b9481
NEWS
MichaelChirico Mar 6, 2025
6389d55
bad copy-paste
MichaelChirico Mar 6, 2025
1550ead
need stop_on_failure for batch?
MichaelChirico Mar 6, 2025
bbdac43
delint, fix last skip for R<4.1.0
MichaelChirico Mar 6, 2025
523c218
more extensible structure
MichaelChirico Mar 7, 2025
852d0ea
expect_no_lint
MichaelChirico Mar 7, 2025
3eb21ca
progress, incl. many 'nofuzz' & 'no_lint'
MichaelChirico Mar 7, 2025
8059091
another round of nofuzz
MichaelChirico Mar 7, 2025
df8cccc
another batch
MichaelChirico Mar 7, 2025
138e9cc
tweak
MichaelChirico Mar 7, 2025
683c461
another nofuzz case, attempting to reduce nofuzz requirements
MichaelChirico Mar 7, 2025
98086d4
fix; scale back nofuzz for an attempt
MichaelChirico Mar 7, 2025
fe88c59
reinstate more legit nofuzz
MichaelChirico Mar 7, 2025
1928831
general fix for issue of S4 method calls under @
MichaelChirico Mar 7, 2025
168ee65
fix missed S4 extractions looking for preamble
MichaelChirico Mar 7, 2025
413e029
expect_no_lint
MichaelChirico Mar 7, 2025
c7cc8ac
handle @ equivalency
MichaelChirico Mar 7, 2025
4d3ff4f
fix an equivalency issue in indentation_linter
MichaelChirico Mar 7, 2025
1d8869f
new simple swap fuzzer, some nofuzz
MichaelChirico Mar 7, 2025
3d106d7
add some vectorization to make debugging easier
MichaelChirico Mar 7, 2025
1d19687
another indentation inconsistency
MichaelChirico Mar 7, 2025
dbfaf5c
nofuzz'ing
MichaelChirico Mar 7, 2025
88117cb
complete NEWS
MichaelChirico Mar 7, 2025
ee611cb
more expect_no_lint
MichaelChirico Mar 7, 2025
aa756e6
more expect_no_lint
MichaelChirico Mar 7, 2025
c3a99ce
add tests of include_s4_slots
MichaelChirico Mar 7, 2025
5893b8c
initial try, let's see
MichaelChirico Mar 7, 2025
8a83c01
adversarial comment protection
MichaelChirico Mar 7, 2025
836bf31
expect_no_lint
MichaelChirico Mar 7, 2025
f0974d9
caught true false positive
MichaelChirico Mar 7, 2025
a0dc171
some nofuzz
MichaelChirico Mar 7, 2025
4fc370d
don't break up calls; report the actual content
MichaelChirico Mar 7, 2025
c130bf4
nofuzz
MichaelChirico Mar 7, 2025
fc097ba
fix another one
MichaelChirico Mar 7, 2025
092f98e
fix another one
MichaelChirico Mar 8, 2025
4c08d49
another real fix
MichaelChirico Mar 8, 2025
7b2a16d
nofuzz
MichaelChirico Mar 8, 2025
78bdc12
no hope for brace_linter; use a space before comment for infix_spaces
MichaelChirico Mar 8, 2025
6446bbb
another real fix
MichaelChirico Mar 8, 2025
d256ccb
expect_no_lint
MichaelChirico Mar 8, 2025
f4e53cb
another real fix
MichaelChirico Mar 8, 2025
f817c77
expect_no_lint
MichaelChirico Mar 8, 2025
3f817a7
expect_no_lint, stylistic touch-up
MichaelChirico Mar 8, 2025
0fa5647
style touch-up (no message=)
MichaelChirico Mar 8, 2025
c2b18ca
nofuzz
MichaelChirico Mar 8, 2025
24f32a4
expect_no_lint
MichaelChirico Mar 8, 2025
c7530e6
more true fixes
MichaelChirico Mar 8, 2025
0ed031d
tidy
MichaelChirico Mar 8, 2025
ccc0f89
tidy2
MichaelChirico Mar 8, 2025
6c090b7
nofuzz, expect_no_lint
MichaelChirico Mar 8, 2025
cd7052b
a real bear here, fixed
MichaelChirico Mar 8, 2025
4326789
another true fix
MichaelChirico Mar 8, 2025
35e7708
bug fix
MichaelChirico Mar 8, 2025
218e656
more nofuzz, another true fix
MichaelChirico Mar 8, 2025
29a6232
another nofuzz, another true fix
MichaelChirico Mar 8, 2025
85fd53e
another case requiring stripping comments, more nofuzz
MichaelChirico Mar 8, 2025
7ae6552
fix yet another, nofuzz
MichaelChirico Mar 8, 2025
c750079
another fix
MichaelChirico Mar 8, 2025
7cede8e
closer & closer: another
MichaelChirico Mar 8, 2025
bacce08
found the MRE, not fixed yet
MichaelChirico Mar 8, 2025
edfe6d3
what eldritch horrors...
MichaelChirico Mar 8, 2025
06b2047
the easier fixes continue
MichaelChirico Mar 8, 2025
de0bf4a
modernize the test file first to apply nofuzz next
MichaelChirico Mar 8, 2025
15d3a50
further tweak, nofuzz
MichaelChirico Mar 8, 2025
7699a7c
ban the '*'
MichaelChirico Mar 8, 2025
4c38b90
node equality tests are going to be a pain...
MichaelChirico Mar 8, 2025
68ab08d
another one requiring a tree copy
MichaelChirico Mar 8, 2025
aad82de
nofuzz
MichaelChirico Mar 8, 2025
9a508b1
*[2]
MichaelChirico Mar 8, 2025
0ad134c
skip another file
MichaelChirico Mar 8, 2025
3929a43
preceding-sibling::*
MichaelChirico Mar 9, 2025
e57707e
improve handling of ifelse_censor, add a new test
MichaelChirico Mar 9, 2025
72164ee
more comments interfering with node=node tests
MichaelChirico Mar 9, 2025
ac774a3
simpler preceding-sibling::* fix
MichaelChirico Mar 9, 2025
173e932
more nofuzz, more preceding-sibling::*
MichaelChirico Mar 9, 2025
fcf7cf7
tricky tricky
MichaelChirico Mar 9, 2025
025537b
hit the dj khaled
MichaelChirico Mar 9, 2025
f0c8c9a
another tricky one
MichaelChirico Mar 9, 2025
ebe7936
kitchen sink
MichaelChirico Mar 9, 2025
04d4e1b
new one, old rule
MichaelChirico Mar 9, 2025
a7c9768
just drop formulas
MichaelChirico Mar 9, 2025
e1a497c
edge case in seq_linter
MichaelChirico Mar 9, 2025
3d11316
another sprintf case
MichaelChirico Mar 9, 2025
a9dca0c
re-fixed unreachable code thing?
MichaelChirico Mar 9, 2025
2a0831d
more nofuzz, and finally fixed the conjunct_test issue
MichaelChirico Mar 9, 2025
9344004
unbelievably tricky
MichaelChirico Mar 10, 2025
f53ec50
need one further level up here too
MichaelChirico Mar 10, 2025
5d61675
one more easy one
MichaelChirico Mar 10, 2025
dd08aa2
another tricky one
MichaelChirico Mar 10, 2025
b96f2b1
trickier handling needed
MichaelChirico Mar 10, 2025
ff083c2
for future reference
MichaelChirico Mar 10, 2025
88f998c
blessed oversight
MichaelChirico Mar 10, 2025
5606eac
vestigial
MichaelChirico Mar 10, 2025
49872bf
expect_no_lint
MichaelChirico Mar 10, 2025
3494010
leftovers
MichaelChirico Mar 10, 2025
0f02cc5
expect_no_lint in all touched files
MichaelChirico Mar 10, 2025
aa3e930
start the NEWS
MichaelChirico Mar 10, 2025
18f32d3
cite all changed linters
MichaelChirico Mar 10, 2025
2907c86
annotate reference issues where noteworthy
MichaelChirico Mar 10, 2025
a42bda5
cleanup
MichaelChirico Mar 10, 2025
1275cca
Merge branch 'main' into fuzz-dollar
AshesITR Jul 24, 2025
ee9b446
remove empty line
AshesITR Jul 24, 2025
50b8832
revert
MichaelChirico Jul 24, 2025
4f95e40
revert
MichaelChirico Jul 24, 2025
ebab604
revert
MichaelChirico Jul 24, 2025
2a1ebe2
failed merge?
MichaelChirico Jul 24, 2025
4870ecb
narrow line
MichaelChirico Jul 24, 2025
83aa12e
trailing ws
MichaelChirico Jul 24, 2025
53148c6
Merge branch 'fuzz-dollar' into fuzz-comments
MichaelChirico Jul 25, 2025
2840d7b
Merge branch 'main' into fuzz-comments
MichaelChirico Jul 26, 2025
5d61a1e
Merge branch 'main' into fuzz-comments
MichaelChirico Jul 26, 2025
2392fa4
missed expect_comparison_linter
MichaelChirico Jul 26, 2025
5490c64
Merge branch 'main' into fuzz-comments
MichaelChirico Jul 28, 2025
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
27 changes: 26 additions & 1 deletion .dev/ast_fuzz_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ writeLines(
),
expect_lint_file
)

# Ensure the fuzzed contents are always visible to facilitate backing out which fuzzed content is at issue
contents <- readLines(expect_lint_file)
wrong_number_def_idx <- grep('wrong_number_fmt <- "got %d lints instead of %d%s"', contents, fixed = TRUE)
wrong_number_use_idx <- grep("sprintf(wrong_number_fmt,", contents, fixed = TRUE)
if (
length(wrong_number_def_idx) != 1L ||
length(wrong_number_use_idx) == 0L ||
# these lines should be self-contained & have no comments
!all(endsWith(contents[wrong_number_use_idx], ")")) ||
inherits(tryCatch(parse(text = contents[wrong_number_use_idx]), error = identity), "error")
) {
stop(sprintf(
"Please update this workflow -- need wrong_number_fmt to be easily replaced in file '%s'.",
expect_lint_file
))
}

contents[wrong_number_def_idx] <-
'wrong_number_fmt <- "got %d lints instead of %d%s\\nFile contents:\\n%s"'
contents[wrong_number_use_idx] <-
gsub("\\)$", ", readChar(file, file.size(file)))", contents[wrong_number_use_idx])
writeLines(contents, expect_lint_file)

# Not useful in CI but good when running locally.
withr::defer({
writeLines(original, expect_lint_file)
Expand Down Expand Up @@ -116,7 +140,8 @@ failures <- reporter$failures$as_list()
valid_failure <- vapply(
failures,
function(failure) {
if (grepl("(column_number|ranges|line) .* did not match", failure$message)) {
# line_number is for the comment injection fuzzer, which adds newlines.
if (grepl("(column_number|ranges|line|line_number) .* did not match", failure$message)) {
return(TRUE)
}
FALSE
Expand Down
30 changes: 29 additions & 1 deletion .dev/maybe_fuzz_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ maybe_fuzz_content <- function(file, lines) {
file.copy(file, new_file, copy.mode = FALSE)
}

apply_fuzzers(new_file, list(function_lambda_fuzzer, pipe_fuzzer, dollar_at_fuzzer))
apply_fuzzers(new_file, list(function_lambda_fuzzer, pipe_fuzzer, dollar_at_fuzzer, comment_injection_fuzzer))

new_file
}
Expand Down Expand Up @@ -59,6 +59,34 @@ dollar_at_fuzzer <- simple_swap_fuzzer(
replacements = c("$", "@")
)

comment_injection_fuzzer <- function(pd, lines) {
# injecting comment before a call often structurally breaks parsing
# (SYMBOL_FUNCTION_CALL-->SYMBOL), so avoid
terminal_token_idx <- which(pd$terminal & !pd$token %in% c("COMMENT", "SYMBOL_FUNCTION_CALL", "SLOT"))
# formula is messy because it's very easy to break parsing, but not easy to exclude the right
# elements from the pd data.frame (easier with XPath ancestor axis). Just skip for now.
if (any(pd$token == "'~'")) {
return(invisible())
}
injection_count <- sample(0:length(terminal_token_idx), 1L)

if (injection_count == 0L) {
return(invisible())
}

terminal_token_idx <- sort(sample(terminal_token_idx, injection_count))

for (ii in rev(terminal_token_idx)) {
line <- lines[pd$line2[ii]]
lines[pd$line2[ii]] <- paste0(
substr(line, 1L, pd$col2[ii]),
" # INJECTED COMMENT\n",
substr(line, pd$col2[ii] + 1L, nchar(line))
)
}
lines
}

# we could also consider just passing any test where no fuzzing takes place,
# i.e. letting the other GHA handle whether unfuzzed tests pass as expected.
apply_fuzzers <- function(f, fuzzers) {
Expand Down
32 changes: 32 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@
* `object_usage_linter()` lints missing packages that may cause false positives (#2872, @AshesITR)
* New argument `include_s4_slots` for the `xml_find_function_calls()` entry in the `get_source_expressions()` to govern whether calls of the form `s4Obj@fun()` are included in the result (#2820, @MichaelChirico).
* `sprintf_linter()` lints `sprintf()` and `gettextf()` calls when a constant string is passed to `fmt` (#2894, @Bisaloo).
* General handling of logic around where comments can appear in code has been improved (#2822, @MichaelChirico). In many cases, this is a tiny robustness fix for weird edge cases unlikely to be found in practice, but in others, this improves practical linter precision (reduced false positives and/or false negatives). The affected linters (with annotations for changes noteworthy enough to have gotten a dedicated bug) are:
+ `brace_linter()`
+ `coalesce_linter()`
+ `comparison_negation_linter()` #2826
+ `conjunct_test_linter()` #2827
+ `empty_assignment_linter()`
+ `expect_comparison_linter()`
+ `fixed_regex_linter()` #2827
+ `if_switch_linter()`
+ `ifelse_censor_linter()` #2826
+ `implicit_assignment_linter()`
+ `length_test_linter()`
+ `literal_coercion_linter()` #2824
+ `matrix_apply_linter()` #2825
+ `nzchar_linter()` #2826
+ `object_length_linter()` #2827
+ `object_name_linter()` #2827
+ `object_usage_linter()`
+ `outer_negation_linter()` #2827
+ `redundant_equals_linter()`
+ `regex_subset_linter()`
+ `seq_linter()`
+ `sort_linter()`
+ `sprintf_linter()` #2827
+ `string_boundary_linter()`
+ `strings_as_factors_linter()`
+ `unnecessary_concatenation_linter()` #2827
+ `unnecessary_lambda_linter()` #2827
+ `unnecessary_nesting_linter()` #2827
+ `unnecessary_placeholder_linter()`
+ `unreachable_code_linter()` #2827
+ `vector_logic_linter()` #2826

### New linters

Expand Down
2 changes: 1 addition & 1 deletion R/brace_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ brace_linter <- function(allow_single_line = FALSE,
{ xp_cond_closed }
and (
(@line1 = preceding-sibling::*[1][not(self::OP-LEFT-BRACE)]/@line2)
or (@line1 = parent::expr/following-sibling::*[1][not(self::ELSE)]/@line1)
or (@line1 = parent::expr/following-sibling::*[not(self::COMMENT)][1][not(self::ELSE)]/@line1)
)
]")

Expand Down
28 changes: 14 additions & 14 deletions R/coalesce_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,33 @@
coalesce_linter <- function() {
braced_expr_cond <- "expr[1][OP-LEFT-BRACE and count(*) = 3]/expr"
xpath <- glue("
parent::expr[
expr[expr[
preceding-sibling::IF
and (
expr[2] = following-sibling::ELSE/following-sibling::expr
or expr[2] = following-sibling::ELSE/following-sibling::{braced_expr_cond}
or expr[2][LEFT_ASSIGN]/expr[1] = following-sibling::ELSE/following-sibling::expr
or expr[2][LEFT_ASSIGN]/expr[1] = following-sibling::ELSE/following-sibling::{braced_expr_cond}
)
]
/parent::expr
]]
|
parent::expr[
preceding-sibling::OP-EXCLAMATION
and parent::expr/preceding-sibling::IF
self::*[expr[
preceding-sibling::IF
and OP-EXCLAMATION
and (
expr[2] = parent::expr/following-sibling::expr[1]
or expr[2] = parent::expr/following-sibling::{braced_expr_cond}
or expr[2][LEFT_ASSIGN]/expr[1] = parent::expr/following-sibling::expr[1]
or expr[2][LEFT_ASSIGN]/expr[1] = parent::expr/following-sibling::{braced_expr_cond}
expr/expr[2] = following-sibling::expr[1]
or expr/expr[2] = following-sibling::{braced_expr_cond}
or expr/expr[2][LEFT_ASSIGN]/expr[1] = following-sibling::expr[1]
or expr/expr[2][LEFT_ASSIGN]/expr[1] = following-sibling::{braced_expr_cond}
)
]
/parent::expr
/parent::expr
]]
")

Linter(linter_level = "expression", function(source_expression) {
null_calls <- source_expression$xml_find_function_calls("is.null")
null_calls <- xml_parent(xml_parent(xml_parent(
source_expression$xml_find_function_calls("is.null")
)))
null_calls <- strip_comments_from_subtree(null_calls)
bad_expr <- xml_find_all(null_calls, xpath)
is_negation <- !is.na(xml_find_first(bad_expr, "expr/OP-EXCLAMATION"))
observed <- ifelse(is_negation, "if (!is.null(x)) x else y", "if (is.null(x)) y else x")
Expand Down
2 changes: 1 addition & 1 deletion R/empty_assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
empty_assignment_linter <- make_linter_from_xpath(
# for some reason, the parent in the `=` case is <equal_assign>, not <expr>, hence parent::expr
xpath = "
//OP-LEFT-BRACE[following-sibling::*[1][self::OP-RIGHT-BRACE]]
//OP-LEFT-BRACE[following-sibling::*[not(self::COMMENT)][1][self::OP-RIGHT-BRACE]]
/parent::expr[
preceding-sibling::LEFT_ASSIGN
or preceding-sibling::EQ_ASSIGN
Expand Down
2 changes: 1 addition & 1 deletion R/expect_comparison_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ expect_comparison_linter <- function() {
xml_calls <- source_expression$xml_find_function_calls("expect_true")
bad_expr <- xml_find_all(xml_calls, xpath)

comparator <- xml_find_chr(bad_expr, "string(expr[2]/*[2])")
comparator <- xml_find_chr(bad_expr, "string(expr[2]/*[not(self::COMMENT)][2])")
expectation <- comparator_expectation_map[comparator]
lint_message <- sprintf("%s(x, y) is better than expect_true(x %s y).", expectation, comparator)
xml_nodes_to_lints(bad_expr, source_expression, lint_message = lint_message, type = "warning")
Expand Down
19 changes: 12 additions & 7 deletions R/if_switch_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@
# NB: IF AND {...} AND ELSE/... implies >= 3 equality conditions are present
# .//expr/IF/...: the expr in `==` that's _not_ the STR_CONST
# not(preceding::IF): prevent nested matches which might be incorrect globally
# not(. != .): don't match if there are _any_ expr which _don't_ match the top
# expr
if_xpath <- glue("
//IF
/parent::expr[
Expand All @@ -203,21 +201,28 @@
and {equal_str_cond}
and ELSE/following-sibling::expr[IF and {equal_str_cond}]
]
and not(
.//expr/IF/following-sibling::{equal_str_cond}/expr[not(STR_CONST)]
!= expr[1][EQ]/expr[not(STR_CONST)]
)
and not({ max_lines_cond })
]
")

# not(. != .): don't match if there are _any_ expr which _don't_ match the top expr
# do this as a second step to

Check warning on line 209 in R/if_switch_linter.R

View workflow job for this annotation

GitHub Actions / lint

file=R/if_switch_linter.R,line=209,col=34,[trailing_whitespace_linter] Remove trailing whitespace.
equality_test_cond <- glue("self::*[
.//expr/IF/following-sibling::{equal_str_cond}/expr[not(STR_CONST)]
!= expr[1][EQ]/expr[not(STR_CONST)]
]")

Linter(linter_level = "expression", function(source_expression) {
xml <- source_expression$xml_parsed_content

bad_expr <- xml_find_all(xml, if_xpath)
expr_all_equal <- is.na(xml_find_first(
strip_comments_from_subtree(bad_expr),
equality_test_cond
))

lints <- xml_nodes_to_lints(
bad_expr,
bad_expr[expr_all_equal],
source_expression = source_expression,
lint_message = paste(
"Prefer switch() statements over repeated if/else equality tests,",
Expand Down
4 changes: 2 additions & 2 deletions R/implicit_assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ implicit_assignment_linter <- function(except = c("bquote", "expression", "expr"
xpath <- glue("
({assignments})
/parent::expr[
preceding-sibling::*[2][self::IF or self::WHILE]
preceding-sibling::*[not(self::COMMENT)][2][self::IF or self::WHILE]
or parent::forcond
or preceding-sibling::expr/{xpath_exceptions}
or parent::expr/*[1][self::OP-LEFT-PAREN]
Expand All @@ -94,7 +94,7 @@ implicit_assignment_linter <- function(except = c("bquote", "expression", "expr"
}
if (allow_scoped) {
# force 2nd preceding to ensure we're in the loop condition, not the loop expression
in_branch_cond <- "ancestor::expr[preceding-sibling::*[2][self::IF or self::WHILE]]"
in_branch_cond <- "ancestor::expr[preceding-sibling::*[not(self::COMMENT)][2][self::IF or self::WHILE]]"
xpath <- paste0(
xpath,
# _if_ we're in an IF/WHILE branch, lint if the assigned SYMBOL appears anywhere later on.
Expand Down
7 changes: 6 additions & 1 deletion R/length_test_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ length_test_linter <- function() {
Linter(linter_level = "expression", function(source_expression) {
xml_calls <- source_expression$xml_find_function_calls("length")
bad_expr <- xml_find_all(xml_calls, xpath)
bad_expr <- strip_comments_from_subtree(bad_expr)

expr_parts <- vapply(lapply(bad_expr, xml_find_all, "expr[2]/*"), xml_text, character(3L))
expr_parts <- vapply(
lapply(bad_expr, xml_find_all, "expr[2]/*[not(self::COMMENT)]"),
xml_text,
character(3L)
)
lint_message <- sprintf(
"Checking the length of a logical vector is likely a mistake. Did you mean `length(%s) %s %s`?",
expr_parts[1L, ], expr_parts[2L, ], expr_parts[3L, ]
Expand Down
30 changes: 20 additions & 10 deletions R/object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ object_usage_linter <- function(interpret_glue = NULL, interpret_extensions = c(
# NB: the repeated expr[2][FUNCTION] XPath has no performance impact, so the different direct assignment XPaths are
# split for better readability, see PR#1197
# TODO(#1106): use //[...] to capture assignments in more scopes
xpath_function_assignment <- "
expr[LEFT_ASSIGN or EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]
| expr_or_assign_or_help[EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]
| equal_assign[EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]
| //SYMBOL_FUNCTION_CALL[text() = 'assign']/parent::expr/following-sibling::expr[2][FUNCTION or OP-LAMBDA]
| //SYMBOL_FUNCTION_CALL[text() = 'setMethod']/parent::expr/following-sibling::expr[3][FUNCTION or OP-LAMBDA]
"
fun_node <- "FUNCTION or OP-LAMBDA"
xpath_function_assignment <- glue("
expr[LEFT_ASSIGN or EQ_ASSIGN]/expr[2][{fun_node}]
| expr_or_assign_or_help[EQ_ASSIGN]/expr[2][{fun_node}]
| equal_assign[EQ_ASSIGN]/expr[2][{fun_node}]
| //SYMBOL_FUNCTION_CALL[text() = 'assign']/parent::expr/following-sibling::expr[2][{fun_node}]
| //SYMBOL_FUNCTION_CALL[text() = 'setMethod']/parent::expr/following-sibling::expr[3][{fun_node}]
")

# code like:content
# foo <- \ #comment
# (x) x
# is technically valid, but won't parse unless the lambda is in a bigger expression (here '<-').
# the same doesn't apply to 'function'.
xpath_unsafe_lambda <- "OP-LAMBDA[@line1 = following-sibling::*[1][self::COMMENT]/@line1]"

# not all instances of linted symbols are potential sources for the observed violations -- see #1914
symbol_exclude_cond <- "preceding-sibling::OP-DOLLAR or preceding-sibling::OP-AT or ancestor::expr[OP-TILDE]"
Expand Down Expand Up @@ -100,7 +108,9 @@ object_usage_linter <- function(interpret_glue = NULL, interpret_extensions = c(
fun_assignments <- xml_find_all(xml, xpath_function_assignment)

lapply(fun_assignments, function(fun_assignment) {
code <- get_content(lines = source_expression$content, fun_assignment)
# this will mess with the source line numbers. but I don't think anybody cares.
known_safe <- is.na(xml_find_first(fun_assignment, xpath_unsafe_lambda))
code <- get_content(lines = source_expression$content, fun_assignment, known_safe = known_safe)
fun <- try_silently(eval(
envir = env,
parse(
Expand Down Expand Up @@ -190,8 +200,8 @@ get_assignment_symbols <- function(xml) {
expr[RIGHT_ASSIGN]/expr[2]/SYMBOL[1] |
equal_assign/expr[1]/SYMBOL[1] |
expr_or_assign_or_help/expr[1]/SYMBOL[1] |
expr[expr[1][SYMBOL_FUNCTION_CALL/text()='assign']]/expr[2]/* |
expr[expr[1][SYMBOL_FUNCTION_CALL/text()='setMethod']]/expr[2]/*
expr[expr[1][SYMBOL_FUNCTION_CALL/text() = 'assign']]/expr[2]/* |
expr[expr[1][SYMBOL_FUNCTION_CALL/text() = 'setMethod']]/expr[2]/*
"
))
}
Expand Down
2 changes: 1 addition & 1 deletion R/redundant_equals_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ redundant_equals_linter <- function() {
xml <- source_expression$xml_parsed_content

bad_expr <- xml_find_all(xml, xpath)
op <- xml_text(xml_find_first(bad_expr, "*[2]"))
op <- xml_text(xml_find_first(bad_expr, "*[not(self::COMMENT)][2]"))

xml_nodes_to_lints(
bad_expr,
Expand Down
27 changes: 14 additions & 13 deletions R/regex_subset_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,23 @@
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
regex_subset_linter <- function() {
# parent::expr for LEFT_ASSIGN and RIGHT_ASSIGN, but, strangely,
# parent::equal_assign for EQ_ASSIGN. So just use * as a catchall.
# See https://www.w3.org/TR/1999/REC-xpath-19991116/#booleans;
# equality of nodes is based on the string value of the nodes, which
# is basically what we need, i.e., whatever expression comes in
# <expr>[grepl(pattern, <expr>)] matches exactly, e.g. names(x)[grepl(ptn, names(x))].
xpath_fmt <- "
parent::expr[
parent::expr[
self::*[
not(LEFT_ASSIGN or EQ_ASSIGN or RIGHT_ASSIGN)
]
/expr[
OP-LEFT-BRACKET
and not(parent::*[LEFT_ASSIGN or EQ_ASSIGN or RIGHT_ASSIGN])
and expr[1] = expr/expr[position() = {arg_pos} ]
]
and expr[position() = {arg_pos} ] = parent::expr/expr[1]
]"
"
grep_xpath <- glue(xpath_fmt, arg_pos = 3L)
stringr_xpath <- glue(xpath_fmt, arg_pos = 2L)

Linter(linter_level = "expression", function(source_expression) {
grep_calls <- source_expression$xml_find_function_calls(c("grepl", "grep"))
grep_calls <- xml_parent(xml_parent(xml_parent(
source_expression$xml_find_function_calls(c("grepl", "grep"))
)))
grep_calls <- strip_comments_from_subtree(grep_calls)
grep_expr <- xml_find_all(grep_calls, grep_xpath)

grep_lints <- xml_nodes_to_lints(
Expand All @@ -78,7 +76,10 @@ regex_subset_linter <- function() {
type = "warning"
)

stringr_calls <- source_expression$xml_find_function_calls(c("str_detect", "str_which"))
stringr_calls <- xml_parent(xml_parent(xml_parent(
source_expression$xml_find_function_calls(c("str_detect", "str_which"))
)))
stringr_calls <- strip_comments_from_subtree(stringr_calls)
stringr_expr <- xml_find_all(stringr_calls, stringr_xpath)

stringr_lints <- xml_nodes_to_lints(
Expand Down
1 change: 1 addition & 0 deletions R/seq_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ seq_linter <- function() {
xml_find_all(seq_calls, seq_xpath),
xml_find_all(xml, colon_xpath)
)
seq_expr <- strip_comments_from_subtree(seq_expr)

dot_expr1 <- get_fun(seq_expr, 1L)
dot_expr2 <- get_fun(seq_expr, 2L)
Expand Down
Loading
Loading