Bug 265142 - Compiler fails to warn on unused constructors of private classes.
Summary: Compiler fails to warn on unused constructors of private classes.
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.5 M6   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-17 07:53 EST by Srikanth Sankaran CLA
Modified: 2009-03-10 10:05 EDT (History)
2 users (show)

See Also:


Attachments
Proposed patch + tests (16.78 KB, patch)
2009-02-18 07:34 EST, Srikanth Sankaran CLA
no flags Details | Diff
Patch with review comments incorporated (16.89 KB, patch)
2009-02-19 06:09 EST, Srikanth Sankaran CLA
kent_johnson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Srikanth Sankaran CLA 2009-02-17 07:53:10 EST
Build ID: I20090217-0800

Steps To Reproduce:
Basically the fix for bug#264991 suppressed the emission
of unused constructor message for unused constructors of
private classes. These diagnostics were introduced by the
fix for bug#201912. These need to be reintroduced in the
appropriate manner.

Test case: Look at org.eclipse.jdt.core.tests.compiler.regression.ProblemConstructorTest.test004()

We should warn about the constructor of X.M#M(int) being unused. We don't.

More information:
Comment 1 Srikanth Sankaran CLA 2009-02-18 07:34:23 EST
Created attachment 126014 [details]
Proposed patch + tests

Kent, can you please review this patch and help take it to commit. Thanks.
Comment 2 Kent Johnson CLA 2009-02-18 11:09:58 EST
In ConstructorDeclaration.analyseCode(), you're finding the super constructor by asking the scope.

MethodBinding superCtor = this.scope.getConstructor(superClass,
  Binding.NO_PARAMETERS, this.constructorCall);
if (superCtor == null || !superCtor.isValidBinding())
	break checkUnused;

Why not just ask the superclass directly?

if (superClass.getExactConstructor(Binding.NO_PARAMETERS) == null)
	break checkUnused;
Comment 3 Kent Johnson CLA 2009-02-18 11:19:17 EST
Ok - so I now see why - we need to check visibility. That can still be done explicitly vs. the scope call which has side affects.


I have another question, if a private type has only 1 constructor - why would we ever complain that it is unused ? Does it make sense that the user remove the type's only constructor ?

Comment 4 Srikanth Sankaran CLA 2009-02-19 06:09:13 EST
Created attachment 126152 [details]
Patch with review comments incorporated
Comment 5 Srikanth Sankaran CLA 2009-02-19 06:22:20 EST
(In reply to comment #3)
> Ok - so I now see why - we need to check visibility. That can still be done
> explicitly vs. the scope call which has side affects.

    I don't think this call has any adverse side effects (I picked up that
usage from ExplicitConstructorCall.resolve), but I agree it is needlessly heavyweight for the current needs. The patch posted now addresses this
concern.

> I have another question, if a private type has only 1 constructor - why would
> we ever complain that it is unused ? Does it make sense that the user remove
> the type's only constructor ?

    As long as an element is not explicitly used and (cannot be used in future
in unknown ways (by virtue of being private)) it should be ok to warn I think.

    The litmus test should really be whether each warning is "actionable on
an independant basis" and whether the action of deleting the seemingly unused
item causes any ill effects (such as program build failure or run time
behavior difference)

    Am I missing something ??

Comment 6 Kent Johnson CLA 2009-02-19 11:39:00 EST
We call scope methods to resolve explicit references in the AST. When you're checking to see if an inherited method exists, we usually just go to the bindings themselves since we do care to report problems, record references, etc.

"Am I missing something ??"

No - I was just wondering since some users define a 'default' constructor for no reason, so warning that it is unused seems unneccessary.



BTW - The comments are backwards for ProblemConstructorTest test008 & test009
Comment 7 Kent Johnson CLA 2009-02-19 13:54:39 EST
Fix and test released for 3.5M6
Comment 8 Srikanth Sankaran CLA 2009-02-19 23:08:40 EST
(In reply to comment #6)

> BTW - The comments are backwards for ProblemConstructorTest test008 & test009

Thanks Kent!
Comment 9 Frederic Fusier CLA 2009-03-10 10:05:09 EDT
Verified for 3.5M6 using I20090310-0100.