Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Error AD0001 Analyzer 'CodeCracker.CSharp.Usage.DisposableVariableNotDisposedAnalyzer' threw an exception of type 'System.ArgumentNullException' with message 'Value cannot be null. Parameter name: syntax'. #1039

Open
sviatoslav-vilkovych opened this issue Oct 7, 2019 · 0 comments

Comments

@sviatoslav-vilkovych
Copy link

#Bug
CodeCracker throws an exception when building code below.

// The code that reproduces the bug
    public class A : IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
    public class B
    {
        public static B CreateB(A obj)
        {
            return new B(obj);
        }
        public B(A obj)
        {
            Obj = obj;
        }
        private readonly A Obj;
    }
    public class Test
    {
        public async static Task ConfigureSmthAsync()
        {
            if (!(new C()).IsSmth())
                return;
            var a = new A();

            var b = await Task.Run(() => B.CreateB(a));
        }
    }
    public class C
    {
        public C() { Variable = true; }
        public bool IsSmth() => Variable;

        private readonly bool Variable;
    }

    class Program
    {
        static void Main()
        {
            var test = new Test();
            Test.ConfigureSmthAsync().Wait();
        }

    }

Interesting thing that simple change of if() statement removes error.

public class Test
    {
        public async static Task ConfigureSmthAsync()
        {
            if ((new C()).IsSmth())
            {
            var a = new A();

            var b = await Task.Run(() => B.CreateB(a));
            }
        }
    }

Don't know if it is important, but error is not appearing when

  1. A is not inherit from IDisposable.
  2. ConfigureSmthAsync is not async.

I managed to get call stack of the error.

System.ArgumentNullException: Value cannot be null.
Parameter name: syntax
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.CheckSyntaxNode(CSharpSyntaxNode syntax)
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfo(ExpressionSyntax expression, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetSymbolInfo(SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken)
   at CodeCracker.CSharp.Usage.DisposableVariableNotDisposedAnalyzer.<>c__DisplayClass12_0.<IsReturned>b__2(ExpressionSyntax returnExpression)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at CodeCracker.CSharp.Usage.DisposableVariableNotDisposedAnalyzer.IsReturned(MethodDeclarationSyntax method, StatementSyntax statement, SemanticModel semanticModel, ILocalSymbol identitySymbol)
   at CodeCracker.CSharp.Usage.DisposableVariableNotDisposedAnalyzer.IsDisposedOrAssigned(SemanticModel semanticModel, StatementSyntax statement, ILocalSymbol identitySymbol)
   at CodeCracker.CSharp.Usage.DisposableVariableNotDisposedAnalyzer.AnalyzeObjectCreation(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__44`1.<ExecuteSyntaxNodeAction>b__44_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)

I couldn't find similar bug and I am using last release version of the Code Cracker (1.1.0). From NuGet I have only Code Cracker insalled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant