pipe_return_linter: Block usage of return() in magrittr pipelines

pipe_return_linterR Documentation

Block usage of return() in magrittr pipelines

Description

return() inside a magrittr pipeline does not actually execute return() like you'd expect:

Usage

pipe_return_linter()

Details

bad_usage <- function(x) {
  x %>%
    return()
  FALSE
}

bad_usage(TRUE) will return FALSE! It will technically work "as expected" if this is the final statement in the function body, but such usage is misleading. Instead, assign the pipe outcome to a variable and return that.

Tags

best_practices, common_mistakes

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "function(x) x %>% return()",
  linters = pipe_return_linter()
)

# okay
code <- "function(x) {\n  y <- sum(x)\n  return(y)\n}"
writeLines(code)
lint(
  text = code,
  linters = pipe_return_linter()
)


r-lib/lintr documentation built on June 9, 2025, 7:45 a.m.