Bug 335377 - Provide a @SuppressWarnings pragma for "Comparing identical expressions" compiler warning
Summary: Provide a @SuppressWarnings pragma for "Comparing identical expressions" comp...
Status: VERIFIED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: 3.7 M6   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-25 15:21 EST by Kostya Vasilyev CLA
Modified: 2011-03-08 08:40 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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