Menu

#1369 ConsecutiveLiteralAppends not detected properly on StringBuffer

PMD-5.3.3
closed
None
PMD
3-Major
Bug
5.3.2
ConsecutiveLiteralAppends
2015-06-29
2015-06-10
No

There is an inconsistent behaviour on ConsecutiveLiteralAppends, which is not always reported.

The code below is a sample where there are 2 equally wrong uses of StringBuffer but only one is reported.

Also the ammount of the consecutive ".appends" is not correct

public class StringBufferTest {

    public void test() {

        // This lint is reported as ConsecutiveLiteralAppends, but says ".append is called **5** consecutive times"
        final StringBuffer stringBuffer = new StringBuffer().append("agrego ").append("un ");
        stringBuffer.append("string "); // and in this line says ".append is called **4** consecutive times"
        Log.i(TAG, stringBuffer.toString());

        final StringBuffer stringBuffer2 = new StringBuffer();
        // ConsecutiveLiteralAppends is not reported on any of these lines
        stringBuffer2.append("agrego ");        
        stringBuffer2.append("un ");            
        stringBuffer2.append("string ");        
        Log.i(TAG, stringBuffer2.toString());
    }
}

Discussion

  • Andreas Dangel

    Andreas Dangel - 2015-06-29
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.3
    • Ruleset / Rule: --> ConsecutiveLiteralAppends
     

Log in to post a comment.