在 IntelliJ 中禁用一行的警告

我有一个 Java 代码行,其中 IntelliJ 显示一个警告。如何在不影响其他行中显示的警告的情况下,使该特定行中的警告静音?

在这个问题中,实际的警告是什么并不重要: 现在我不是在寻求关于如何提高一段特定 Java 代码的质量的建议,而是想知道一般情况下如何防止 IntelliJ 在一个特定的 Java 源代码行上显示警告。

55570 次浏览

Depending on the warning you can use @SuppressWarnings. Eg:

@SuppressWarnings("deprecation")
yourLineWhichIsDeprecated;

Take a look at this answer for a pretty long list of warnings you can suppress. Check the other posts on that topic for more details.

Mostly in IntelliJ, you can click on the line and Alt+Enter, and it will have options for suppressing the warning, among other things.

Expanding upon Ryan Stewart's answer, in IntelliJ, use Alt+Enter, then select the first sub-menu, then the last item: Suppress for statement.

enter image description here

Update

Using IntelliJ IDEA 13, I noticed an additional menu item: "Suppress for statement with comment". This will use the IntelliJ style //noinspection unchecked. If you select "Suppress for statement", IntelliJ will insert this text: @SuppressWarnings("unchecked").

In IntelliJ 15 the inline "yellow bulb" and alt-enter menus don't offer to suppress the inspection for 1 line.

There are more options when running the inspections via the Menu: Analyze -> Inspect Code....

Then on the Inspection panel the right side offers various options. Some of text in the right hand panel is clickable. Note that usually the problem resolution function (quick fix) is also available.

enter image description here

(Apparently @tino already noticed this in a comment, but I missed his comment the first time. I'm adding this as full answer to make the important bit I personally missed easier to find.)

When compiling code using Kotlin language and IntelliJ here is a hint

Here is a link to the github source where the compiler warnings have their origin and where the default error messages output by the Kotlin compiler are defined

kotlin/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java

If the compiler outputs "Variable ''{0}'' is never used" it origins from this line form DefaultErrorMessages.java

MAP.put(UNUSED_VARIABLE, "Variable ''{0}'' is never used", NAME);

To suppress the warning you can put a @Suppress() annotation before an unused variable in your code like this:

@Suppress("UNUSED_VARIABLE")
var y: Int = 3

So the trick if the IntelliJ does not help you pop up suggestions pressing Alt+ENTER at a highlighted expression is to look inside DefaultErrorMessages.java if you can find the error message and the keyword to supress a particular warning using @Suppress(..names)

This topic is not marked "Kotlin" but at least marked IntelliJ

//noinspection unchecked,ConstantConditions

@SuppressWarnings does not work in every place

  1. Click on the complaining area that has a wavy line 〰️ beneath it
  2. A light bulb 💡 appears, click the light bulb 💡
  3. Select the suppress statement option

enter image description here

4. An no inspection comment will be added above current line //noinspection CssInvalidPropertyValue

  1. The complaining disappear 👏

As other questions point out, there are several options to achieve what are you asking for. A more comprehensive list of the options available can be found in the official documentation here.

Just one more note. If you are looking for a answer to suppress all warnings for a next line (or part of the code). It might be a reason for a several cases:

  • Idea doesn't provide error name, or suggestions

  • Number of warnings for next line is too large

You can use just:

    //noinspection ALL

Problems panel

The Problems panel shows a list of warnings and errors in our code. There we can peruse the various issues with our code, working through the list one-by-one. This feature arrived in IntelliJ 2020.2.

In at least IntelliJ 2021.2, and perhaps earlier, we can suppress a warning within that panel.

When selecting a problem point, the right-side pane of the Problems panel shows a Suppress widget. This pop-up menu displays items for various ways to suppress the warning.

enter image description here