Skip to content

unnecessary_nesting_linter() incorrectly recommends to get rid of braces in ggplot2 chains #2852

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

Open
IndrajeetPatil opened this issue Apr 23, 2025 · 0 comments
Labels
false-positive code that shouldn't lint, but does

Comments

@IndrajeetPatil
Copy link
Collaborator

Minimal Reprex

"library(ggplot2)
x <- TRUE
ggplot(mtcars, aes(wt, mpg)) +
  {
    if (x) geom_point()
  } +
  geom_smooth()" -> code

library(lintr)
lint(text = code, linters = unnecessary_nesting_linter())
#> <text>:4:3: warning: [unnecessary_nesting_linter] Reduce the nesting of this statement by removing the braces {}.
#>   {
#>   ^

Created on 2025-04-23 with reprex v2.1.1

Code with and without lint

Original code produces expected output

library(ggplot2)
x <- TRUE
ggplot(mtcars, aes(wt, mpg)) +
  {
    if (x) geom_point()
  } +
  geom_smooth()
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Recommended code produces error

ggplot(mtcars, aes(wt, mpg)) +
  if (x) geom_point() +
  geom_smooth()
#> Error:
#> ! Cannot add <ggproto> objects together.
#> ℹ Did you forget to add this object to a <ggplot> object?

Created on 2025-04-23 with reprex v2.1.1

@IndrajeetPatil IndrajeetPatil added the false-positive code that shouldn't lint, but does label Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive code that shouldn't lint, but does
Projects
None yet
Development

No branches or pull requests

1 participant