Container contents are never accessed¶
ID: cs/unused-collectionKind: problemSecurity severity: Severity: errorPrecision: highTags: - quality - maintainability - useless-code - external/cwe/cwe-561Query suites: - csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
If the contents of a collection are never used, then it is useless and therefore unnecessary. This adds performance overhead, obscures the code, and may indicate an error in the logic.
Recommendation¶
Either remove the collection if it is no longer needed, or ensure that it is used as intended.
Example¶
In this example, the propertyNames returns the wrong collection (genres). This logic error means that thenames collection is populated but never accessed.
classComposers{IList<string>names,genres;publicComposers(){names=newList<string>{"Bach","Beethoven","Chopin"};genres=newList<string>{"Classical","Romantic","Jazz"};}publicIList<string>Names{get{returngenres;}}publicIList<string>Genres{get{returngenres;}}}
The code is fixed by returning the correct field forNames.
classComposers{IList<string>names,genres;publicComposers(){names=newList<string>{"Bach","Beethoven","Chopin"};genres=newList<string>{"Classical","Romantic","Jazz"};}publicIList<string>Names{get{returnnames;}}publicIList<string>Genres{get{returngenres;}}}