@@ -2,42 +2,50 @@ test_that("literal_coercion_linter skips allowed usages", {
22 linter <- literal_coercion_linter()
33
44 # naive xpath includes the "_f0" here as a literal
5- expect_lint (' as.numeric(x$"_f0")' , NULL , linter )
6- expect_lint (' as.numeric(x@"_f0")' , NULL , linter )
5+ expect_no_lint (' as.numeric(x$"_f0")' , linter )
6+ expect_no_lint (' as.numeric(x@"_f0")' , linter )
77 # only examine the first method for as.<type> methods
8- expect_lint (" as.character(as.Date(x), '%Y%m%d')" , NULL , linter )
8+ expect_no_lint (" as.character(as.Date(x), '%Y%m%d')" , linter )
99
1010 # we are as yet agnostic on whether to prefer literals over coerced vectors
11- expect_lint (" as.integer(c(1, 2, 3))" , NULL , linter )
11+ expect_no_lint (" as.integer(c(1, 2, 3))" , linter )
1212 # even more ambiguous for character vectors like here, where quotes are much
1313 # more awkward to type than a sequence of numbers
14- expect_lint (" as.character(c(1, 2, 3))" , NULL , linter )
14+ expect_no_lint (" as.character(c(1, 2, 3))" , linter )
1515 # not possible to declare raw literals
16- expect_lint (" as.raw(c(1, 2, 3))" , NULL , linter )
16+ expect_no_lint (" as.raw(c(1, 2, 3))" , linter )
1717 # also not taking a stand on as.complex(0) vs. 0 + 0i
18- expect_lint (" as.complex(0)" , NULL , linter )
18+ expect_no_lint (" as.complex(0)" , linter )
1919 # ditto for as.integer(1e6) vs. 1000000L
20- expect_lint (" as.integer(1e6)" , NULL , linter )
20+ expect_no_lint (" as.integer(1e6)" , linter )
2121 # ditto for as.numeric(1:3) vs. c(1, 2, 3)
22- expect_lint (" as.numeric(1:3)" , NULL , linter )
22+ expect_no_lint (" as.numeric(1:3)" , linter )
2323})
2424
2525test_that(" literal_coercion_linter skips allowed rlang usages" , {
2626 linter <- literal_coercion_linter()
2727
28- expect_lint (" int(1, 2.0, 3)" , NULL , linter )
29- expect_lint (" chr('e', 'ab', 'xyz')" , NULL , linter )
30- expect_lint (" lgl(0, 1)" , NULL , linter )
31- expect_lint (" lgl(0L, 1)" , NULL , linter )
32- expect_lint (" dbl(1.2, 1e5, 3L, 2E4)" , NULL , linter )
28+ expect_no_lint (" int(1, 2.0, 3)" , linter )
29+ expect_no_lint (" chr('e', 'ab', 'xyz')" , linter )
30+ expect_no_lint (" lgl(0, 1)" , linter )
31+ expect_no_lint (" lgl(0L, 1)" , linter )
32+ expect_no_lint (" dbl(1.2, 1e5, 3L, 2E4)" , linter )
3333 # make sure using namespace (`rlang::`) doesn't create problems
34- expect_lint (" rlang::int(1, 2, 3)" , NULL , linter )
34+ expect_no_lint (" rlang::int(1, 2, 3)" , linter )
3535 # even if scalar, carve out exceptions for the following
36- expect_lint (" int(1.0e6)" , NULL , linter )
36+ expect_no_lint (" int(1.0e6)" , linter )
3737})
3838
3939test_that(" literal_coercion_linter skips quoted keyword arguments" , {
40- expect_lint(" as.numeric(foo('a' = 1))" , NULL , literal_coercion_linter())
40+ linter <- literal_coercion_linter()
41+ expect_no_lint(" as.numeric(foo('a' = 1))" , linter )
42+ expect_no_lint(
43+ trim_some("
44+ as.numeric(foo('a' # comment
45+ = 1))
46+ " ),
47+ linter
48+ )
4149})
4250
4351test_that(" no warnings surfaced by running coercion" , {
@@ -50,6 +58,18 @@ test_that("no warnings surfaced by running coercion", {
5058 expect_no_warning(
5159 expect_lint(" as.integer(2147483648)" , " Use NA_integer_" , linter )
5260 )
61+
62+ expect_no_warning(
63+ expect_lint(
64+ trim_some("
65+ as.double(
66+ NA # comment
67+ )
68+ " ),
69+ " Use NA_real_" ,
70+ linter
71+ )
72+ )
5373})
5474
5575skip_if_not_installed(" tibble" )
@@ -81,6 +101,7 @@ patrick::with_parameters_test_that(
81101
82102skip_if_not_installed(" rlang" )
83103test_that(" multiple lints return custom messages" , {
104+ linter <- literal_coercion_linter()
84105 expect_lint(
85106 trim_some(" {
86107 as.integer(1)
@@ -90,7 +111,24 @@ test_that("multiple lints return custom messages", {
90111 list (rex :: rex(" Use 1L instead of as.integer(1)" ), line_number = 2L ),
91112 list (rex :: rex(" Use TRUE instead of lgl(1L)" ), line_number = 3L )
92113 ),
93- literal_coercion_linter()
114+ linter
115+ )
116+
117+ # also ensure comment remove logic works across several lints
118+ expect_lint(
119+ trim_some(" {
120+ as.integer( # comment
121+ 1 # comment
122+ ) # comment
123+ lgl( # comment
124+ 1L # comment
125+ ) # comment
126+ }" ),
127+ list (
128+ list (rex :: rex(" Use 1L instead of as.integer(1)" ), line_number = 2L ),
129+ list (rex :: rex(" Use TRUE instead of lgl(1L)" ), line_number = 5L )
130+ ),
131+ linter
94132 )
95133})
96134
0 commit comments