Roslyn-analyzers: FxCop rule CA1062: Validate arguments of public methods, do not recognize ValidatedNotNullAttribute in NullCheck helper classes

Created on 17 Mar 2019  路  6Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Example: Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v2.9.0-beta1.final+321600d1

Diagnostic ID

CA1062

Repro steps

  1. clone https://github.com/xmarshal/CodeAnalysisIssues
  2. build

Expected behavior

no warning or errors

Actual behavior

CA1062 In externally visible method 'string Ca1062Repro.ToString(object param)', validate parameter 'param' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. CodeAnalysis1062 Ca1062Repro.cs 15

Area-Microsoft.CodeQuality.Analyzers DataFlow Feature Request Resolution-By Design

Most helpful comment

@mavasani
I have tried adding a helper method to a NuGet package using Args.cs.pp file transformation:

using System;

namespace $RootNamespace$
{
    public static class Args
    {
        public static void NotNull([ValidatedNotNull]object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(message);
            }
        }
    }

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    public sealed class ValidatedNotNullAttribute : Attribute
    {
    }
}

This does not work either; CA1062 is still being reported. Does your explanation above cover this scenario, or should this approach work?

All 6 comments

what with this Issue, it's important for our production

Thanks, I agree this needs to be fixed. It will likely only be fixed in version 2.9.x. I am going out on vacation for few weeks starting today, I will get to it once I am back.

This might likely be fixed by https://github.com/dotnet/roslyn-analyzers/pull/2345/. I will validate once that PR is merged to use the corresponding NuGet package for https://github.com/xmarshal/CodeAnalysisIssues

Ah, I looked at your repro, and the issue seems to be that the helper method performing null checks is in a different project/compilation. Roslyn analyzers do not support interprocedural analysis across project/compilation boundary, so the invocation into a different project is not analyzed and leads to the analysis flagging this case. If I copy the code from Annotations.csproj into CodeAnalysis1062.csproj, analysis correctly detects the validation.

Unfortunately, this behavior is by design for the current analysis support in Roslyn analyzers.

Kindly file an issue on the https://github.com/dotnet/roslyn/issues if you believe cross assembly analysis support is critical for Roslyn analyzers. Closing this issue as by design.

@mavasani
I have tried adding a helper method to a NuGet package using Args.cs.pp file transformation:

using System;

namespace $RootNamespace$
{
    public static class Args
    {
        public static void NotNull([ValidatedNotNull]object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(message);
            }
        }
    }

    [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
    public sealed class ValidatedNotNullAttribute : Attribute
    {
    }
}

This does not work either; CA1062 is still being reported. Does your explanation above cover this scenario, or should this approach work?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulomorgado picture paulomorgado  路  3Comments

onyxmaster picture onyxmaster  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments