Bug 448843 - Dubious code in org.eclipse.jdt.internal.compiler.codegen.BranchLabel
Summary: Dubious code in org.eclipse.jdt.internal.compiler.codegen.BranchLabel
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.5   Edit
Hardware: All All
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2014-10-25 12:56 EDT by Tagir Valeev CLA
Modified: 2022-07-12 05:28 EDT (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 Tagir Valeev CLA 2014-10-25 12:56:38 EDT
I'm writing some experimental detectors for FindBugs and used Eclipse JDT code to test them. During my testing I've found the following code:
http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/codegen/BranchLabel.java#n159

public int forwardReferenceCount() {
  if (this.delegate != null) this.delegate.forwardReferenceCount();
  return this.forwardReferenceCount;
}
public int[] forwardReferences() {
  if (this.delegate != null) this.delegate.forwardReferences();
  return this.forwardReferences;
}

My new detector marks the if lines as redundant code. Could you please check whether this is intended? It seems that the code should be written like this:

public int forwardReferenceCount() {
  if (this.delegate != null) _return_ this.delegate.forwardReferenceCount();
  return this.forwardReferenceCount;
}
public int[] forwardReferences() {
  if (this.delegate != null) _return_ this.delegate.forwardReferences();
  return this.forwardReferences;
}

Thank you in advance.
Comment 1 Stephan Herrmann CLA 2014-10-25 13:15:41 EDT
On the one hand, yes, the code definitely looks suspicious.
Thanks for letting us know!
Unfortunately, the team member who wrote this code 8 years ago is no longer aboard, so it's difficult to speak about *intention*.
We'll check what is the desired behaviour, though.


OTOH, in order to signal it as *redundant*, you'd have to assert that all implementations (incl. all sub-classes) of forwardReferenceCound() are pure functions, i.e., don't produce side effects.
Comment 2 Tagir Valeev CLA 2014-10-25 13:44:28 EDT
Thank you for your response. Yes, sure, my detector looks for every implementation in subclasses accessible for analysis. Otherwise there would be too many false-positives. Of course it may be wrong if the method is intended to have side-effects in derived projects that were not supplied to the analysis. My detector is still in early development stage, but hopefully will become something useful.
Comment 3 Srikanth Sankaran CLA 2014-10-25 14:28:42 EDT
If that is all the dubious code your tool found in JDT/Core I would
say, your tool is dubious, there should be much more suspect code in
JDT :)

Banter aside, I agree that method looks suspicious. We will take a closer
look in the coming days.

(In reply to Tagir Valeev from comment #2)

> the analysis. My detector is still in early development stage, but hopefully
> will become something useful.

Good luck, do send us any more diagnostics you uncover. TIA.
Comment 4 Tagir Valeev CLA 2014-10-26 00:35:02 EDT
Srikanth Sankaran, well there are two more similar places found by my detector if you're interested in:
http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/RecoveredImport.java#n45

public ImportReference updatedImportReference(){
  return this.importReference;
}

public void updateParseTree(){
  updatedImportReference();
}

http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/parser/RecoveredAnnotation.java#n216

public Annotation updatedAnnotationReference() {
  return this.annotation;
}
...
public void updateParseTree() {
  updatedAnnotationReference();
}

The body of updateParseTree() methods has no effect. Again, there are no overridden side-effect updated*Reference methods, but probably it was planned to extend them later...
Comment 5 Tagir Valeev CLA 2014-10-26 23:58:50 EDT
One more detection after I've implemented some improvements regarding arrays in my detector:
http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/search/org/eclipse/jdt/core/search/SearchPattern.java#n2077
if (!method.isBinary()) {
  // prefix with a '*' as the full qualification could be bigger (because of an import)
  CharOperation.concat(IIndexConstants.ONE_STAR, returnQualification);
}

CharOperation.concat has no side effects, so it seems that result should be assigned to something. See CharOperation.java for detail:

http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/CharOperation.java#n907
Comment 6 Srikanth Sankaran CLA 2014-10-27 00:15:31 EDT
(In reply to Tagir Valeev from comment #5)
> One more detection after I've implemented some improvements regarding arrays
> in my detector:

Thanks, now I agree your detector is up to snuff and withdraw my
remarks made in jest in comment#3 :)
Comment 7 Eclipse Genie CLA 2019-09-07 14:26:43 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 8 Stephan Herrmann CLA 2019-09-07 15:23:25 EDT
These valuable hints should not go to waste. 

Looks like low-hanging fruit.

Anyone?
Comment 9 Jay Arthanareeswaran CLA 2019-09-09 02:51:36 EDT
(In reply to Stephan Herrmann from comment #8)
> These valuable hints should not go to waste. 
> 
> Looks like low-hanging fruit.
> 
> Anyone?

Looks like both the issue and solution is already provided. Only question will be about a testcase. Do we have that already too?
Comment 10 Stephan Herrmann CLA 2019-11-17 16:08:34 EST
Bulk move of unassigned bugs to 4.15
Comment 11 Stephan Herrmann CLA 2020-02-27 18:49:02 EST
Bulk move to 4.16
Comment 12 Stephan Herrmann CLA 2020-05-18 18:46:17 EDT
Bulk move of unassigned bugs to 4.17
Comment 13 Eclipse Genie CLA 2022-07-12 05:28:27 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.