jcrodriguez1989 / rco

Package: The R Code Optimizer
https://jcrodriguez1989.github.io/rco/
84 stars 20 forks source link

Bug: opt_dead_expr() does not work on statements which end with semicolons #107

Open ZeroDawn0D opened 4 years ago

ZeroDawn0D commented 4 years ago

The following code snippet:

library(rco);
code1 <- paste(
  "f1 <- function(x){",
      "1",
      "2",
      "3",
      "4",
      "5",
  "}",
  sep = "\n"
)
code2 <- paste(
  "f2 <- function(x){",
      "1;",
      "2;",
      "3;",
      "4;",
      "5;",
  "}",
  sep = "\n"
)
cat(opt_dead_expr(list(code1))$codes[[1]]);
cat("\n");
cat(opt_dead_expr(list(code2))$codes[[1]]);

returns an output of:

f1 <- function(x){ 5 } f2 <- function(x){ 1; 2; 3; 4; 5; }

The function does not optimize lines which end with semicolons. Please look into this issue as general users may not be aware of it without explicit testing