【问题标题】:Eclipse missing warning if variable used before assigned如果在分配之前使用了变量,则 Eclipse 缺少警告
【发布时间】:2018-03-02 08:17:54
【问题描述】:

我们在旧代码中发现了一个错误,如果有警告,很容易找到,

问题是内部类成员已使用但未分配或初始化。示例代码最小化。问题是url 始终为空。

 public class Main {
    public class Sender {
       private String url;

        public Sender() {
         }
         public void send() {
             OtherClass.send(url);
         }
      }
   }

intellij 中,它会警告从未分配过的变量。在eclipse中我没有找到这样的警告。

代码更复杂,在任何级别添加警告都会减少查找类似错误的时间。

如果在分配/初始化之前使用变量,eclipse是否可以以某种方式发出警告?

【问题讨论】:

    标签: eclipse


    【解决方案1】:

    您可以使用 SpotBugs(findbugs 的继任者)eclipse 插件。右键单击项目 Spotbugs->find bugs 这将找到这些类型的错误。

    我建议也安装 sonarlint 插件,它具有良好的静态分析能力。

    https://marketplace.eclipse.org/content/spotbugs-eclipse-plugin

    https://marketplace.eclipse.org/content/sonarlint

    【讨论】:

    • SpotBugs 没有发现问题,我应该配置它吗?如何配置?
    • @user7294900 我复制了您的示例代码,将 otherclass.send 替换为 system.out.print 方法。它确实在 system.out.print 行找到它。我正在使用氧气 1.a 和最新的 SpotBugs
    • 它会警告您private String url 吗?未初始化就使用过
    • @user7294900 选中项目后右键 Spotbugs->find bugs,里面有一个名为Bug explorer 的视图,它被列为This field is never written. All reads of it will return the default value. Check for errors (should it have been initialized?), or remove it if it is useless. 你可以打开这个视图窗口-> 显示视图->其他查看和搜索错误资源管理器。在源代码中,该行附近有一个小错误图标
    • @user7294900 我刚刚意识到你的发送方法需要参数 url,它是从哪里调用的。如果从外部调用,则不会使用 Sender 类中的 url。
    猜你喜欢
    • 2013-02-23
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2021-08-16
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多