Useless upcast¶
ID: cs/useless-upcastKind: problemSecurity severity: Severity: warningPrecision: mediumTags: - quality - maintainability - useless-code - external/cwe/cwe-561Query 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 fromSub toSuper is redundant.
classBad{classSuper{}classSub:Super{}voidM(){varsub=newSub();Supersuper=(Super)sub;}}
The code above can be fixed, either by removing the explicit cast, or by makingsuper an implicitly typed (var) variable.
References¶
Microsoft:Casting and Type Conversions.
Common Weakness Enumeration:CWE-561.