Skip to content

How do we exclude if from the implicit_assignment_linter? #2913

Closed

Description

@TimTaylor

The two attempts below fail to exclude if. Am I missing something obvious?

library(lintr)
lint(
    linters = list(implicit_assignment_linter(except = c("if", "`if`"))),
    text="if(a <- 1) 1"
)
#> <text>:1:4: warning: [implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
#> if(a <- 1) 1
#>    ^~~~~~

Created on 2025-07-31 with reprex v2.1.1

Activity

  1. MichaelChirico commented on Jul 31, 2025

    @MichaelChirico
    Collaborator

    you might want allow_scoped=TRUE

  2. TimTaylor commented on Jul 31, 2025

    @TimTaylor
    ContributorAuthor

    you might want allow_scoped=TRUE

    Afraid not

    library(lintr)
    lint(
        linters = list(implicit_assignment_linter(except = c("if", "`if`"), allow_scoped = T)),
        text="if(a <- 1) 1"
    )
    #> <text>:1:4: warning: [implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
    #> if(a <- 1) 1
    #>    ^~~~~~

    Seems specific to if() (possibly others)

    lint(
        linters = list(implicit_assignment_linter(except = c("mean"), allow_scoped = T)),
        text="mean(a <- 1)"
    )
    #> ℹ No lints found.
  3. MichaelChirico commented on Jul 31, 2025

    @MichaelChirico
    Collaborator

    allow_scoped will skip if and only if 'a' is used inside that branch and not later on.

    there's not support for your use case otherwise, you'll just want to disable the linter.

  4. TimTaylor commented on Jul 31, 2025

    @TimTaylor
    ContributorAuthor

    No worries. Will close. Is it worth mentioning in the docs that some functions cannot be used in except? I'm still not clear what can't be used in except from the docs.

  5. MichaelChirico commented on Jul 31, 2025

    @MichaelChirico
    Collaborator

    if is an operator, not a function, I'm not sure it's worth mentioning.

  6. TimTaylor commented on Jul 31, 2025

    @TimTaylor
    ContributorAuthor

    Even though a control structure I assume it still counts as a function, i.e.

    is.function(`if`)
    #> [1] TRUE
    `if`(TRUE, bob <- 99)
    bob
    #> [1] 99

    That said I guess my feature request is to be able to disambiguate the, IMO, more problematic issues such as

    fun <- function(x, y) if (y > 0) "yes" else "boo"
    fun(y <- 3, x <- -1)
    #> [1] "boo"

    from ones where the arguments against are more stylistic (i.e. assignment in control structure). In essence I want to be able to flag the fun example to protect colleagues whilst allowing the use of assignment in control structures to which I'm pretty ambivalent.

    Is this worth a separate FR issue or is this a no go?

  7. TimTaylor commented on Jul 31, 2025

    @TimTaylor
    ContributorAuthor

    @MichaelChirico - Actually maybe this does want reopening? Coming back to your original comment about allow_scoped, I think the intention would be that this would work (i.e. no lints)

    library(lintr)
    lint(
        linters = list(
            implicit_assignment_linter(
                except = c("if", "`if`"),
                allow_scoped = TRUE)
        ),
        text = "if(a <- 1) print(a)"
    )
    #> <text>:1:4: warning: [implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
    #> if(a <- 1) print(a)
    #>    ^~~~~~

    if is also referred to as a function in the hard coded error message within implicit_assignment_linter.

  8. MichaelChirico commented on Jul 31, 2025

    @MichaelChirico
    Collaborator

    That's right -- fixed in #2914.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions