简体   繁体   English

抑制 IntelliJ IDEA 中的“变量从未分配”警告

[英]Suppress "variable is never assigned" warning in IntelliJ IDEA

We use reflection extensively to set class field values in our code.我们在代码中广泛使用反射来设置 class 字段值。 The fields are accessed in code but they are never assigned except via reflection.这些字段是在代码中访问的,但除了通过反射之外,它们永远不会被分配。 So IDEA displays "is never assigned" warning.因此 IDEA 显示“从未分配”警告。 If I ask IDEA to suppress the inspection, it inserts如果我要求 IDEA 禁止检查,它会插入

@SuppressWarnings({"UnusedDeclaration"})

but this also disables the check of whether the field is used or not, which we do not want.但这也禁用了对该字段是否被使用的检查,这是我们不想要的。

Is it anyhow possible to disable only "not assigned" check and leave "not used" check for specific fields only?是否可以仅禁用“未分配”检查并仅对特定字段保留“未使用”检查?

IDEA version is 10.5 IDEA 版本为 10.5

You could use an annotation to mark it as an injected field.您可以使用注释将其标记为注入字段。 (similar to how it would treat @EJB ). (类似于对待@EJB的方式)。 The IntelliJ inspections (at least with version 10.5) allow you to configure your own annotations to mark fields as being injected. IntelliJ 检查(至少在 10.5 版中)允许您配置自己的注释以将字段标记为被注入。

Select Analyze, Inspect Code from the menu and then go to the unused declaration inspection and you can configure an annotation. Select 从菜单中分析,检查代码,然后从 go 到未使用的声明检查,您可以配置注释。

  1. run analysis: Analyze > Inpect Code...运行分析:分析 > 检查代码...
  2. Right click on Unused Declaration (below the Declaration redundancy tree node)右键单击未使用的声明(在声明冗余树节点下方)
  3. Click on the "Configure Annotations..."单击“配置注释...”
  4. Add com.google.google.inject.Inject and javax.inject.Inject添加 com.google.google.inject.Inject 和 javax.inject.Inject

To configure annotations in Android Studio 1.1.0 after inspecting your code right click Unused declaration -> Edit Settings -> Configure annotations.要在检查代码后在 Android Studio 1.1.0 中配置注释,请右键单击未使用的声明 -> 编辑设置 -> 配置注释。

安卓工作室 1.1.0

安卓工作室 1.1.0

If you popup the actions hint window (alt + Enter on Mac), it should suggest you to suppress warning for Inject annotation如果您弹出操作提示 window(Mac 上的 alt + Enter),它应该建议您取消对注入注释的警告

Settings -> Editor -> Inspections -> Declaration Redundancy -> Unused declaration -> Entry points -> Annotations设置 -> 编辑器 -> 检查 -> 声明冗余 -> 未使用的声明 -> 入口点 -> 注释

A new popup opens, split into 2 different areas: "Mark as entry point if annotated by" and "Mark field as implicitly written if annotated by".一个新的弹出窗口打开,分为 2 个不同的区域:“如果由注释标记为入口点”和“如果注释由标记字段为隐式写入”。 Only in the latter you click the "+" icon (Add Annotation Class) and select Autowired annotation (or whatever annotation you're using for dependency injection).只有在后者中,您单击“+”图标(添加注释类)和 select 自动装配注释(或您用于依赖注入的任何注释)。

This will disable the warning "private field is never assigned" but will not disable the warning "private field is never used" in case you never use the field in the class code, which is the desirable behaviour.如果您从不使用 class 代码中的字段,这将禁用警告“从不分配私有字段”,但不会禁用警告“从不使用私有字段”,这是理想的行为。

在此处输入图像描述

This worked on IntelliJ IDEA Community Edition 2018.3.3 and 2020.2这适用于 IntelliJ IDEA 社区版 2018.3.3 和 2020.2

Nope, it seems that IDEA's inspection is not that fine grained.不,IDEA 的检查似乎没有那么细。 Even with annotation based dependency injected fields the same warning can be suppressed for fields annotated with @Inject .即使使用基于注释的依赖注入字段,对于使用@Inject注释的字段,也可以抑制相同的警告。 Automatically the warning "not used" is suppressed.自动抑制“未使用”警告。

I've just tried running FindBugs-IDEA against the class and no warnings or errors were raised.我刚刚尝试针对 class 运行 FindBugs-IDEA 并且没有引发警告或错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM