Search code examples
javaannotationssuppress-warnings

Weird result from "@SuppressWarnings"


Eclipse was warning me that a local variable randInt might not be initialized (it was). So I added the first line:

@SuppressWarnings("all")
return randInt;

The warning went away, but I'm getting two new errors on the first line: Syntax error: insert "enum Identifier" to complete EnumHeaderName, and Syntax error: insert "EnumBody" to complete BlockStatement

What on earth? It's surprisingly hard to find information about @SuppressWarnings. Is there a more precise way of getting rid of this specific warning than using "all"?

Minimal, complete, verifiable example:

public class SuppressTest {
    public int cut() { 
        int randInt = 0; 
        @SuppressWarnings("all") 
        return randInt;
    }
}

Solution

  • You can't insert @SuppresWarnings on a return statement. In java 8 you can annotate only classes, methods/constructors, fields, parameters and (new in java 8) local variables.

    So in your case java can't parse what you have written. Move the @SuppressWarnings at the method level.