Bug 281776 - Should not warn for comparison of identical expression with float type
Summary: Should not warn for comparison of identical expression with float type
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M1   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 251227 301405 309087 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-06-28 11:07 EDT by Stefan Kolarov CLA
Modified: 2010-04-14 07:47 EDT (History)
4 users (show)

See Also:


Attachments
Patch possibility (2.94 KB, patch)
2009-07-16 02:13 EDT, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Kolarov CLA 2009-06-28 11:07:42 EDT
Build ID: 20090621-0832

Steps To Reproduce:
1. In a class, declare variable var of type float or double.
2. Write "if(var != var) ..."
3. You get a warning for comparing identical expressions, while this comparison is idiom for checking for NaN.

There should be an option for turning off identical expression comparison for double and float expression types.

More information:

public class IdenticalExpressionWarningOnDoubles {
	public static void main(String[] args) {
		double var = Double.NaN;
		if(var != var) {
			System.out.println("NaN");
		}
	}
}
Comment 1 Olivier Thomann CLA 2009-06-29 11:41:07 EDT
And why don't you use this code pattern (java.lang.Double.isNaN(double))?

public class IdenticalExpressionWarningOnDoubles {
        public static void main(String[] args) {
                double var = Double.NaN;
                if(Double.isNaN(var)) {
                        System.out.println("NaN");
                }
        }
}
Comment 2 Stefan Kolarov CLA 2009-06-30 16:28:36 EDT
I do not want to refactor the legacy code which does contains such expressions. At least not until Eclipse provides quick fix for this :) Yes, I can use SuppressWarning too, but it looks cumbersome, and is sort of refactoring too.
Comment 3 Srikanth Sankaran CLA 2009-07-16 02:10:39 EDT
(In reply to comment #2)
> I do not want to refactor the legacy code which does contains such expressions.
> At least not until Eclipse provides quick fix for this :) Yes, I can use
> SuppressWarning too, but it looks cumbersome, and is sort of refactoring too.

It sounds reasonable to unconditionally tolerate these cases as a
legitimate idiom for testing for NaNs.

Olivier, what do you think ? Or do you see enough justification for
introducing an option that the user can set/unset ? 

Comment 4 Srikanth Sankaran CLA 2009-07-16 02:13:00 EDT
Created attachment 141735 [details]
Patch possibility
Comment 5 Dani Megert CLA 2009-07-16 02:40:01 EDT
re comment 3: I agree. We shouldn't issue invalid warnings and adding an option to do some seems wrong.
Comment 6 Olivier Thomann CLA 2009-07-16 09:09:17 EDT
I don't think we need to add a new option for this.
Comparison between two identical float or double expression cannot be reported as a warning because of the common pattern to check NaN.
Srikanth, please go forward and release the patch.
No need to backport to 3.5.1 as this is not a severe problem.
Comment 7 Srikanth Sankaran CLA 2009-07-16 23:12:16 EDT
Fix and tests released in HEAD for 3.6M1
Comment 8 Frederic Fusier CLA 2009-08-04 08:41:03 EDT
Verified for 3.6M1 using build 20090802-2000.
Comment 9 Srikanth Sankaran CLA 2010-02-11 00:25:16 EST
*** Bug 301405 has been marked as a duplicate of this bug. ***
Comment 10 Srikanth Sankaran CLA 2010-02-25 00:25:52 EST
*** Bug 251227 has been marked as a duplicate of this bug. ***
Comment 11 Srikanth Sankaran CLA 2010-04-14 07:47:42 EDT
*** Bug 309087 has been marked as a duplicate of this bug. ***