Bug 394721 - [inline] Inline local variable with initializer generates assignment where left-hand side is not a variable
Summary: [inline] Inline local variable with initializer generates assignment where le...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.2.1   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.5 M7   Edit
Assignee: Nikolay Metchev CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-11-20 19:15 EST by Milos Gligoric CLA
Modified: 2015-03-30 11:14 EDT (History)
4 users (show)

See Also:
noopur_gupta: review+


Attachments
bug fix + unit test (5.50 KB, patch)
2013-10-04 11:12 EDT, Nikolay Metchev CLA
no flags Details | Diff
bug fix + unit test (7.44 KB, patch)
2014-12-22 12:04 EST, Nikolay Metchev CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Milos Gligoric CLA 2012-11-20 19:15:12 EST
Steps to reproduce:
1. Invoke "Inline" on "m" in code below
2. The resulting file cannot be compiled ("The left-hand side of an assignment must be a variable")

class InlineLocalVariableBug1 {
    void f() {
        // Invoke "Inline..." on "m" result in "new int[10][0] = 0;"
        int[] m = new int[10];
        m[0] = 0;
    }
}
Comment 1 Martin Mathew CLA 2012-12-05 03:41:29 EST
The issue is reproducible. The refactoring results in compiler error.
Comment 2 Nikolay Metchev CLA 2013-10-04 11:12:05 EDT
Created attachment 236120 [details]
bug fix + unit test

This contribution complies with http://www.eclipse.org/legal/CoO.php
Comment 3 Noopur Gupta CLA 2014-12-08 01:49:41 EST
(In reply to Nikolay Metchev from comment #2)
> Created attachment 236120 [details] [diff]
> bug fix + unit test
> 
> This contribution complies with http://www.eclipse.org/legal/CoO.php

The fix should go in org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker.needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType)

which is already invoked in InlineTempRefactoring, and so that the issue is handled everywhere (example: (new int[10])[0] = 0; should not suggest the quick assist to remove parentheses i.e. "Remove extra parentheses").

Also, make sure that extra parentheses are not added when the array creation expr has initializer. Add test case for the same.

Verify that it is fixed for InlineConstantRefactoring also.
Comment 4 Nikolay Metchev CLA 2014-12-22 12:04:41 EST
Created attachment 249595 [details]
bug fix + unit test

This contribution complies with http://www.eclipse.org/legal/CoO.php

Thanks Noopur,
I have incorporated your suggestions. Just to let you know that test_29 and test_30 cover the case where the array creation has an initializer, so I didn't add a duplicate test.
Comment 5 Noopur Gupta CLA 2015-03-30 11:14:16 EDT
(In reply to Nikolay Metchev from comment #4)
> Created attachment 249595 [details] [diff]
> bug fix + unit test
> 
> This contribution complies with http://www.eclipse.org/legal/CoO.php

Fixed copyright and contributors list, inlined the new method and released with:
http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=0501a95fb1ec44c661d81f4d2740f27ae93fe234