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: ⁠\(x) { x %>% return(); FALSE }⁠ 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.

Usage

pipe_return_linter()

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 Dec. 20, 2024, 7:24 p.m.