Bug 293558 - [quick assist] "Invert if statement" fails when comment follows
Summary: [quick assist] "Invert if statement" fails when comment follows
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5.1   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-28 07:31 EDT by Olaf Kock CLA
Modified: 2010-04-26 12:37 EDT (History)
3 users (show)

See Also:


Attachments
Proposed fix + regression tests (5.53 KB, patch)
2010-03-17 12:18 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Olaf Kock CLA 2009-10-28 07:31:00 EDT
User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.14) Gecko/2009090216 Ubuntu/8.04 (hardy) Firefox/3.0.14
Build Identifier: 20090920-1017

Using the "invert 'if' statement" quickfix gives invalid code when the statement is followed by a comment.

Reproducible: Always

Steps to Reproduce:
1. (no special setup required)
2. Enter the following conditional (including the single line comment following the condition) anywhere such code is valid:

  if(Math.random() < 0.5) {
    System.out.println("<");
  } else {
    System.out.println(">");
  }
  // some comment

3. Use the "invert 'if' statement" quickfix to invert. This results in the following invalid code:

  if(Math.random() >= 0.5) {
    System.out.println(">");
  }
  // some comment else {
    System.out.println("<");
  }
Comment 1 Dani Megert CLA 2009-10-29 04:05:49 EDT
Can reproduce using I20091027-0100.

Deepak, please investigate.
Comment 2 Deepak Azad CLA 2009-10-30 08:17:45 EDT
The transformation and comment shifting is done using ASTRewrite in JDT core.

Also if there is a line gap in the comment(as shown below) the comment is left as it is

  if(Math.random() < 0.5) {
    System.out.println("<");
  } else {
    System.out.println(">");
  }

  // some comment
Comment 3 Olivier Thomann CLA 2009-11-26 13:16:02 EST
This might be a bug in the extended range for a node. The comment following the '}' should not be included within that range if it is located on the next line.
Comment 4 Olaf Kock CLA 2009-11-27 05:02:22 EST
Thank you for taking care of this issue.

In reply to the previous comment I'd argue that it shouldn't result in this behaviour even if the comment was on the same line as the closing '}'. 

This code:

  if(Math.random() < 0.5) {
    System.out.println("<");
  } else {
    System.out.println(">");
  } // some comment

should also be valid after an "invert 'if' statement" quickfix, otherwise it would be 'invalidated', not 'inverted'.
Comment 5 Olivier Thomann CLA 2010-03-17 12:18:16 EDT
Created attachment 162313 [details]
Proposed fix + regression tests
Comment 6 Olivier Thomann CLA 2010-03-17 12:18:57 EDT
Released for 3.6M7.
Regression test added in:
org.eclipse.jdt.core.tests.rewrite.describing.ASTRewritingStatementsTest#testIfStatementReplaceElse5
Comment 7 Frederic Fusier CLA 2010-04-26 12:37:07 EDT
Verified for 3.6M7 using I20100425-2000.