SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Bug Core 21

Inspired by

Original Description

“DMI: Collections should not contain themselves”

“This call to a generic collection's method would only make sense if a collection contained itself (e.g., if s.contains(s) were true). This is unlikely to be true and would cause problems if it were true (such as the computation of the hash code resulting in infinite recursion). It is likely that the wrong value is being passed as a parameter.” – 2013-10-14

Detailed Description

Bug category : Correctness – Probable bug – an apparent coding mistake resulting in code that was probably not what the developer intended. Should strive for a low false positive rate. Collections should not contain themselves in call to {2.givenClass}.

Sample Problem Scenario

<code Java> s.contains(s); </Code>

This detector looks at the arguments of calls to generic collection methods that receive a java.lang.Object See if the argument’s type is related to the collection’s parameter. Arguments with unrelated class types are never going to be in the collection.

For example, if foo is a List<String> and bar is a StringBuffer, the call

<code Java> foo.contains(bar) </Code>

will always return false. This is a fast detector.

<code Java>

public boolean wrong1(){
	return collection.contains(collection);
}

</Code>

<code Java>

public void wrong2(Collection<String> col){
	col.contains(col);
}

</Code>

<code Java>

public void wrong3(){
	Collection<Integer> colInt = new LinkedList<Integer>();
	colInt.contains(colInt);
}

</Code>

<code Java>

public void wrong4(){
	Collection<Integer> colInt = new LinkedList<Integer>();
	colInt.contains(collection);
}

</Code>

Sample Fix

<code Java> … ??? let the developer decide what to do !!! … </Code>

Evaluation Results

Benchmark project Precision Recall
FB JT Delta FB JT Delta
Project … …% …% …% …% …% …%
Project … …% …% …% …% …% …%

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs

teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor21.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023