根据数据流分析报告从未到达的代码。 可能是前一个始终为 true 或始终为 false 的条件、无法到达的循环主体或 catch 部分的结果。 通常(尽管并非总是)无法到达的代码是先前警告的结果,因此请检查形成“为 null 性和数据流问题”、“常量值”或“空容器上的冗余操作”的检查警告,以更好地了解原因。

示例:


  void finishApplication() {
    System.exit(0);
    System.out.println("Application is terminated"); // 不可到达的代码
  }

请注意,此检查依赖于方法约定推断。 特别是,如果您调用始终引发异常的 static 或 final 方法,将推断“始终失败”约定,并且方法调用之后的代码将被视为不可到达。 例如:


  void run() {
    performAction();
    System.out.println("Action is performed"); // 不可到达的代码
  }
  
  static void performAction() {
    throw new AssertionError();
  }

如果使用任何类型的代码后处理,这可能会导致误报,例如,如果注解处理器稍后将方法体替换为有用的内容。 为了避免误报警告,请使用 org.jetbrains:annotations 软件包中的显式 @org.jetbrains.annotations.Contract 注解来禁止自动约定推断:


  void run() {
    performAction();
    System.out.println("Action is performed"); // 不再有警告
  }

  @Contract("-> _") // 实现将被替换
  static void performAction() {
    throw new AssertionError();
  }

2024.1 最新变化