CodeQL 文档

无用的向上转型

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

点击查看 CodeQL 代码库中的查询

在大多数情况下,如果表达式存在相应的隐式转换,则对其进行强制转换是没有意义的。

建议

删除冗余的强制转换。

示例

在此示例中,从 Sub 显式强制转换为 Super 是冗余的。

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

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

可以通过删除显式强制转换或将 super 设为隐式类型 (var) 变量来修复上面的代码。

参考资料

  • ©GitHub 公司
  • 条款
  • 隐私