Skip to content

False positive in extraction_operator_linter() with plotmath expressions #1484

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
IndrajeetPatil opened this issue Jul 29, 2022 · 4 comments · Fixed by #2409
Closed

False positive in extraction_operator_linter() with plotmath expressions #1484

IndrajeetPatil opened this issue Jul 29, 2022 · 4 comments · Fixed by #2409
Labels
false-positive code that shouldn't lint, but does

Comments

@IndrajeetPatil
Copy link
Collaborator

One can use plotmath to display mathematical expressions in a graphic:

library(ggplot2)
expr <- list(quote(italic("x")["i"]))
ggplot() + labs(title = parse(text = expr))

Created on 2022-07-29 by the reprex package (v2.0.1)

But the code for subscript produces an incorrect lint:

library(lintr)

lint(
  text = "list(quote(italic('x')['i']))",
  linters = extraction_operator_linter()
)
#> <text>:1:23: warning: [extraction_operator_linter] Use `[[` instead of `[` to extract an element.
#> list(quote(italic('x')['i']))
#>                       ^

Created on 2022-07-29 by the reprex package (v2.0.1)

The suggestion is misleading as it won't produce the same output:

library(ggplot2)
expr <- list(quote(italic("x")[["i"]]))
ggplot() + labs(title = parse(text = expr))

Created on 2022-07-29 by the reprex package (v2.0.1)

@IndrajeetPatil IndrajeetPatil added the false-positive code that shouldn't lint, but does label Jul 29, 2022
@AshesITR
Copy link
Collaborator

Maybe we could optionally skip quote() and friends.

@IndrajeetPatil
Copy link
Collaborator Author

Yeah, I agree. We can also skip its close cousins from {rlang} package.

@MichaelChirico
Copy link
Collaborator

I think there are many such cases where extraction_operator_linter() is giving false positives (e.g., [[ method is not defined for the object, or it really is intentional to maintain the class but subset to one element, e.g. iris["Species"] is perfectly valid).

Not sure how worth effort it is to maintain exceptions here, authors dinged by this linter often should simply not use it IMO :)

@MichaelChirico
Copy link
Collaborator

Perhaps a better choice is to add parameters: allow_single_extraction and allow_dollar_extraction that toggle which of the usages are linted.

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

Successfully merging a pull request may close this issue.

3 participants