lintr icon indicating copy to clipboard operation
lintr copied to clipboard

nonportable_path_linter lints gsub("[^0-9\\-]", "", x)

Open mcol opened this issue 2 months ago • 2 comments

This shows the problem:

lintr::lint(text = 'gsub("[^0-9\\\\-]", "", x)', linter = lintr::nonportable_path_linter())
# <text>:1:7: warning: [nonportable_path_linter] Use file.path() to construct portable file paths.
# gsub("[^0-9\\-]", "", x)
#       ^~~~~~~~~

A minimized version:

lintr::lint(text = "'[\\\\-]'", linter = lintr::nonportable_path_linter())
# <text>:1:2: warning: [nonportable_path_linter] Use file.path() to construct portable file paths.
# '[\\-]'
#  ^~~~~

Actually, my script has gsub("[^0-9\\-]", "", x), but this is parsed differently when using text:

lintr::lint(text = "gsub('[^0-9\\-]', '', x)", linter = lintr::nonportable_path_linter())
# <text>:1:13: error: [error] '\-' is an unrecognized escape in character string.
# gsub('[^0-9\-]', '', x)

mcol avatar Oct 17 '25 08:10 mcol

A different false positive:

lintr::lint(text = 'as.Date(x, "%m/%d/%Y")', linter = lintr::nonportable_path_linter())
# <text>:1:34: warning: [nonportable_path_linter] Use file.path() to construct portable file paths.
# as.Date(x, "%m/%d/%Y")
#             ^~~~~~~~

mcol avatar Oct 17 '25 09:10 mcol

Related: #1356, #468

MichaelChirico avatar Oct 17 '25 22:10 MichaelChirico