New case for DisposableFieldsShouldBeDisposedRule?

15 views
Skip to first unread message

knocte

unread,
Jan 21, 2011, 6:37:21 AM1/21/11
to Gendarme
Hi Gendarmes,

I'm wondering if DisposableFieldsShouldBeDisposedRule should be
smarter and detect this case too (saying that class A should be
IDisposable):

interface ISomeInterface
{
string Name { get; }
}

class A
{
private ISomeInterface someMember;

public ISomeInterface SomeMember {
get {
if (someMember == null)
someMember = new B();
return someMember;
}
}
}

class B : ISomeInterface, IDisposable
{
private FileStream stream;

public string Name { get { return "test"; } }
public B () {
stream = new FileStream("whatever", FileMode.Append);
}

public void Dispose()
{
if (stream != null)
stream.Dispose();
}
}

Cheers,

Andres

knocte

unread,
Jan 21, 2011, 6:55:19 AM1/21/11
to Gendarme, kno...@gmail.com
Sorry, I meant TypesWithDisposableFieldsShouldBeDisposableRule.

"Andrés G. Aragoneses"

unread,
Jan 25, 2011, 6:08:28 PM1/25/11
to gend...@googlegroups.com

No feedback anyone? I'll create an enhancement request in BNC if no one
is opposed in 24hours ;)

(And will provide a patch if nobody marks it as WONTFIX after a week!)

Sebastien Pouliot

unread,
Jan 25, 2011, 8:24:03 PM1/25/11
to gend...@googlegroups.com
Hello Andrés,

On Wed, 2011-01-26 at 00:08 +0100, "Andrés G. Aragoneses" wrote:
> No feedback anyone? I'll create an enhancement request in BNC if no one
> is opposed in 24hours ;)
>
> (And will provide a patch if nobody marks it as WONTFIX after a week!)

I'll be away from tomorrow night until Feb 8th - so you're unlikely to
see a WONTFIX in that period ;-)

Seriously I had not the time to review all related rules (quite a few
deals with IDisposable) but the test case looks valid. IOW your patches
will be most welcome :-)

Thanks!
Sebastien

p.s. see comment below

>
> On 21/01/11 12:55, knocte wrote:
> > Sorry, I meant TypesWithDisposableFieldsShouldBeDisposableRule.
> >
> > On Jan 21, 11:37 am, knocte<kno...@gmail.com> wrote:
> >> Hi Gendarmes,
> >>
> >> I'm wondering if DisposableFieldsShouldBeDisposedRule should be
> >> smarter and detect this case too (saying that class A should be
> >> IDisposable):
> >>
> >> interface ISomeInterface
> >> {
> >> string Name { get; }
> >> }
> >>
> >> class A
> >> {
> >> private ISomeInterface someMember;
> >>
> >> public ISomeInterface SomeMember {
> >> get {
> >> if (someMember == null)
> >> someMember = new B();

The message provided by the defect will need to be clear since
alternatives could also provide non-IDisposable, e.g.:

switch (SomeOtherProperty) {
` case 0:
// not IDisposable
someMember = new C();
break;
default:
someMember = new B();
break;
}

Make this a large switch/case and this IDisposable-case can be hard to
find (and easy to forget ;-)

Reply all
Reply to author
Forward
0 new messages