Correct code is recognized as incorrect
namespace WindowsApplication4
{
public class Form1
{
private bool visible;
private bool filtered;
public void Test()
{
bool b;
visible = visible | DoUpdate(out b); // |!
filtered = filtered || b; // Resharper says this is incorrect, but this is uninitialized but this is initiliazed
}
private static bool DoUpdate(out bool b)
{
b = false;
return false;
}
}
}
Please sign in to leave a comment.
Since visible is a field, it's value will persist between calls. For
example, the following code:
Form1 form1 = new Form1();
form1.Test();
form1.Test();
The first call to Test() might cause visible to become true and then, b
will no be initialized during the second call. In you sample code,
visible can never become true because DoUpdate() always returns false
but I think resharper can be excused for this oversite. I don't expect
resharper to analyze all the class methods & properties for ones that
can possibly change visible to true. I think this is a good call by
resharper.
In article <1189247.1172157324443.JavaMail.itn@is.intellij.net>,
no_reply@jetbrains.com says...
Acctually, I take it back. There is a Resharper bug here. I missed the
fact that the operator being used is "|" rather that "||". while "||" is
a lazy "or", "|" is not and will evaluate both operands even if the
first is true. Here's a test program. In fact, if you change the | to
||, the compiler will complain that b is uninitialized.
using System;
public class Class {
public bool F1(bool x) {
bool b;
x = x | F2(out b);
// b will be true regardless of wether x is true or false
// which is proof it is always initialized but resharper
// still complains
//
Console.Out.WriteLine("b = {0}", b);
return x;
}
private static bool F2(out bool b) {
b = true;
return false;
}
public static void Main(string[] args) {
Class c = new Class();
c.F1(true);
c.F1(false);
}
}
Output:
b = True
b = True
In article <MPG.20479d295f6add23989681@news.jetbrains.com>,
notme@nowhere.com says...
Hello,
We appreciate your feedback.
The corresponding JIRA request has been created, and you are welcome to monitor
its status at http://www.jetbrains.net/jira/browse/RSRP-36202.
Best regards,
- Development Team.