Roslyn-analyzers: CA1062: False positive when a null check and assignment guard is used

Created on 3 Apr 2019  路  3Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Microsoft.CodeQuality.Analyzers

Package Version

2.9.1

Diagnostic ID

CA1062

Repro steps

It incorrectly flags tableNames as needing a parameter check for null. In this example, null is checked for and tableNames is given a non-null value in that case.

    public TableSet(DbDataReader reader, params string[] tableNames)
    {
        if (reader == null)
            throw new ArgumentNullException("reader", "reader is null.");
        if (tableNames == null)
            tableNames = Array.Empty<string>();

        var index = 0;
        do
        {
            var tableName = (index < tableNames.Length) ? tableNames[index] : ("Table " + index);
            m_Internal.Add(new Table(tableName, reader));
            index += 1;
        }
        while (reader.NextResult());
    }
4 - In Review Area-Microsoft.CodeQuality.Analyzers Bug

Most helpful comment

Needs special handling in points to analysis to understand that Array.Empty returns a non-null value, not the default maybe-null value.

This wasn't actually required (PR: https://github.com/dotnet/roslyn-analyzers/pull/2345), I made a more conservative implementation in analysis where assigning to a parameter on a path causes us to stop tracking the parameter as we cannot null ref due to incoming argument value for the parameter

All 3 comments

Needs special handling in points to analysis to understand that Array.Empty returns a non-null value, not the default maybe-null value.

Needs special handling in points to analysis to understand that Array.Empty returns a non-null value, not the default maybe-null value.

This wasn't actually required (PR: https://github.com/dotnet/roslyn-analyzers/pull/2345), I made a more conservative implementation in analysis where assigning to a parameter on a path causes us to stop tracking the parameter as we cannot null ref due to incoming argument value for the parameter

Was this page helpful?
0 / 5 - 0 ratings