-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SPP_NO_CHAR_SB_CTOR conflicts with PMD performance rule. #397
Comments
I understand that this bug falls under the category of SillyPotPourri of bugs in fb-contrib. As someone who uses other static code analyzers such as Checkstyle and PMD in conjunction with Spotbugs, having to suppress this conflict every time is more than a minor irritant. |
I'm unclear on why you think this is a false positive. Can you provide a sample of code that triggers the detector but should not? |
It's not a false positive. |
The point of the rule is to catch situations like:
Presumably the developer intended to create a new StringBuilder with the default capacity, containing a single letter 'a'. But instead, what they will get is an empty StringBuilder with capacity 97 (the numeric value of 'a'). They should instead use If the rule fires on actual integers, then that would be a bug. Can you provide a code sample where that occurs? |
I find it catching intentional explicit integer values as well---the
number of characters estimated beforehand. It should be more restrictive
so that it catches single-quoted character only and not integer values (which specify the initial buffer size).
This is all quite evident in the original post itself.
I've edited the OP with code samples.
|
The rule
SPP_NO_CHAR_SB_CTOR from fb-
contrib
"This method constructs a StringBuffer or a StringBuilder using the constructor that takes an integer, but appears to pass a character instead. It is probable that the author assumed that the character would be appended to the StringBuffer/Builder, but instead the integer value of the character is used as an initial size for the buffer."
conflicts with the following PMD performance rule:
InsufficientStringBufferDeclaration
'Failing to pre-size a StringBuffer or StringBuilder properly could cause it to re-size many times during runtime. This rule attempts to determine the total number the characters that are actually passed into StringBuffer.append(), but represents a best guess "worst case" scenario. An empty StringBuffer/StringBuilder constructor initializes the object to 16 characters. This default is assumed if the length of the constructor can not be determined.'
The rule
SPP_NO_CHAR_SB_CTOR can be dropped.
I see no reason for this rule to exist which is evidently only catching a typo on the part of the user.
From what I understand, this rule intends to prevent a programmer from initialising a String[Builder|Buffer] with a size equal to the character specified in single quotes when the programmer intended to initialise the object with size 1 containing the said character.
The correct way is to append the char to the object.
I don't see why an integer value should be flagged, though. Only a character in quotes ought to be flagged.
There exists another PMD rule StringBufferInstantiationWithChar which disallows initialization of StringBuffer/StringBuilder with a char.
The text was updated successfully, but these errors were encountered: