Disable warning in IntelliJ for one line

后端 未结 7 1115
庸人自扰
庸人自扰 2020-12-15 02:03

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?

相关标签:
7条回答
  • 2020-12-15 02:41

    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.

    0 讨论(0)
  • 2020-12-15 02:45

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

    0 讨论(0)
  • 2020-12-15 02:48

    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

    0 讨论(0)
  • 2020-12-15 02:51

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

    0 讨论(0)
  • 2020-12-15 02:53
    1. Click on the complaining area that has a wavy line 〰️ beneath it
    2. A light bulb
    0 讨论(0)
  • 2020-12-15 02:54

    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.

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

    0 讨论(0)
提交回复
热议问题