squid:S2175 - Inappropriate "Collection" calls should not be made

437 views
Skip to first unread message

fleisch...@gmail.com

unread,
Mar 4, 2016, 5:49:44 AM3/4/16
to SonarQube
Hi there,

according to the rule squid:S2175 (Inappropriate "Collection" calls should not be made) the following code should raise an issue:

Map<MyType, String> myMap = new HashMap<>();
String value = myMap.get(new UnrelatedType());


Obviously myMap will never contain any element of type UnrelatedType because the keys are of type MyType.
SonarQube (v. 5.3) does not raise this issue here.
Neither does it with a Map:

List<MyType> myList = new ArrayList<>();
boolean contains = myList.contains(new UnrelatedType());
myList.remove(new UnrelatedType());

The documentation of the rule even states explicitly this example:

List<String> list = new ArrayList<String>();
Integer integer = Integer.valueOf(1);
if (list.contains(integer)) { // Noncompliant. Always false.
    list.remove(integer); // Noncompliant. list.add(integer) doesn't compile, so this will always return false
}

The Profile SonarWay is activate for this project.
What might be going wrong here?

Thank you very much for your help.
Regards, Simon

Michael Gumowski

unread,
Mar 4, 2016, 8:51:51 AM3/4/16
to fleisch...@gmail.com, SonarQube
Hello Simon,

Thanks for your feedback! About the point you are rising:
  • As stated in its description, the rule only targets methods 'remove(Object o)' and 'contains(Object o)' of 'Collection'. As you are not calling such methods, on top of the fact that a 'Map' is not a 'Collection'(meaning the Map interface is not a subtype of the Collection interface), your first code snippet won't raise any issue.  Improving the rule to also cover such cases could however be a good improvement of the rule!
  • Regarding your second example, it works perfectly on my side, as long as you are providing the bytecode for the analysis. Without the bytecode, the analysis engine won't be able to resolve MyType and UnrelatedType. This types being unknown, the rule won't do any assumptions in order to not raise false positives. 
  • This example should perfectly work:
class L {
  void foo() {
    List<MyType> myList = new ArrayList<>();
    boolean contains = myList.contains(new UnrelatedType());
    myList.remove(new UnrelatedType());
  }
  
  class MyType {}
  class UnrelatedType {}
}

I hope this helps,

Regards, 

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/80f41d6a-3f9c-449c-af44-67680dcaf2a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fleisch...@gmail.com

unread,
Mar 7, 2016, 8:50:35 AM3/7/16
to SonarQube, fleisch...@gmail.com
Hi Michael,

you are absolutely right. Me example with Map can't work.
And the List example did not work because I did not analyze class files.
I'm currently trying to do so, but SonarRunner can't find the class files.
Thank you very much for youtr answer, anyway! It helped a lot!

Regards,
Simon
Reply all
Reply to author
Forward
0 new messages