String concat in a loop annoyance

Does anyone find this inspection to be a bit..dumb?

I have code like this in a number of places:

for(item : items)
collection.add(prefix + item);

This looks like a perfectly legitimate practice to me, and using a stringbuffer would gain absolutely nothing, yet the inspection complains. Is there any way to make it detect this sort of construct and not whine about it?

2 comments
Comment actions Permalink

Hani Suleiman wrote:

Does anyone find this inspection to be a bit..dumb?

I have code like this in a number of places:

for(item : items) collection.add(prefix + item);

This looks like a perfectly legitimate practice to me, and using a
stringbuffer would gain absolutely nothing, yet the inspection
complains. Is there any way to make it detect this sort of construct
and not whine about it?


You're right. I have wanted to fix this for quite some time, but never
got around to it. Short of disabling the inspection, or suppressing the
inspection there's no solution (before the release of IDEA 5).
Personally I have disabled this inspection.
You should submit a JIRA issue about it.

Bas

0
Comment actions Permalink


There's a switch on the inspection to only complain if the concatenation is used in an assignment. Turning that on will probably make the inspection do what you expect. I will say the stock semantics still has value, in highly performance critical environments, or times where you're looking to track down memory leaks. There's an inspection for any uses of allocations in loops, just for that reason. I would probably agree that the flag should be set more liberally by default, though. The current default setting is there purely for reasons of inertia.

--Dave Griffith

0

Please sign in to leave a comment.