Dereferencing shows warning "value assigned is not used in any execution path"

When you want to null dereference a variable and to enforce it goes out of
scope ReSharper shows the warning "value assigned is not used in any
execution path". Is it possible to disable this warning just for null
dereferencing?

Gabriel Lozano-Morán

8 comments
Comment actions Permalink

What the use of manual nulling of a local variable before leaving it's
declaration scope?
AFAIK this is useless

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
news:ejph41$msr$1@is.intellij.net...

When you want to null dereference a variable and to enforce it goes out of
scope ReSharper shows the warning "value assigned is not used in any
execution path". Is it possible to disable this warning just for null
dereferencing?

>

Gabriel Lozano-Morán



0
Comment actions Permalink

It depends, if you start a longer running action than normal and in the
beginning of the action you need a large object that you want to be able to
be destroyed by the Garbage Collection before the action ends.

AFAIK this is useful, anyway the warning "value assigned is not used in any
execution path" is not correct in this scenario whether or not you think
this is useless or not.

Gabriel

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:ejphrn$62n$1@is.intellij.net...

What the use of manual nulling of a local variable before leaving it's
declaration scope?
AFAIK this is useless

>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
news:ejph41$msr$1@is.intellij.net...

>> When you want to null dereference a variable and to enforce it goes out
>> of scope ReSharper shows the warning "value assigned is not used in any
>> execution path". Is it possible to disable this warning just for null
>> dereferencing?
>>
>> Gabriel Lozano-Morán
>


0
Comment actions Permalink

Depending on the complexity of the method, I sometimes null expensive
objects after use to indicate I'm finished using them making the code more
readable. It's also an old habit I have from pre-garbage collection days to
clean up memory after use, making it "look good" (but useless I know) to see
paired creation / nulling of objects.

I consider this to belong to the same readability category as redundant
"this" reference warnings, the latter which can be disabled.

It doesn't occur often so it's not a big deal for me, but +1 for the
suggestion.

Dag


"Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
news:ejqe74$nt5$1@is.intellij.net...

It depends, if you start a longer running action than normal and in the
beginning of the action you need a large object that you want to be able
to be destroyed by the Garbage Collection before the action ends.

>

AFAIK this is useful, anyway the warning "value assigned is not used in
any execution path" is not correct in this scenario whether or not you
think this is useless or not.

>

Gabriel

>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:ejphrn$62n$1@is.intellij.net...

>> What the use of manual nulling of a local variable before leaving it's
>> declaration scope?
>> AFAIK this is useless
>>
>> --
>> Eugene Pasynkov
>> Developer
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> "Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
>> news:ejph41$msr$1@is.intellij.net...
>>> When you want to null dereference a variable and to enforce it goes out
>>> of scope ReSharper shows the warning "value assigned is not used in any
>>> execution path". Is it possible to disable this warning just for null
>>> dereferencing?
>>>
>>> Gabriel Lozano-Morán
>>
>>



0
Comment actions Permalink

Hi Gabriel,

I'm not sure if it's the case with C# as well, but the Delphi optimizer
already frees locally scoped variables directly after it's last use, not
after the method ends, if I'd have to guess IL would do this as well (at
least in Release mode, maybe not in Debug mode)

Isn't it a best practice to implement IDisposable for memory intensive
objects to free it's (unmanaged) resources, and let the GC do it's work
whenever he likes?

just my 2cts, please correct me if I'm wrong :)...

Wiebe Tijsma

Gabriel Lozano-Morán wrote:

It depends, if you start a longer running action than normal and in the
beginning of the action you need a large object that you want to be able
to be destroyed by the Garbage Collection before the action ends.

AFAIK this is useful, anyway the warning "value assigned is not used in
any execution path" is not correct in this scenario whether or not you
think this is useless or not.

Gabriel

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:ejphrn$62n$1@is.intellij.net...

>> What the use of manual nulling of a local variable before leaving it's
>> declaration scope?
>> AFAIK this is useless
>>
>> --
>> Eugene Pasynkov
>> Developer
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> "Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
>> news:ejph41$msr$1@is.intellij.net...
>>
>>> When you want to null dereference a variable and to enforce it goes
>>> out of scope ReSharper shows the warning "value assigned is not used
>>> in any execution path". Is it possible to disable this warning just
>>> for null dereferencing?
>>>
>>> Gabriel Lozano-Morán
>>
>>
>>

