Example: Microsoft.CodeAnalysis.FxCopAnalyzers
v2.9.0-beta1.final+321600d1
no warning or errors
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
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?
Most helpful comment
@mavasani
I have tried adding a helper method to a NuGet package using Args.cs.pp file transformation:
This does not work either; CA1062 is still being reported. Does your explanation above cover this scenario, or should this approach work?