Reporter | Théo Lebrun (Théo.Lebrun) |
---|---|
Created | Apr 29, 2013 1:54:23 PM |
Updated | Apr 26, 2018 6:14:17 PM |
Subsystem | Code Analysis - Linq Tools |
Assignee | Alisa Afonina (alisa.afonina) |
Priority | Normal |
State | Submitted |
Type | Bug |
Fix version | Backlog |
Affected versions | 2018.1, 7.1.3 |
Fixed In Version ReSharper | Undefined |
VsVersion | All Versions |
If you have something like that :
var xElements = new List<XElement>();
foreach (XElement xElement in xElements)
{
switch (xElement.Parent.Name.LocalName)
{
case "FOO":
Console.WriteLine("FOOBAR !");
break;
}
}
Resharper tell you to add an if for possible NRE on "xElement.Parent" :
var xElements = new List<XElement>();
foreach (XElement xElement in xElements)
{
if (xElement.Parent != null)
switch (xElement.Parent.Name.LocalName)
{
case "FOO":
Console.WriteLine("FOOBAR !");
break;
}
}
And after you can convert it to a LINQ expression :
var xElements = new List<XElement>();
foreach (XElement xElement in xElements.Where(xElement => xElement.Parent != null))
{
switch (xElement.Parent.Name.LocalName)
{
case "FOO":
Console.WriteLine("FOOBAR !");
break;
}
}
But Resharper still tell you to add an if for possible NRE on "xElement.Parent" :
var xElements = new List<XElement>();
foreach (XElement xElement in xElements)
{
switch (xElement.Parent.Name.LocalName)
{
case "FOO":
Console.WriteLine("FOOBAR !");
break;
}
}
Resharper tell you to add an if for possible NRE on "xElement.Parent" :
var xElements = new List<XElement>();
foreach (XElement xElement in xElements)
{
if (xElement.Parent != null)
switch (xElement.Parent.Name.LocalName)
{
case "FOO":
Console.WriteLine("FOOBAR !");
break;
}
}
And after you can convert it to a LINQ expression :
var xElements = new List<XElement>();
foreach (XElement xElement in xElements.Where(xElement => xElement.Parent != null))
{
switch (xElement.Parent.Name.LocalName)
{
case "FOO":
Console.WriteLine("FOOBAR !");
break;
}
}
But Resharper still tell you to add an if for possible NRE on "xElement.Parent" :