Skip to content

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

Closed

Description

@eitsupi

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)) {...}

Activity

  1. AshesITR commented on Aug 22, 2022

    @AshesITR
    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).

  2. eitsupi commented on Aug 22, 2022

    @eitsupi
    ContributorAuthor

    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

  3. IndrajeetPatil commented on Aug 26, 2022

    @IndrajeetPatil
    Contributor

    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

  4. added
    feature
    on Aug 26, 2022
  5. MichaelChirico commented on Aug 26, 2022

    @MichaelChirico
    Collaborator

    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)]
  6. IndrajeetPatil commented on Aug 26, 2022

    @IndrajeetPatil
    Contributor

    Nice!

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

  7. MichaelChirico commented on Aug 26, 2022

    @MichaelChirico
    Collaborator

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

  8. changed the title [-]New linter for `if(isTRUE(x))` and `if(isFALSE(x))`[/-] [+]New linter for `if(x == TRUE)` and `if(x == FALSE)`[/+] on Aug 27, 2022
  9. eitsupi commented on Aug 27, 2022

    @eitsupi
    ContributorAuthor

    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.

  10. added and removed
    feature
    on Aug 29, 2022
  11. added this to the 3.0.2 milestone on Sep 20, 2022
  12. IndrajeetPatil commented on Sep 20, 2022

    @IndrajeetPatil
    Contributor

    @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.

  13. MichaelChirico commented on Sep 20, 2022

    @MichaelChirico
    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.

  14. modified the milestones: 3.0.3, on Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions