Bug 115814

Summary: [compiler] warning on comparing same to same
Product: [Eclipse Project] JDT Reporter: Adam Kiezun <akiezun>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P3    
Version: 3.1   
Target Milestone: 3.5 M1   
Hardware: PC   
OS: All   
Whiteboard:
Bug Depends on:    
Bug Blocks: 243414    
Attachments:
Description Flags
Proposed patch none

Description Adam Kiezun CLA 2005-11-10 09:53:40 EST
3.2M3

There's a warning for assignments from same to same (like x = x)
But there's none for comparisons of that kind. Those are just as likely to be bogus.

Example

class Foo{
	final int x;
	Foo(int x){ this.x= x;}
	
	@Override
 	public int hashCode() {
	 return x * 997;
 	}
	
	@Override
	public boolean equals(Object obj) {
	  if (obj instanceof Foo){
	    Foo foo = (Foo) obj;
	    return x == x; //<<< BUG that could be flagged early
	  } else
	    return false;
	}
}
Comment 1 Olivier Thomann CLA 2006-10-10 13:54:53 EDT
Philippe,

Should we investigate such a warning?
Comment 2 Philipe Mulet CLA 2008-06-04 13:23:18 EDT
Created attachment 103604 [details]
Proposed patch

combined with patch for bug 235004
Comment 3 Philipe Mulet CLA 2008-06-23 11:51:19 EDT
Fixed, released for 3.5M1
Comment 4 Philipe Mulet CLA 2008-06-23 11:54:34 EDT
Added ProgrammingProblemsTest#test037
Comment 5 Philipe Mulet CLA 2008-06-24 07:23:01 EDT
Added a new compiler warning to detect comparisons between identical expressions. This diagnosis is controlled by option: JavaCore.COMPILER_PB_COMPARING_IDENTICAL and produces a problem marker which ID is IProblem.ComparingIdentical problem ID.

Compiler option ID: Reporting Comparison of Identical Expressions.
When enabled, the compiler will issue an error or a warning if a comparison
is involving identical operands (e.g 'x == x').
  - Option id:"org.eclipse.jdt.core.compiler.problem.comparingIdentical"
  - Possible values: { "error", "warning", "ignore" }
  - Default: "warning"
Comment 6 Philipe Mulet CLA 2008-06-24 07:24:13 EDT
entered bug 238226 for ui counterpart
Comment 7 Kent Johnson CLA 2008-08-06 12:58:53 EDT
Verified for 3.5M1 using I20080805-1307