Skip to content

Formatting and linting are inconsistent in terms of spacing a^(b) #273

Closed

Description

@bersbersbers

Note that I am using https://github.com/REditorSupport/vscode-r-lsp, but I feel that particular implementation is not the core issue, so I'll try here first.

My problem is this line:

x^(y + z)

which I feel is nicely spaced. However, lintr says

Place a space before left parenthesis, except in a function call.

So when I do this,

x^ (y + z)

the lintr message disappears, but a subsequent "Format Document With ... - R LSP Client" in VS Code will remove the space and I end up with the first line and its lintr message. (The same is true for x ^ (y + z).)

I guess the problem is that lintr follows Hadley Wickham's style guide (source), which states

Place a space before left parentheses, except in a function call.

and

Place spaces around all infix operators

(emphasis mine) and has an y ^ x example in the code. So it's hard to argue that lintr is wrong.

At the same time, styler follows the tidyverse style guide (source), which does not require spaces around all parentheses and has an exception for certain infix operators, including ^:

There are a few exceptions, which should never be surrounded by spaces:

  • The operators with high precedence: ::, :::, $, @, [, [[, ^, unary -, unary +, and :.

Activity

  1. renkun-ken commented on Jun 5, 2020

    @renkun-ken
    Member

    This is more of inconsistencies between styler and lintr. We are using styler to do formatting and lintr for diagnostics.

  2. bersbersbers commented on Jul 20, 2020

    @bersbersbers
    Author

    I think you can see from my original question that you basically rephrased my analysis ;) The question is, what should be done about it?

    I did my own research and found r-lib/lintr#302 - which is fixed but for some reason, the fix maybe has not made it into CRAN yet.

  3. bersbersbers commented on Oct 10, 2020

    @bersbersbers
    Author

    Here's another example:

    a <- -(b)
    

    Place a space before left parenthesis, except in a function call.

    a <- - (b)
    

    No lintr message, but "Format Document" reverts it back to the above code.

    Since the lintr documentation has changed since I reported this (it now says

    Most of the default linters are based on Hadley Wickham's The tidyverse style guide.

    I will also report this in r-lib/lintr#508

  4. renkun-ken commented on Oct 6, 2026

    @renkun-ken
    Member

    I rechecked both reported spacing cases with current master and lintr 3.4.0: x <- y^(z + 1) and a <- -(b) produce no diagnostics. The lintr/styler spacing mismatch described here no longer reproduces; the linked upstream lintr issue is also closed.

    Closing as resolved.

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