Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 149801 - [quick assist] 'exchange left and right operands' incorrectly removes parentheses around logical operators
Summary: [quick assist] 'exchange left and right operands' incorrectly removes parenth...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.7 M4   Edit
Assignee: Deepak Azad CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 312474 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-07-06 06:21 EDT by Dominic Evans CLA
Modified: 2010-11-11 22:55 EST (History)
4 users (show)

See Also:


Attachments
fix + tests (5.91 KB, patch)
2010-11-11 22:54 EST, Deepak Azad CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dominic Evans CLA 2006-07-06 06:21:06 EDT
The 'exchange left and right operands for infix expression' incorrectly removes parentheses that can be necessary for the statement to evaluate correctly if it contains logical operators.

e.g.,

    private boolean isEnabled(byte[] masterBits)
    {
        return (0 == (masterBits[ENABLE_INDEX] & ENABLE_MASK));
    }

Applying the 'exchange left and right' operator, Eclipse removes the parantheses on the right and side and _incorrectly_ turns the method into:

    private boolean isEnabled(byte[] masterBits)
    {
        return (masterBits[ENABLE_INDEX] & ENABLE_MASK == 0);
    }

This yields the Java compile error: 'The operator & is undefined for the argument type(s) byte, boolean'

It should have refactored to:

    private boolean isEnabled(byte[] masterBits)
    {
        return ((masterBits[ENABLE_INDEX] & ENABLE_MASK) == 0);
    }
Comment 1 Frederic Fusier CLA 2006-07-06 06:49:29 EDT
Move to JDT/UI
Comment 2 Pieter Schoenmakers CLA 2006-09-29 04:43:02 EDT
The same is true for boolean operators; in the following, exchanging the operands of && drops the parentheses around the ||.

import java.util.Set;

public class TestInfixSwap
{
  public boolean test (Set s, int i)
  {
    return (i == 0 || i == 1) && s.size () > 0;
  }
}
Comment 3 Carl Manaster CLA 2007-03-28 17:54:37 EDT
This also happens with arithmetic operators:

    int n = 4 * (2 + 3);

becomes

    int n = 2 + 3 * 4;
Comment 4 Dani Megert CLA 2010-05-12 02:35:29 EDT
*** Bug 312474 has been marked as a duplicate of this bug. ***
Comment 5 Deepak Azad CLA 2010-11-11 22:54:32 EST
Created attachment 182948 [details]
fix + tests
Comment 6 Deepak Azad CLA 2010-11-11 22:55:07 EST
Fixed in HEAD.