I’ve just corrected the following piece of source code, written by a former colleague:
Class1 obj1 = database.getData<Class1>().Where(o => o.Id == <some_Value>).FirstOrDefault();
if (obj1 != null)
{
obj1.property1.reserved = false;
obj1.property2.reserved = false;
}
I’ve corrected it into:
Class1 obj1 = database.getData<Class1>().Where(o => o.Id == <some_Value>).FirstOrDefault();
if (obj1 != null)
{
if (obj1.property1 != null)
obj1.property1.reserved = false;
if (obj1.property2 != null)
obj1.property2.reserved = false;
}
When compiling the code, I see compiler warnings like:
- Warning CS0162 : Unreachable code detected
- Warning CS0169 : The field ‘…’ is never used.
So I thought “Wouldn’t it be neat, having a compiler warning, saying something like:
The property ‘property1’ is used but never checked for nullity.
(Something like that.)
The situation is: I have corrected the mentioned error after having received a call from the customer, complaining that the code was not working somewhere, and I would like to avoid the customer needing to call me: I’d like to act in a more proactive way.
Does anybody know if such a compiler warning exists and in case yes, how I can configure my compiler in order to use it?
My project is a C# project, written in Visual Studio Enterprise 2022, version 17.9.6.