Wrong Expression Is Always True warning

Hi, I noticed this topic come up several times but I would like to know if there is a way to handle it correctly.

Suppose you have this code:

if (m_socket != null) {
     lock (m_socketLockObject)      {           if (m_socket != null)        {



This is the typical double checked locking.

On the second m_socket != null expression R# issues a "Expression is always true" warning, which is obviously wrong. This is incredibily misleading as my thought was that R# should always issue errors/warnings/hints and the like only when there was absolute certainty. In this specific case removing the second null check can cause wrong results in the program flow.
6 comments

Thank you for the feedback.  This is one of the cases when our heuristic algorithm makes false positive. I'll think how we can resolve this.

0

Evgeny, this false positive in quite dangerous. I think R# should stay on the safe side instead, giving suggestions only when there is total certainty that the suggested change doesn't introduce bugs, at the cost of giving fewer suggestions.

0

I agree that this false-positive is quite dangerous, though it is quite rare. I'll think how I can modify the analysis.

0

What is interesting, in multi-threaded scenario every state is non-deterministic, besides the current scope variables and readonly fields.

And if you are making an assignment to local variable in a delegate, then pass this delegate to another thread, this makes a local variable non-deterministic also.

0

gpolevoy wrote:

And if you are making an assignment to local variable in a delegate, then pass this delegate to another thread, this makes a local variable non-deterministic also.


As it's not longer a local variable.

0

Yes, you're right. ReSharper analysis knows about possible non-determinism of locals captured into closure. But, as any heuristic analysis, it has false positives, and we try to minimize it by adding more heuristic rules

0

Please sign in to leave a comment.