【问题标题】:SonarQube Complaint Either remove this useless object instantiation of class or use itSonarQube 投诉要么删除这个无用的类对象实例化,要么使用它
【发布时间】:2021-02-11 06:01:10
【问题描述】:

我正在尝试修复我从 SonarQube 获得的这个错误,但他们的解决方案建议对我的情况不太有帮助。 我从他们那里得到的建议是:“没有充分的理由创建一个不对其做任何事情的新对象。大多数情况下,这是由于缺少一段代码,因此可能导致意外行为正在生产中。”

任何关于如何处理的建议将不胜感激。

private RootElement GetParentAsRoot(Element element, string method) {
        if (element.Parent == null) {
            new RootElement(element, NewControlsNotifier);
       //Either remove this useless object instantiation of class 'RootElement' or use it.
        }
        var root = element.Parent as RootElement;
        if (root == null) {
            throw new ArgumentException(method + " method is applicable only on top-most element");
        }
        return root;
    }

【问题讨论】:

    标签: c# object sonarqube


    【解决方案1】:

    导致此问题的代码如下:

    new RootElement(element, NewControlsNotifier);
    

    您正在使用 new 运算符创建 RootElement,但忽略了它的结果。您应该完全删除对象创建或使用您创建的对象。

    如果你想返回你的对象,试试这个:

    private RootElement GetParentAsRoot(Element element, string method) {
            if (element.Parent == null) {
                return new RootElement(element, NewControlsNotifier);
            }
            var root = element.Parent as RootElement;
            if (root == null) {
                throw new ArgumentException(method + " method is applicable only on top-most element");
            }
            return root;
        }
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2017-11-23
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      相关资源
      最近更新 更多