lintr icon indicating copy to clipboard operation
lintr copied to clipboard

False positive in pipe_continuation_linter when code has nested pipes

Open javieriserte opened this issue 1 year ago • 4 comments

When a piece of code has two pipes, one nested inside another like in example code below:

c("1", "2", "3", "4") |>
  sapply(function(x) x |> runif() |> mean()) |>
  as.character()

the linter produces a false positive:

<text>:2:46: style: [pipe_continuation_linter] `|>` should always have a space before it and a new line after it, unless the full pipeline fits on one line.
  sapply(function(x) x |> runif() |> mean()) |>
                                         ^~

I think that the linter may consider the expression like a single pipe, instead of two nested pipes.

I'm using lintr 3.1.2.

javieriserte avatar Jul 15 '24 14:07 javieriserte

Does the following work for you?

c("1", "2", "3", "4") |>
  sapply(
    function(x) x |> runif() |> mean()
  ) |>
  as.character()

I think linting he inner pipeline could be made optional. Personally I prefer keeping the lint because regardless of syntax, discerning the multiple pipe characters in one line is hard on my eyes.

AshesITR avatar Jul 16 '24 06:07 AshesITR

discerning the multiple pipe characters in one line is hard on my eyes.

I agree, but for this we already have https://github.com/r-lib/lintr/blob/73e55d31f6ead2908fcd9a49d6ccf46524b1cccc/R/nested_pipe_linter.R

I'll mark this as "help wanted" as I'm not sure it's worth the effort, but if this is driving you nuts feel free to have a go at it.

MichaelChirico avatar Jul 16 '24 06:07 MichaelChirico

Thank you!,

I'll look into it.

javieriserte avatar Jul 16 '24 19:07 javieriserte

Keeping this open to track the false positive

AshesITR avatar Jul 17 '24 13:07 AshesITR