Skip to content

Any pipe (|> or %>%) on the end of a line leads to a false negative for commented_code_linter #2671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jcken95 opened this issue Oct 18, 2024 · 2 comments · Fixed by #2672
Closed

Comments

@jcken95
Copy link
Contributor

jcken95 commented Oct 18, 2024

##  no pipe

lintr::lint(
  text = "# mean()"
)
#> <text>:1:3: style: [commented_code_linter] Remove commented code.
#> # mean()
#>   ^~~~~~

## base pipes

lintr::lint(
  text = "# mean() |>"
)
#> ℹ No lints found.

lintr::lint(
  text = "# 1:5 |> mean()"
)
#> <text>:1:3: style: [commented_code_linter] Remove commented code.
#> # 1:5 |> mean()
#>   ^~~~~~~~~~~~~

## magrittr pipes

lintr::lint(
  text = "# mean() %>%"
)
#> ℹ No lints found.

lintr::lint(
  text = "# 1:5 %>% mean()"
)
#> <text>:1:3: style: [commented_code_linter] Remove commented code.
#> # 1:5 %>% mean()
#>   ^~~~~~~~~~~~~~

Created on 2024-10-18 with reprex v2.1.1


# .lintr

linters: linters_with_defaults(
           line_length_linter(120L),
           object_length_linter(40L),
           pipe_consistency_linter("|>"),
           object_usage_linter = NULL,
           indentation_linter = NULL,
           unreachable_code_linter()
         )
encoding: "UTF-8"
@jcken95 jcken95 changed the title Base pipe |> on the end of a line leads to a false negative for commented_code_linter Any pipe (|> or %>%) on the end of a line leads to a false negative for commented_code_linter Oct 18, 2024
@MichaelChirico
Copy link
Collaborator

Sorry, what's the expected behavior here?

In order for code to be detected, it has to parse. Your examples with no lints don't parse.

@jcken95
Copy link
Contributor Author

jcken95 commented Oct 19, 2024

The expected behaviour would be for lintr::lint(text = " # mean() |>" to return a commented code linter.

Appreciate this code is not parsable in isolation, however, code of this form is a common occurrence in data wrangling. For example:

# no lint detected (expected, desirable behaviour)
lintr::lint(
  text = "mtcars |>
            dplyr::filter(cyl > 4) |>
            dplyr::summarise(m = mean(wt))",
  linters = lintr:::commented_code_linter()
)

# would like/expect the commented out `dplyr::filter(cyl > 4) |>` to picked up by the lintr
# but commented_code_linter() is not flagged in this example, because the single line
# is not parsable
lintr::lint(
  text = "mtcars |>
           #dplyr::filter(cyl > 4) |>
            dplyr::summarise(m = mean(wt))",
  linters = lintr:::commented_code_linter()
)

Created on 2024-10-19 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants