Bug 492287 - Using StringBuffer.append(a + b) should generate a warning
Summary: Using StringBuffer.append(a + b) should generate a warning
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.6   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-22 17:44 EDT by Alex Blewitt CLA
Modified: 2016-04-23 02:14 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Blewitt CLA 2016-04-22 17:44:47 EDT
There are a number of places in existing Java code that are written as:

import java.util.Date;
public class Test {
  public static void main(String args[]) throws Exception {
    StringBuffer buffer = new StringBuffer();
    buffer.append("foo" + new Date() + "bar");
  }
}

(see also bug 492230)

This will result in two StringBuffers being initialised; one to perform the implicit String concatenation, and the other initialised explicitly on the first line.

We should generate a warning when this occurs and offer a quick-fix to re-write this as:

buffer.append("foo").append(new Date()).append("bar");
Comment 1 Noopur Gupta CLA 2016-04-23 02:14:42 EDT
Moving to JDT Core for consideration.