C# not flagging FirstOrDefault null reference in sonarcloud

There is a similar thread on this but Im on SonarCloud.

Basically, the code is something like rtds.Where(r => …)?.FirstOrDefault().ReadingDate , after FirstOrDefault there is no null check and it is not flagging out as bug in S2259 rule.

Hey there.

Can you please provide a complete, well-formatted code sample?

@Colin

Position position = new Position();
if (rtds != null && rtds.Any())
                {
                    foreach (var rtd in rtds.GroupBy(x => x.CreatedSource))
                    {
                        LatestRtdsDTO latestRtds = new LatestRtdsDTO();
                        latestRtds.Rtds = rtd.Select(x => new Rtds
                        {
                            Order = x.Type,
                            Value = x.Data
                        }).ToList();
                        position.LatestRtds.Add(latestRtds);
                    }
                    position.LastScannedRtdAt = rtds.Where(r => r.CreatedSource != DeviceTypeEnum.Manual_Inspection.ToString()).OrderBy(r => decimal.Parse(r.Data))?.FirstOrDefault().ReadingDate;

                }

public class PositionTaskDetails
    {
        public List<LatestRtdsDTO> LatestRtds { get; set; }
        public DateTime? LastScannedRtdAt { get; set; }
}

Hey @xxeasterxx ,

Could you simplify the example a bit and make it compile?
I tried something like this locally, and it seems to work:

namespace Reproducers
{
    public class Repro_FirstOrDefault
    {
        public class Thing
        {
            public DateTime field;
        }

        public void DoWork(List<Thing> thingies)
        {
            _ = thingies.Where(x => true).OrderBy(x => x)?.FirstOrDefault(x => true).field; // Noncompliant
        }
    }
}

Thanks!

Hi, why do you need a compilable code in order to resolve this issue? it is really just not flagging out in VS sonarlint and in sonarcloud.

Because this is a Community forum, and while we appreciate your feedback, if the issue is important to you we really expect you to make it easy for us. We have a backlog of issues to fix, and we can spend time fixing those issues or trying to reproduce the one you reported. :person_shrugging:

1 Like

Gregory tried to reproduce it with the information you gave in the OP but the rule was raised correctly. If it doesn’t for you, you did something different. In order to move forward, we need to figure out, what is going on.

[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Test()
        {
            var wheelPos = new WheelMeasurement();
            var position = new PositionTaskDetails();
            position.LastScannedRtdAt = wheelPos.Measurements?.FirstOrDefault(r => r.Type == "RTD").ReadingDate;
        }
    }
    public class WheelMeasurement
    {
        public ICollection<VehicleInfoMeasurement> Measurements { get; set; }
    }
    public class VehicleInfoMeasurement
    {
        public string Type { get; set; }
        public DateTime? ReadingDate { get; set; }
    }
    public class PositionTaskDetails
    {
        public DateTime? LastScannedRtdAt { get; set; }
    }

Hey again,

The rule should actually raise, and if you do the same thing on non-test code it will in fact raise.
As you can see on the specification of the rule, it is configured to raise only in production code.
Because you have a dependency on the UnitTest framework in order to use the TestClass and TestMethod attributes, the code is considered test code and the rule does not run.

The reasons for this is on a case-by-case basis. But for this rule, if you get a NRE on a Unit test, it probably means you need to either look into the test’s input or the logic and fix it. External input should be impossible in test code, so there is no real danger.

If you can make it not-raise in a non-test project, please let me know.
Preferably by making a small reproducing on sonarcloud, so that I can look into it in a real scenario.

Thanks a lot! :slight_smile:

Can I send you the full code via PM? i think the simplified code is flagging out but on my production code it is not. @gregory.paidis