102

I have a Java code line where IntelliJ displays a warning. How do I silence the warning in that particular line, without affecting warnings displayed in other lines?

In this question it's irrelevant what the actual warning is: now I'm not seeking advice on how to improve the quality of a specific piece of Java code, but I want to know in general how to prevent IntelliJ from displaying a warning on a specific Java source line.

kat
  • 1,955
  • 4
  • 15
  • 13
  • That's really not enough detail for someone to answer. What warning? What's on the line? – Jim Garrison Oct 17 '11 at 20:04
  • More information about the warning would be helpful. Warnings do exist for a reason and its often better to fix the underlying problem than to ignore the warning. – Freiheit Oct 17 '11 at 20:27
  • 12
    In this question the specific warning is irrelevant. I want to know in general what can I do to disable warnings reported by IntelliJ for a specific line (in addition to fixing my code), for all possible warnings. – kat Oct 17 '11 at 20:39
  • 1
    For instance: Intellij complains that `password.replaceAll(".","*")` is usually a mistake. It thinks you meant `replaceAll("\.",...)`. Thanks, but no. So now I want to acknowledge and suppress the warning. I don't want it to hang around. – Florian F Jul 08 '17 at 05:54

10 Answers10

99

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

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 23
    It doesn't have such an option in my case. It offers some options to fix the root cause of the warning, but I don't want that now. I just want to get rid of the warning. – kat Oct 17 '11 at 20:43
  • 39
    @kat, you need to press right arrow key to open the inspection sub-menu from where you can suppress it for current line, which will add the annotation automatically. – CrazyCoder Oct 17 '11 at 21:14
  • 6
    It works now as you (Ryan Stewart and CrazyCoder) wrote: I press F2 to jump to the next warning, then I press Alt-Enter to see the warning, then I press the Right arrow key to get options to fix the problem, then I select the appropriate Ignore... option. FYI Just hovering the mouse over the warning line displays a tooltip using which it's impossible to get rid of the warning. – kat Oct 17 '11 at 22:36
  • 3
    Current IntelliJ allows to use "Analyze" : "Inspect Code" which opens a lower pane. On the right pane there is a convenient "Suppress for statement" on the right side. Needs a big screen and a mouse, though. – Tino Jan 02 '14 at 12:09
  • I do not find any of the suggestions or comments on a Mac. – Dirk Schumacher Jul 04 '21 at 06:08
  • 2022 and intellij still has very bad UI design with respect to this issue. So hard to find the suppress option. – Robert Mar 03 '22 at 00:44
  • This suggestion is not often true. For example, here is my suggestions after doing alt+enter on a warning: https://i.imgur.com/XrGUzGm.png – Hatefiend Feb 12 '23 at 17:55
66

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").

kevinarpe
  • 20,319
  • 26
  • 127
  • 154
16

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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • Thank you for the background information, it's good to know how SuppressWarning works. However, I was looking for an IntelliJ-specific solution. – kat Oct 17 '11 at 22:38
11

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.)

Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
4

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
GensaGames
  • 5,538
  • 4
  • 24
  • 53
3

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2
  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
sangelee
  • 155
  • 1
  • 8
0

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

flodis
  • 1,123
  • 13
  • 9
0

//noinspection unchecked,ConstantConditions

@SuppressWarnings does not work in every place

telebog
  • 1,706
  • 5
  • 25
  • 34
0

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.

Paolo
  • 515
  • 5
  • 9