False "This field is always assigned before being used and can be converted to a local variable" warning

I'm running into a false warning problem (first saw it in ReSharper 8 and still exists in 8.0.1 EAP).  This error did not occur in 6.6.  It is difficult to reproduce so I haven't been able to build a small test case for it, unfortunately.  Hopefully someone else can reproduce it too.  My code looks like this:

    public partial class MyForm : Form
    {
        private readonly bool _flag; // false warning is shown here

        public MyForm(bool flag)
        {
            _flag = flag;
            InitializeComponent();
        }

        private void MyForm_Load(object sender, EventArgs e)
        {
            // Lots of various form and control initialization occurs here.  Nothing fancy but there needs to be at least 40+ lines of code here for the issue to occur.
            someControl.Enabled = _flag;
        }
    }

There are a few strange things here.  The problem only occurs when there is a good amount of code ahead of the first (and only) usage of the '_flag' field.  If I move it up closer to the beginning of the MyForm_Load method, then the warning goes away.  Also, changing some of the code that appears before the _flag usage also seems to affect this bug.  That is, if I had something like 'field.Enabled = otherField.Enabled' and then changed it to something like 'field.Enabled = false' then the issue may disappear.  This is quite odd since the '_flag' field isn't involved with this other code at all.  It seems there is some condition(s) that is causing the ReSharper analyzer to get confused.  Lastly, if I took ReSharper's suggested fix of 'Convert to local variable' that produces uncompilable code as the '_flag' field is no longer available in the MyForm_Load method.  It is as if ReSharper can no longer see the usage of '_flag' in MyForm_Load when there is a lot of code ahead of it or something.  Very weird, hopefully this can be fixed for 8.0.1.

2 comments
Comment actions Permalink

Hello Jason,

I logged a new ticket http://youtrack.jetbrains.com/issue/RSRP-383472 about the issue. Hope we will be able to fix it in ReSharper 8.0.1.

You may track ticket's status and comment it.

Thanks!

0
Comment actions Permalink

Hi Jason,
Could you show us source code of the method "MyForm_Load" ? It can be non-compilable, we need only to see at it from control flow analysis perspective. You can post it either here or in the youtrack ticket with "jetbrains-team" visibility.
Thank you.

0

Please sign in to leave a comment.