GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — PatternMatchingInstanceof leaves unnecessary parentheses · Issue #4996 · google/error-prone

PatternMatchingInstanceof leaves unnecessary parentheses #4996

New issue
Copy link
New issue
Copy link
Closed
Closed
PatternMatchingInstanceof leaves unnecessary parentheses#4996
Copy link

Description

@PhilippWendler
PhilippWendler
opened on Apr 17, 2025
Issue body actions

Since ab45284 the PatternMatchingInstanceof check leaves parentheses that are flagged by the UnnecessaryParentheses check. I guess it is not desired that one Error Prone check produces code that another check than complains about?

One particular important case is:

if (o instanceof Test) {
  Test t = ((Test) o);
}

The parentheses make Error Prone miss the fact that it can eliminate the variable declaration and directly use o instanceof Test t.

Other cases found inside our code case and flagged by UnnecessaryParentheses are in return statements, inside divisions, and ternary operators. It is easy to extend PatternMatchingInstanceof.requiresParentheses() and fix particular cases, and I could do so in a PR, but I wonder whether this is the best approach, or whether the function should instead use a list of cases with required parentheses? So I wanted to clarify this upfront.

Activity

  1. cushon commented on Apr 17, 2025

    @cushon
    cushon
    on Apr 17, 2025
    Collaborator
    More actions

    It would be nice if PatternMatchingInstanceof handled all these cases. Would you not get the same result by running UnnecessaryParentheses first? Maybe that check does things you don't want?

    I wonder whether this is the best approach, or whether the function should instead use a list of cases with required parentheses?

    Maybe, I'm not sure. The risk of checking for cases where parentheses are required is that if we miss them the check produces bad findings, instead of missing a finding. Either way I find it hard to remember all of the cases where they are/aren't required. I'm open to being convinced that handling the cases where they're required is simpler :)

  2. PhilippWendler commented on Apr 22, 2025

    @PhilippWendler
    PhilippWendler
    on Apr 22, 2025
    Author
    More actions

    It would be nice if PatternMatchingInstanceof handled all these cases. Would you not get the same result by running UnnecessaryParentheses first? Maybe that check does things you don't want?

    Unfortunately, no. We do run Error Prone as part of our standard build already and have all suggestions of UnnecessaryParentheses applied.

    For the assignment example from my post, extending UnnecessaryParentheses to flag it could make sense. But cases like boolean foo = (bar == baz); imply for me that UnnecessaryParentheses should not flag all top-level parentheses on the right-hand side of an assignment, so it would need some more complex logic, and I don't think the complexity seems worth it.

    And furthermore, for other cases I noticed in our code base, like ((Double) d) / 2.0, I can understand that developers would like to have the parentheses (if they are not even required), but after applying PatternMatchingInstanceof this should of course be d / 2.0 and not (d) / 2.0.

    I wonder whether this is the best approach, or whether the function should instead use a list of cases with required parentheses?

    Maybe, I'm not sure. The risk of checking for cases where parentheses are required is that if we miss them the check produces bad findings, instead of missing a finding. Either way I find it hard to remember all of the cases where they are/aren't required. I'm open to being convinced that handling the cases where they're required is simpler :)

    I am also unsure, that's why I asked. :-) Ideal would be a way to distinguish this kind of parentheses from parentheses that are part of the syntax of some other construct. But I don't know whether the AST provides this information.

  3. graememorgan commented on May 2, 2025

    @graememorgan
    graememorgan
    on May 2, 2025
    Contributor
    More actions

    PatternMatchingInstanceof.requiresParentheses

    There's no such method, so I'm not quite sure what you mean. I mailed a fix though.

    I'd be in favour of UnnecessaryParentheses firing on foo = (a); even for the case in the above comment, but I can see that being controversial.

  4. PhilippWendler commented on May 5, 2025

    @PhilippWendler
    PhilippWendler
    on May 5, 2025
    Author
    More actions

    PatternMatchingInstanceof.requiresParentheses

    There's no such method, so I'm not quite sure what you mean. I mailed a fix though.

    I was referring to this:

    error-prone/core/src/main/java/com/google/errorprone/bugpatterns/PatternMatchingInstanceof.java

    Line 272 in b20b019

    private static boolean requiresParentheses(TreePath path) {

    If you have a fix, great, I am willing to test.

  5. graememorgan commented on May 6, 2025

    @graememorgan
    graememorgan
    on May 6, 2025
    Contributor
    More actions

    Apparently I should have gone to Specsavers. :)

  6. copybara-service
    added 2 commits that reference this issue on May 6, 2025

    Look through parens when finding variables in PatternMatchingInstanceof.

    5a78a08

    Look through parens when finding variables in PatternMatchingInstanceof.

    32ff434
  7. graememorgan
    closed this as completedon May 7, 2025
  8. svc-squareup-copybara
    added a commit that references this issue on Jun 26, 2025

    This PR contains the following updates:

    089d991
  9. eizus
    added a commit that references this issue on Jul 18, 2026

    This PR contains the following updates:

    d285a3b
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

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.