Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ int getLengthComp(String a, String b, String c) {
return a.length() + b.length() + c.length();
}

int getLengthNoUsageAtAll(String a, String b, String c) {
// Do not report in this case: lack of usage may indicate unreliable data about the symbol
// and this case should also be covered by S1481 and S1854.
List<String> strings = new ArrayList<>();
return a.length() + b.length() + c.length();
}

int getLengthCompTested(String a, String b, String c) {
List<String> strings = new ArrayList<>();
strings.add(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import org.sonar.plugins.java.api.tree.VariableTree;

/**
* Check for collections that are new read, that is the only operations are adding and removing elements.
* Check for collections whose content is not used,
* that is, the only operations on them are adding and removing elements.
*/
@Rule(key = "S4030")
public class UnusedCollectionCheck extends IssuableSubscriptionVisitor {
Expand All @@ -52,6 +53,9 @@ public void visitNode(Tree tree) {
if (symbol.type().isSubtypeOf("java.util.Collection") &&
symbol.isLocalVariable() &&
isInitializedByConstructor(variableTree.initializer()) &&
// Do not raise without any usage: usages() may be unreliable,
// moreover, this case is in scope of S1481 and S1854 (unused variable and assignment)
!symbol.usages().isEmpty() &&
symbol.usages().stream().allMatch(UnusedCollectionCheck::isWriteOnly)) {
reportIssue(variableTree.simpleName(), "Consume or remove this unused collection");
}
Expand Down