CodeQL documentation

Useless upcast

ID: cs/useless-upcast
Kind: problem
Security severity: 
Severity: warning
Precision: medium
Tags:
   - maintainability
   - language-features
   - external/cwe/cwe-561
Query suites:
   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

In most situations, casting an expression where there exists a corresponding implicit conversion serves no purpose.

Recommendation

Remove the redundant cast.

Example

In this example, casting explicitly from Sub to Super is redundant.

class Bad
{
    class Super {}
    class Sub : Super {}

    void M()
    {
        var sub = new Sub();
        Super super = (Super)sub;
    }
}

The code above can be fixed, either by removing the explicit cast, or by making super an implicitly typed (var) variable.

References

  • © 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