0
Comment actions Permalink

See:

http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx

The only case when it's useful to set a variable to null, is when you
reuse the variable later.

I think reuse of local variables is evil anyway.

Just declare 2 vars, and there's no use to set any of the variables to
null (this is already done by the JIT)

Wiebe Tijsma

Wiebe Tijsma wrote:

Hi Gabriel,

I'm not sure if it's the case with C# as well, but the Delphi optimizer
already frees locally scoped variables directly after it's last use, not
after the method ends, if I'd have to guess IL would do this as well (at
least in Release mode, maybe not in Debug mode)

Isn't it a best practice to implement IDisposable for memory intensive
objects to free it's (unmanaged) resources, and let the GC do it's work
whenever he likes?

just my 2cts, please correct me if I'm wrong :)...

Wiebe Tijsma

Gabriel Lozano-Morán wrote:

>> It depends, if you start a longer running action than normal and in
>> the beginning of the action you need a large object that you want to
>> be able to be destroyed by the Garbage Collection before the action ends.
>>
>> AFAIK this is useful, anyway the warning "value assigned is not used
>> in any execution path" is not correct in this scenario whether or not
>> you think this is useless or not.
>>
>> Gabriel
>>
>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
>> message news:ejphrn$62n$1@is.intellij.net...
>>
>>> What the use of manual nulling of a local variable before leaving
>>> it's declaration scope?
>>> AFAIK this is useless
>>>
>>> --
>>> Eugene Pasynkov
>>> Developer
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>> "Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
>>> news:ejph41$msr$1@is.intellij.net...
>>>
>>>> When you want to null dereference a variable and to enforce it goes
>>>> out of scope ReSharper shows the warning "value assigned is not used
>>>> in any execution path". Is it possible to disable this warning just
>>>> for null dereferencing?
>>>>
>>>> Gabriel Lozano-Morán
>>>
>>>
>>>
>>>
>>

0
Comment actions Permalink

Like Jon Skeet mentions in his comment this could be checked by the CLR but
I doubt that it is easy since the GC can't be 100% sure that the memory can
actually be reclaimed. Nevertheless in the current implementations of
Microsoft's CLR variables stay in-scope until the method ends. I agree that
if the design is right you wouldn't need to do these kind of tricks but
unfortunately not all systems are well-designed and not all projecst have
the budget for a redesign/refactoring.

Cheers

Gabriel Lozano-Morán

"Wiebe Tijsma" <wiebeREMOVE@CAPITALStijsma.com> wrote in message
news:ejsiut$9ke$1@is.intellij.net...

See:

>

http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx

>

The only case when it's useful to set a variable to null, is when you
reuse the variable later.

>

I think reuse of local variables is evil anyway.

>

Just declare 2 vars, and there's no use to set any of the variables to
null (this is already done by the JIT)

>

Wiebe Tijsma

>

Wiebe Tijsma wrote:

>> Hi Gabriel,
>>
>> I'm not sure if it's the case with C# as well, but the Delphi optimizer
>> already frees locally scoped variables directly after it's last use, not
>> after the method ends, if I'd have to guess IL would do this as well (at
>> least in Release mode, maybe not in Debug mode)
>>
>> Isn't it a best practice to implement IDisposable for memory intensive
>> objects to free it's (unmanaged) resources, and let the GC do it's work
>> whenever he likes?
>>
>> just my 2cts, please correct me if I'm wrong :)...
>>
>> Wiebe Tijsma
>>
>> Gabriel Lozano-Morán wrote:
>>
>>> It depends, if you start a longer running action than normal and in the
>>> beginning of the action you need a large object that you want to be able
>>> to be destroyed by the Garbage Collection before the action ends.
>>>
>>> AFAIK this is useful, anyway the warning "value assigned is not used in
>>> any execution path" is not correct in this scenario whether or not you
>>> think this is useless or not.
>>>
>>> Gabriel
>>>
>>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
>>> message news:ejphrn$62n$1@is.intellij.net...
>>>
>>>> What the use of manual nulling of a local variable before leaving it's
>>>> declaration scope?
>>>> AFAIK this is useless
>>>>
>>>> --
>>>> Eugene Pasynkov
>>>> Developer
>>>> JetBrains, Inc
>>>> http://www.jetbrains.com
>>>> "Develop with pleasure!"
>>>> "Gabriel Lozano-Morán" <gabriel@pointerx.net> wrote in message
>>>> news:ejph41$msr$1@is.intellij.net...
>>>>
>>>>> When you want to null dereference a variable and to enforce it goes
>>>>> out of scope ReSharper shows the warning "value assigned is not used
>>>>> in any execution path". Is it possible to disable this warning just
>>>>> for null dereferencing?
>>>>>
>>>>> Gabriel Lozano-Morán
>>>>
>>>>
>>>>
>>>>
>>>

0
Comment actions Permalink

Hello Gabriel_Lozano-Moran,

G> Nevertheless in the current
G> implementations of Microsoft's CLR variables stay in-scope until the
G> method ends.

As far as I know, this is not true in RELEASE build, but it is true in DEBUG
build, so you can inspect variable values while debugging. In release mode,
JIT builds special table for each method, indicating last used point of a
value. That's the reason for GC.KeepAlive to even exist. Consider

object o = new object(); // point 1
... do something not using 'o'
o = new object(); // point 2

In this example, object allocated at point 1 is eligible to garbage collection
immediately, not when o is overwritten at point 2.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


G> Like Jon Skeet mentions in his comment this could be checked by the
G> CLR but I doubt that it is easy since the GC can't be 100% sure that
G> the memory can actually be reclaimed. Nevertheless in the current
G> implementations of Microsoft's CLR variables stay in-scope until the
G> method ends. I agree that if the design is right you wouldn't need to
G> do these kind of tricks but unfortunately not all systems are
G> well-designed and not all projecst have the budget for a
G> redesign/refactoring.
G>
G> Cheers
G>
G> Gabriel Lozano-Moran
G>
G> "Wiebe Tijsma" <wiebeREMOVE@CAPITALStijsma.com> wrote in message
G> news:ejsiut$9ke$1@is.intellij.net...
G>
>> See:
>>
>> http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx
>>
>> The only case when it's useful to set a variable to null, is when you
>> reuse the variable later.
>>
>> I think reuse of local variables is evil anyway.
>>
>> Just declare 2 vars, and there's no use to set any of the variables
>> to null (this is already done by the JIT)
>>
>> Wiebe Tijsma
>>
>> Wiebe Tijsma wrote:
>>
>>> Hi Gabriel,
>>>
>>> I'm not sure if it's the case with C# as well, but the Delphi
>>> optimizer already frees locally scoped variables directly after it's
>>> last use, not after the method ends, if I'd have to guess IL would
>>> do this as well (at least in Release mode, maybe not in Debug mode)
>>>
>>> Isn't it a best practice to implement IDisposable for memory
>>> intensive objects to free it's (unmanaged) resources, and let the GC
>>> do it's work whenever he likes?
>>>
>>> just my 2cts, please correct me if I'm wrong :)...
>>>
>>> Wiebe Tijsma
>>>
>>> Gabriel Lozano-Moran wrote:
>>>
>>>> It depends, if you start a longer running action than normal and in
>>>> the beginning of the action you need a large object that you want
>>>> to be able to be destroyed by the Garbage Collection before the
>>>> action ends.
>>>>
>>>> AFAIK this is useful, anyway the warning "value assigned is not
>>>> used in any execution path" is not correct in this scenario whether
>>>> or not you think this is useless or not.
>>>>
>>>> Gabriel
>>>>
>>>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote
>>>> in message news:ejphrn$62n$1@is.intellij.net...
>>>>
>>>>> What the use of manual nulling of a local variable before leaving
>>>>> it's
>>>>> declaration scope?
>>>>> AFAIK this is useless
>>>>> --
>>>>> Eugene Pasynkov
>>>>> Developer
>>>>> JetBrains, Inc
>>>>> http://www.jetbrains.com
>>>>> "Develop with pleasure!"
>>>>> "Gabriel Lozano-Moran" <gabriel@pointerx.net> wrote in message
>>>>> news:ejph41$msr$1@is.intellij.net...
>>>>>> When you want to null dereference a variable and to enforce it
>>>>>> goes out of scope ReSharper shows the warning "value assigned is
>>>>>> not used in any execution path". Is it possible to disable this
>>>>>> warning just for null dereferencing?
>>>>>>
>>>>>> Gabriel Lozano-Moran
>>>>>>




0
Comment actions Permalink

I stand corrected. It seems that the JIT compiler indeed will make sure that
in debug builds the variables do not go out of scope until the method ends
which is logical since you don't want the debugging process to become
problematic. I have to admit that I didn't know about this difference in
behaviour between debug and release builds. It is interesting though to know
that in release builds some unexpected bugs can occur.

Thanks for the info :)

Gabriel Lozano-Moran

"Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
news:9f6b3e2912ec678c8daf80e936f07@news.intellij.net...

Hello Gabriel_Lozano-Moran,

>

G> Nevertheless in the current
G> implementations of Microsoft's CLR variables stay in-scope until the
G> method ends.

>

As far as I know, this is not true in RELEASE build, but it is true in
DEBUG build, so you can inspect variable values while debugging. In
release mode, JIT builds special table for each method, indicating last
used point of a value. That's the reason for GC.KeepAlive to even exist.
Consider

>

object o = new object(); // point 1
... do something not using 'o'
o = new object(); // point 2

>

In this example, object allocated at point 1 is eligible to garbage
collection immediately, not when o is overwritten at point 2.

>

Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>

G> Like Jon Skeet mentions in his comment this could be checked by the
G> CLR but I doubt that it is easy since the GC can't be 100% sure that
G> the memory can actually be reclaimed. Nevertheless in the current
G> implementations of Microsoft's CLR variables stay in-scope until the
G> method ends. I agree that if the design is right you wouldn't need to
G> do these kind of tricks but unfortunately not all systems are
G> well-designed and not all projecst have the budget for a
G> redesign/refactoring.
G> G> Cheers
G> G> Gabriel Lozano-Moran
G> G> "Wiebe Tijsma" <wiebeREMOVE@CAPITALStijsma.com> wrote in message
G> news:ejsiut$9ke$1@is.intellij.net...
G>

>>> See:
>>>
>>> http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx
>>>
>>> The only case when it's useful to set a variable to null, is when you
>>> reuse the variable later.
>>>
>>> I think reuse of local variables is evil anyway.
>>>
>>> Just declare 2 vars, and there's no use to set any of the variables
>>> to null (this is already done by the JIT)
>>>
>>> Wiebe Tijsma
>>>
>>> Wiebe Tijsma wrote:
>>>
>>>> Hi Gabriel,
>>>>
>>>> I'm not sure if it's the case with C# as well, but the Delphi
>>>> optimizer already frees locally scoped variables directly after it's
>>>> last use, not after the method ends, if I'd have to guess IL would
>>>> do this as well (at least in Release mode, maybe not in Debug mode)
>>>>
>>>> Isn't it a best practice to implement IDisposable for memory
>>>> intensive objects to free it's (unmanaged) resources, and let the GC
>>>> do it's work whenever he likes?
>>>>
>>>> just my 2cts, please correct me if I'm wrong :)...
>>>>
>>>> Wiebe Tijsma
>>>>
>>>> Gabriel Lozano-Moran wrote:
>>>>
>>>>> It depends, if you start a longer running action than normal and in
>>>>> the beginning of the action you need a large object that you want
>>>>> to be able to be destroyed by the Garbage Collection before the
>>>>> action ends.
>>>>>
>>>>> AFAIK this is useful, anyway the warning "value assigned is not
>>>>> used in any execution path" is not correct in this scenario whether
>>>>> or not you think this is useless or not.
>>>>>
>>>>> Gabriel
>>>>>
>>>>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote
>>>>> in message news:ejphrn$62n$1@is.intellij.net...
>>>>>
>>>>>> What the use of manual nulling of a local variable before leaving
>>>>>> it's
>>>>>> declaration scope?
>>>>>> AFAIK this is useless
>>>>>> --
>>>>>> Eugene Pasynkov
>>>>>> Developer
>>>>>> JetBrains, Inc
>>>>>> http://www.jetbrains.com
>>>>>> "Develop with pleasure!"
>>>>>> "Gabriel Lozano-Moran" <gabriel@pointerx.net> wrote in message
>>>>>> news:ejph41$msr$1@is.intellij.net...
>>>>>>> When you want to null dereference a variable and to enforce it
>>>>>>> goes out of scope ReSharper shows the warning "value assigned is
>>>>>>> not used in any execution path". Is it possible to disable this
>>>>>>> warning just for null dereferencing?
>>>>>>>
>>>>>>> Gabriel Lozano-Moran
>>>>>>>
>
>


0

Please sign in to leave a comment.