summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java
blob: 5c53674cd15b1a52784cdcfa9540efc3b5cc331e (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.siyeh.igtest.bugs.mismatched_collection_query_update;

import java.util.*;
import java.io.FileInputStream;

public class MismatchedCollectionQueryUpdate {
    private Set foo = new HashSet();
    private Set <warning descr="Contents of collection 'foo2' are queried, but never updated">foo2</warning> = new HashSet();
    private Set <warning descr="Contents of collection 'bar' are queried, but never updated">bar</warning> ;
    private Set bar2 = new HashSet(foo2);
    private Set bal ;

    public void bar()
    {
        foo.add(new Integer(bar.size()));
        final boolean found = foo.add(new Integer(bar2.size()));
    }

    public void bar2()
    {
        final List <warning descr="Contents of collection 'barzoom' are updated, but never queried">barzoom</warning> = new ArrayList(3);
        barzoom.add(new Integer(3));
    }

    public List bar4()
    {
        final List barzoom = new ArrayList(3);
        barzoom.add(new Integer(3));
        return true?null:barzoom;
    }

    public List bar3()
    {
        final List barzoom = new ArrayList(3);
        barzoom.add(new Integer(3));
        return barzoom;
    }

    class Node{
        private SortedSet mChildren;

        public synchronized SortedSet getChildren(){
            if(mChildren == null){
                mChildren = queryChildren();
            }
            return mChildren;
        }

        private SortedSet queryChildren(){
            return null;
        }
    }
    static class SNMP {
        public static final int AGENT_PORT;
        public static final int MANAGER_PORT;

        static {
            int agentPort;
            int mgrPort;
            try {
                Properties p = new Properties();
                p.loadFromXML(new FileInputStream("config/comms.xml"));
                agentPort = Integer.parseInt(p.getProperty("snmp.agent.port"));
                mgrPort = Integer.parseInt(p.getProperty("snmp.manager.port"));
            } catch (Exception e) {
                agentPort = 161;
                mgrPort = 162;

            }
            AGENT_PORT = agentPort;
            MANAGER_PORT = mgrPort;
        }
    }

    class A {
        private final List<String> list = new ArrayList();

        void foo(String s) {
            list.add(s);
        }

        boolean bar(String s, boolean b, Set<String> set2) {
            return (b ? list : set2).contains(s);
        }
    }

    class B {
        private final List<String> list = new ArrayList();

        boolean foo(String s) {
            return list.contains(s);
        }

        void bar(String s, boolean b, Set<String> set2) {
            (b ? list : set2).add(s);
        }
    }

    class C {
        private final java.util.concurrent.BlockingDeque deque =
        new java.util.concurrent.LinkedBlockingDeque();

        {
            try {
                final Object o = deque.takeFirst();
                deque.putLast(o);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    class Test {
        public String add(final String value) {
            final List<String> list = getList();
            list.add(value);
            return value;
        }

        private List<String> getList() {
            return null;
        }
    }

  private static String foos() {
    final List bar = new ArrayList();
    bar.add("okay");
    return "not " + bar + "";
  }

  void methodArgument() {
    List<String> <warning descr="Contents of collection 'foos' are updated, but never queried">foos</warning> = new ArrayList<>();
    List<String> <warning descr="Contents of collection 'bars' are queried, but never updated">bars</warning> = new ArrayList<>();

    foos.removeAll( bars );

    List<String> other = new ArrayList<>();
    other.add("a");
    m(other);
  }

  void m(List l) {}

  public void foo()
  {
    final Set localFoo = foo;
  }

  public void foofoo()
  {
    final Map<String, String> <warning descr="Contents of collection 'anotherMap' are queried, but never updated">anotherMap</warning> = new HashMap<String, String>();
    final SortedMap<String, String> map = new TreeMap<String, String>(anotherMap);
    final Iterator<String> it = map.keySet().iterator();
    while(it.hasNext()){
      Object o = it.next();

    }
  }

  class MyClass {
    private  List<String> ourValues = new ArrayList<String>() {{
      Collections.addAll((this), "A", "B", "C");
    }};

    public void foo() {
      for (final String value : ourValues) {}
    }
  }

  class MyClass2 {
    private  List<String> ourValues = new ArrayList<String>() {{
      (this).add("");
    }};

    public void foo() {
      for (final String value : ourValues) {}
    }
  }

  private void updateAttachmentWarning(final String message) {
    final List<String> includedAttachments;
    if (message instanceof Object &&
        !(includedAttachments = boo()).isEmpty()) {
      if (includedAttachments.size() == 1) {
      }
    }
  }

  List<String> boo() {return null;}
}

class MethReference<E> {
    private String foo(){
        List<E> list = new ArrayList<>();
        forEach(list::add);
        return list.toString();
    }

  void copyStuff(List other) {
    List <warning descr="Contents of collection 'list' are updated, but never queried">list</warning> = new ArrayList();
    J j = list::add;
  }

    private void forEach(I<E> ei) {}
    interface I<E> {
        boolean _(E e);
    }
    interface J<E> {
      void m(E e);
    }

    void qTest() {
        Map<Integer, Boolean> map = new HashMap<>();
        map.put(1, true);
        I<Integer> mapper = map::get;
    }

  void foo(int a,Collection b) {
    final ArrayList x = new ArrayList();
    x.add("1");

    for (Object o : a>0? b : x)
    {
      System.out.println(o);
    }
  }
}
class CollectionsUser {
  void a(List<String> list) {
    List<String> l = new ArrayList();
    Collections.addAll(l, "1", "2", "4");
    Collections.copy(list, l);
  }

  int b(List<String> list) {
    List<String> l = new ArrayList();
    Collections.addAll(l, "1", "2", "4");
    return Collections.indexOfSubList(list, l);
  }

  void c() {
    List<String> <warning descr="Contents of collection 'l' are queried, but never updated">l</warning> = new ArrayList();
    final int frequency = Collections.frequency(l, "one");
  }

  List<String> d() {
    List<String> <warning descr="Contents of collection 'l' are queried, but never updated">l</warning> = new ArrayList();
    return Collections.unmodifiableList(l);
  }

  List<String> e() {
    List<String> l = new ArrayList<String>();
    return Collections.checkedList(l, String.class);
  }

  private final List<Object> <warning descr="Contents of collection 'list' are updated, but never queried">list</warning> = new ArrayList<Object>();
  public void add() {
    Collections.addAll(list, "Hello");
  }
}

class SimpleAdd {
  protected String[] doPerform() {
    List<String> result = new ArrayList<String>();
    for (String app : getApplications()) {
      if (app.startsWith("")) {
        result.add(app);
      }
    }
    return result.toArray(new String[result.size()]);
  }

  public String[] getApplications() {
    return null;
  }

}