Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suppress Eclipse 3.5's warnings of dead code

I use a class for detecting email addresses which uses static final booleans to configure the matching behavior. Since I upgraded to Eclipse 3.5 I get warnings about dead code, since Eclipse notices that one branch in this can not be reached:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Oddly enough it is happy with this:

private static final String domain;
static {
    if(ALLOW_DOMAIN_LITERALS) {
        domain = rfc2822Domain;
    } else {
        domain= rfc1035DomainName;
    }
}

since it seems to recognize the common if(DEBUG) pattern, but the ternary operator doesn't seem to count.

Since I'd rather not fork the class too much just to keep Eclipse happy, I'd prefer putting an @SuppressWarnings at the top instead of changing the code. Unfortunately I can't find a matching one apart from the brute-force "all". Is there a value just for the dead code detection?

like image 593
Peter Becker Avatar asked Jul 06 '09 05:07

Peter Becker


People also ask

Why does Eclipse say dead code?

In Eclipse, "dead code" is code that will never be executed. Usually it's in a conditional branch that logically will never be entered. A trivial example would be the following: boolean x = true; if (x) { // do something } else { // this is dead code! }


3 Answers

UPDATE: from Adam's comment:

In Eclipse 3.6 and newer Eclipse versions @SuppressWarnings("unused") can now be used to suppress 'dead code' warnings. See Christopher Stock's answer.

See also Eclipse 4.4(Luna) help for @SuppressWarnings.

Original answer:

All SuppressWarnings values Eclipse 3.5 "knows" are listed in this page. It seems that there is no value for suppressing only the new dead-code detection. But you can use the @SuppressWarnings("all") just before the domain declaration so it will suppress warnings for only that line not for the whole class:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
@SuppressWarnings("all") 
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Because dead code check is a new one you can also suggest an enchancement in the Eclipse bug database for supporting the ternary operation as well.

like image 95
Csaba_H Avatar answered Oct 07 '22 02:10

Csaba_H


Select Ignore in Windows -> Preferences > Java > Compiler > Errors/Warnings under Potential programming problems section

like image 35
J-16 SDiZ Avatar answered Oct 07 '22 01:10

J-16 SDiZ


You can disable the 'dead code' warnings using

@SuppressWarnings( "unused" )

See the eclipse documentation for more Information:

http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm

"unused" to suppress warnings relative to unused code and dead code

Greetings

Christopher

like image 7
Christopher Stock Avatar answered Oct 07 '22 00:10

Christopher Stock