Quantcast
Channel: YouTrackReSharper (RSRP) - Bug and Issue Tracker
Viewing all articles
Browse latest Browse all 106942

RSRP-359798: Infinite check on NullReferenceException for a LINQ expression

$
0
0
Reporter Théo Lebrun (Théo.Lebrun) 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" :

Viewing all articles
Browse latest Browse all 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>