object_usage_linter marks wrong line with no visible binding message

207 Views Asked by At

Consider the following code snippet (comments at the end indicate line numbers and are not part of the problem):

set.seed(1)                                   # 1
my_dat1 <- data.frame(x = runif(100, 1, 10),  # 2
                      w = runif(100, 1, 10))  # 3
                                              # 4
my_dat1$y_orig <- my_dat1$x * my_dat1$w       # 5
my_dat1$y_obs  <- my_dat1$y_orig + rnorm(100) # 6
                                              # 7
my_stat <- function(data, orig_mod) {         # 8
  new_mod <- update(orig_mod, data = data)    # 9
  r <- residuals(new_mod)                     #10
  f <- fitted(new_mod)                        #11
  l <- lowess(f, r)                           #12
  res <- c(l$x, l$y)                          #13
  res                                         #14
}                                             #15

When I lint this file via lintr:::addin_lint() I get the following strange error:

$ test.R:List of 8
 ..$ filename     : chr "test.R
 ..$ line_number  : int 6
 ..$ column_number: int 14
 ..$ type         : chr "warning"
 ..$ message      : chr "no visible binding for global variable ‘x’"
 ..$ line         : chr "  res <- c(l$x, l$y)                          #13"
 ..$ ranges       :List of 1
 .. ..$ : int [1:2] 14 14
 ..$ linter       : chr "object_usage_linter"
 ..- attr(*, "class")= chr "lint"
$ test.R:List of 8
 ..$ filename     : chr "test.R"
 ..$ line_number  : int 6
 ..$ column_number: int 19
 ..$ type         : chr "warning"
 ..$ message      : chr "no visible binding for global variable ‘y’"
 ..$ line         : chr "  res <- c(l$x, l$y)                          #13"
 ..$ ranges       :List of 1
 .. ..$ : int [1:2] 19 19
 ..$ linter       : chr "object_usage_linter"
 ..- attr(*, "class")= chr "lint"
- attr(*, "class")= chr "lints"

However, if I remove lines #5 and #6 I do not have the error any more:

set.seed(1)                                   # 1
my_dat1 <- data.frame(x = runif(100, 1, 10),  # 2
                      w = runif(100, 1, 10))  # 3
                                              # 4
                                              # 7
my_stat <- function(data, orig_mod) {         # 8
  new_mod <- update(orig_mod, data = data)    # 9
  r <- residuals(new_mod)                     #10
  f <- fitted(new_mod)                        #11
  l <- lowess(f, r)                           #12
  res <- c(l$x, l$y)                          #13
  res                                         #14
}                                             #15

There is no error when linting this file.

Questions

  1. Is this an intentional behaviour or is a bug in lintr?
  2. Why is the line number wrong given that lintr complains about a snippet in line number #13 and not in line number #6?
0

There are 0 best solutions below