Resharper gives a possible null reference warning for the Element method call just after it was cheked for null (TransformElementBad method), but everything is ok if I keep element value in some variable. Is ReSharper right and I should use its suggestion or it is a bug?
// JetBrains ReSharper 8.2.1
// Build 8.2.1000.4556 on 2014-05-19T09:12:38
public class Transformer
{
private void TransformElementBad(XElement field)
{
var format = string.Empty;
if (field.Element("ViewFormatInfo") != null)
format = field.Element("ViewFormatInfo").Value.Trim(); // ![got][1] warning here
Console.WriteLine(format);
}
private void TransformElementGood(XElement field)
{
var format = string.Empty;
var element = field.Element("ViewFormatInfo");
if (element != null)
format = element.Value.Trim();
Console.WriteLine(format);
}
}
Answer from vendor: Element method is not pure and could not return the same result given the same arguments. So you have to copy its result into local variable if you want to avoid this warning https://youtrack.jetbrains.com/issue/RSRP-424149