-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathReadOnlyContainer.qhelp
More file actions
56 lines (43 loc) · 1.57 KB
/
ReadOnlyContainer.qhelp
File metadata and controls
56 lines (43 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
A method that queries the contents of a collection or map (such as <code>containsKey</code>
or <code>isEmpty</code>) is invoked on an object that is known to be empty. Such method
calls do not return interesting results, and may indicate missing code or a logic error.
</p>
</overview>
<recommendation>
<p>
Either remove the collection/map if it is unnecessary, or ensure that it contains the
elements it was meant to contain.
</p>
</recommendation>
<example>
<p>
The following example code iterates over an array of objects to determine whether it contains
duplicate elements. It maintains a collection <code>seen</code>, which is intended to contain
all the elements seen so far in traversing the array. If the current element is already
contained in that collection then the method returns <code>true</code>, indicating that a
duplicate has been found.
</p>
<p>
Note, however, that no elements are ever actually added to <code>seen</code>, so the method always
returns <code>false</code>.
</p>
<sample src="ReadOnlyContainer.java" />
<p>
To fix this problem, a statement <code>seen.add(o);</code> should be added to the end of the loop
body to ensure that <code>seen</code> is correctly maintained.
</p>
</example>
<references>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html">Collection</a>,
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Map.html">Map</a>.
</li>
</references>
</qhelp>