CodeQL documentation

Useless upcast

ID: cs/useless-upcast
Kind: problem
Security severity: 
Severity: warning
Precision: medium
   - 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.


Remove the redundant cast.


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.


  • © GitHub, Inc.
  • Terms
  • Privacy