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 — [java] New Rule: AvoidReassigningCatchVariables · Issue #2471 · pmd/pmd

[java] New Rule: AvoidReassigningCatchVariables #2471

New issue
Copy link
New issue
Copy link
Closed
#2682
Closed
[java] New Rule: AvoidReassigningCatchVariables#2471
#2682
Copy link
Labels
a:new-ruleProposal to add a new built-in rule
Milestone
6.27.0

Description

@numeralnathan
numeralnathan
opened on May 6, 2020
Issue body actions

Proposed Rule Name: AvoidReassigningCatchVariables

Proposed Category: Best Practices

Description:

The rule AvoidReassigningParameters flags a problem when code assigns a value to a method parameter. Here is the reason in documentation.

Reassigning values to incoming parameters is not recommended. Use temporary local variables instead.

This new rule AvoidReassigningCatchVariables will flag a problem when code assigns a value to the catch variable.

I do not know why AvoidReassigningParameters exists. I assume that AvoidReassigningCatchVariables should exist for the same reason.

Code Sample:

...
catch (Exception e)
{
   e = new Exception("Hello World", e);   // This should be flagged as a problem
}
...

// Fixed code
...
catch (Exception e)
{
   Exception wrap;

   wrap = new Exception("Hello World", e);
}

Possible Properties:

This rule does not need any properties.

See also:

  • [java] Document Reasons to Avoid Reassigning Parameters #2219

Activity

  1. numeralnathan
    added
    a:new-ruleProposal to add a new built-in rule
    on May 6, 2020
  2. oowekyala commented on May 7, 2020

    @oowekyala
    oowekyala
    on May 7, 2020
    Member
    More actions

    One justification this rule could have, that is more solid than that of AvoidReassigningParameters, is that exception parameters in a multicatch block are implicitly final. So if you do

    catch (IOException e)
    {
       e = new Exception("Hello World", e); 
    }

    adding an exception type will make it uncompilable:

    catch (IOException | TokenMgrError e)
    {
       e = new Exception("Hello World", e); // e is implicitly final, this doesn't compile
    }
  3. adangel commented on May 8, 2020

    @adangel
    adangel
    on May 8, 2020
    Member
    More actions

    On the other hand - if the compiler already complains, then we don't need a rule 😄 - Update - I think, I get it now: the rule would prevent this compile error by flagging it even before (when assignment would be possible), so that you actually can add later another exception to the case and make it a multicatch.

    Reassigning parameters or catch variables might be surprising. It depends on what you are used to (principle of least surprise). If you assume, that the parameters/catch vars always have their initial value when entering the block, then you might make false decisions, if this is not the case (e.g. it might not be anymore the original exception with the correct/original stacktrace). See also #2219.

  4. adangel
    added this to the 6.27.0 milestone on Jul 31, 2020
  5. adangel
    changed the title [-][java] AvoidReassigningCatchVariables[/-] [+][java] New Rule: AvoidReassigningCatchVariables[/+] on Jul 31, 2020
  6. adangel
    closed this as completedin #2682on Jul 31, 2020
  7. adangel
    added a commit that references this issue on Jul 31, 2020

    [doc] Update release notes and new rules

    This issue will close once commit c447319 is merged into the 'main' branch.
    c447319
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

    a:new-ruleProposal to add a new built-in rule

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.27.0

      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.