2

What is wrong with the SuppressWarnings annotation above the if statement? Eclipse with Sun JDK 6 provides two syntax error descriptions, both unhelpful and hard to understand, shown in comments.

class TestDeadCode
{
    //@SuppressWarnings("all")
    public static void main(String[] args)
    {
        @SuppressWarnings("all")  // syntax errors: insert enum body, insert enum id
        if ((Constants.flag0) && (Constants.flag1))
            System.out.println("hello\n");      
    }
}

interface Constants
{
    boolean flag0 = false;
    boolean flag1 = false;
}
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114

1 Answers1

8

Only classes, methods, variable declarations, parameters and packages may be annotated. Therefore, you cannot use SuppressWarnings("all") on an if statement.

To fix this issue, you can simply do the following.

@SuppressWarnings("all")
boolean flag = Constants.flag0 && Constants.flag1;
if (flag) {
    System.out.println("hello\n");
}

There is no SuppressWarnings("Dead code") as of yet.

http://docs.oracle.com/javase/7/docs/api/java/lang/SuppressWarnings.html http://pmd.sourceforge.net/suppressing.html

JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
  • bah, just beat me to the post! – Green Day Jan 14 '12 at 07:14
  • Additionally, you can tell eclipse to ignore the dead code warning. Windows > Preferences > Java > Compiler > Errors/Warnings In the "Potential programming problems" section towards the bottom is "Dead Code" – JustinDanielson Jan 14 '12 at 07:15