Bug 335377

Summary: Provide a @SuppressWarnings pragma for "Comparing identical expressions" compiler warning
Product: [Eclipse Project] JDT Reporter: Kostya Vasilyev <kman>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED WONTFIX QA Contact:
Severity: enhancement    
Priority: P3 CC: markus.kell.r, Olivier_Thomann, satyam.kandula
Version: 3.7   
Target Milestone: 3.7 M6   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Kostya Vasilyev CLA 2011-01-25 15:21:58 EST
Build Identifier: 20100917-0705 

This sample code is used to switch debug/release builds:

public class BuildConfig {
	/**
	 * Debug vs. production build
	 */
	public static final int BUILD_CONFIG_PRODUCTION = 1;
	public static final int BUILD_CONFIG_INTERNAL_DEBUG = 2;

	public static final int which = BUILD_CONFIG_INTERNAL_DEBUG;
}

And then:

if (BuildConfig.which == BuildConfig.BUILD_CONFIG_INTERNAL_DEBUG) {
     // blah blah blah
}

This produces a Java compiler warning, "Comparing identical expressions".

The only way to suppress it is to use @SuppressWarnings("all"), but that's too broad, it would be nice to have a specific pragma value.


Reproducible: Always

Steps to Reproduce:
1. Create a Java class like this:

public class BuildConfig {
	/**
	 * Debug vs. production build
	 */
	public static final int BUILD_CONFIG_PRODUCTION = 1;
	public static final int BUILD_CONFIG_INTERNAL_DEBUG = 2;

	public static final int which = BUILD_CONFIG_INTERNAL_DEBUG;
}

2. Use it like this:

if (BuildConfig.which == BuildConfig.BUILD_CONFIG_INTERNAL_DEBUG) {
     // blah blah blah
}

3. You will get a Java compiler warning "Comparing identical expressions".

The only way to suppress it is to use @SuppressWarnings("all"), but that's too broad, it would be nice to have a specific pragma value.
Comment 1 Markus Keller CLA 2011-02-11 06:51:35 EST
I'm not sure it's really worth adding a special token for that. Note that if you compile with ...

    public static final int which = BUILD_CONFIG_PRODUCTION;

you'd two new warnings:
- unused @SuppressWarnings("identical-expression")
- the "// blah blah blah" block is dead code


A better solution would be to just initialize the "which" like this:

    public static final int which;
    static {
        which= BUILD_CONFIG_INTERNAL_DEBUG;
    }

That avoids all these problems. For the final production build, you can of course still initialize the constant directly.
Comment 2 Olivier Thomann CLA 2011-02-11 10:24:24 EST
Agreed. Closing as WONTFIX.
Comment 3 Satyam Kandula CLA 2011-03-08 08:40:29 EST
Verified for 3.7M6