CodeQL documentation

Container contents are never accessed

ID: cs/unused-collection
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - maintainability
   - useless-code
   - external/cwe/cwe-561
Query 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 property Names returns the wrong collection (genres). This logic error means that the names collection is populated but never accessed.

class Composers
{
    IList<string> names, genres;

    public Composers()
    {
        names = new List<string> { "Bach", "Beethoven", "Chopin" };
        genres = new List<string> { "Classical", "Romantic", "Jazz" };
    }

    public IList<string> Names
    {
        get { return genres; }
    }

    public IList<string> Genres
    {
        get { return genres; }
    }
}

The code is fixed by returning the correct field for Names.

class Composers
{
    IList<string> names, genres;

    public Composers()
    {
        names = new List<string> { "Bach", "Beethoven", "Chopin" };
        genres = new List<string> { "Classical", "Romantic", "Jazz" };
    }

    public IList<string> Names
    {
        get { return names; }
    }

    public IList<string> Genres
    {
        get { return genres; }
    }
}
  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy