Skip to content

New linter for if(x == TRUE) and if(x == FALSE) #1500

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
eitsupi opened this issue Aug 21, 2022 · 9 comments · Fixed by #1556
Closed

New linter for if(x == TRUE) and if(x == FALSE) #1500

eitsupi opened this issue Aug 21, 2022 · 9 comments · Fixed by #1556

Comments

@eitsupi
Copy link
Contributor

eitsupi commented Aug 21, 2022

It may be useful for beginners if this is pointed out by lintr, since it may be more appropriate to rewrite if(x == TRUE) {...} as follows

if(x) {...}
if(isTRUE(x)) {...}
@AshesITR
Copy link
Collaborator

if (x) is not equivalent to if (isTRUE(x)) though, e.g. if x is a positive integer.
if (x == TRUE) is a more clear-cut case, same for if (x == FALSE).

@eitsupi
Copy link
Contributor Author

eitsupi commented Aug 22, 2022

Thank you for your reply and for explaining the details.
My concern is that beginners, including myself, may be using these without being fully aware of the differences and it could be helpful if lintr could suggest a better way to write.

I am assuming something similar to what shellcheck detects for single quotes.
(As noted in the documentation, double-quotes and single quotes have different meanings in shell scripts, and users who understand the behavior well enough will intentionally use the two.)

This suggestion is primarily meant to help newbies who assume single and double quotes are basically the same, like in Python and JavaScript. It's not at all meant to discourage experienced users from using single quotes in general. If you are well aware of the difference, please do not hesitate to permanently disable this suggestion with disable=SC2016 in your .shellcheckrc.

https://github.com/koalaman/shellcheck/wiki/SC2016

@IndrajeetPatil
Copy link
Collaborator

Agreed that these should lint:

library(lintr)

lint(text = "if (x == TRUE) 1L",
     linters = linters_with_tags(tags = NULL))

lint(text = "if (x == FALSE) 0L",
     linters = linters_with_tags(tags = NULL))

Created on 2022-08-26 with reprex v2.0.2

@IndrajeetPatil IndrajeetPatil added the feature a feature request or enhancement label Aug 26, 2022
@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Aug 26, 2022

FWIW, we have a linter for this already (slated for upstreaming eventually). Any ==TRUE/FALSE expression is linted.

One tougher case is data.table subsetting, where the overloading of the i argument means we have to take care:

DT[is_treatment == TRUE]
# WRONG, not equivalent
DT[is_treatment]
# correct -- data.table uses () to signal "not a join"
DT[(is_treatment)]

@IndrajeetPatil
Copy link
Collaborator

Nice!

Should this get the "google-linter" label then?

@MichaelChirico
Copy link
Collaborator

up to OP -- originally the requested linter is slightly different. but if this format is satisfactory they ok

@eitsupi eitsupi changed the title New linter for if(isTRUE(x)) and if(isFALSE(x)) New linter for if(x == TRUE) and if(x == FALSE) Aug 27, 2022
@eitsupi
Copy link
Contributor Author

eitsupi commented Aug 27, 2022

originally the requested linter is slightly different. but if this format is satisfactory they ok

I think it would be great if it were implemented.
I hope this issue can be used for it.

@IndrajeetPatil IndrajeetPatil added google-linters and removed feature a feature request or enhancement labels Aug 29, 2022
@IndrajeetPatil IndrajeetPatil added this to the 3.0.2 milestone Sep 20, 2022
@IndrajeetPatil
Copy link
Collaborator

@MichaelChirico Not sure how much it would be to port this over from Google, but, for now, marking this for 3.0.2.
Feel free to remove it if you think that's too optimistic.

@MichaelChirico
Copy link
Collaborator

That's fine, I don't think there's any rush for 3.0.2. Porting is mostly tedium to switch internal idioms to match lintr style, and more importantly reviewer bandwidth.

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

Successfully merging a pull request may close this issue.

4 participants